Linux Programmer | RHCE | RHCSA

Search This Blog

Tuesday, 30 October 2018

Running bash commands in parallel.

Introduction

A modern server is typically multi-core, perhaps even multi-CPU. That is plenty of computing power to unleash on a given job. However, unless you run a job in parallel, you are not maximizing the use of all that power.

Below are some typical everyday operations we can speed up using parallel computing:
  1. Backup files from multiple source directories to a removable disk.
  2. Resize image files in a directory.
  3. Compress files in a directory.

Image result for bash

To execute a job in parallel, you can use any of the following commands:
  • ppss
  • pexec
  • GNU parallel
This post focuses on the GNU parallel command.

Installation of GNU Parallel

To install GNU parallel on a Debian/Ubuntu system, run the following command:
$ sudo apt-get install parallel

General Usage

The GNU parallel program provides many options which you can specify to customize its behavior. Interested readers can read its man page to learn more about their usage. In this post, I will narrow the execution of GNU parallel to the following scenario.
My objective is to run a shell command in parallel, but on the same multi-core machine. The command can take multiple options, but only 1 is variable. Specifically, you run concurrent instances of the command by providing a different value for that one variable option. The different values are fed, one per line, to GNU parallel via the standard input.
The rest of this post shows how GNU parallel can backup multiple source directories by running rsync in parallel.

Parallel backup

The following command backs up 2 directories in parallel: /home/peter and /data.
$ echo -e '/home/peter\n/data' | parallel -j-2 -k --eta rsync -R -av {} /media/myBKUP

Standard input

The echo command assembles the 2 source directory locations, separated by a newline character (\n), and pipes it to GNU parallel.

How many jobs?

By default, GNU parallel deploys 1 job per core. You can override the default usint the -joption.
-j specifies the maximum number of parallel jobs that GNU parallel can deploy. The maximum number can be specified in 1 of several ways:
  • -j followed by a number

    -j2 means that up to 2 jobs can run in parallel.
  • -j+ followed by a number

    -j+2 means that the maximum number of jobs is the number of cores plus 2.
  • -j- followed by a number

    -j-2 means that the maximum number of jobs is the number of cores minus 2.
If you don't know how many cores the machine has, run the command below:
$ parallel --number-of-cores
8

Keeping output order

Each job may output lines to the standard output. When multiple jobs are run in parallel, the default behavior is that a job's output is displayed as soon as the job finishes. You may find this confusing because the output order may be different from the input order. The -k option keeps the output sequence the same as the input sequence.

Showing progress

The --eta option reports progress while GNU parallel executes, including the estimated remaining time (in seconds).

Input place-holder

GNU parallel substitutes the {} parameter with the next line in the standard input.
Each input line is a directory location, e.g., /home/peter. Instead of the full location, you can specify other parameters in order to extract a portion thereof - e.g., the directory name(/home) and the basename (peter). Please refer to the man page for details.

Summary

GNU parallel is a tool that Linux administrators should add to their repertoire. Running a job in parallel can only improve one's efficiency. If you are already familiar with xargs, you will find the syntax familiar. Even if you are new to the command, there is a wealth of on-line help on the GNU parallel website.

How to mount USB drives on VirtualBox


Prerequisites
The VirtualBox extension pack must be installed on the VirtualBox host. 

Configuring USB

Follow the steps below to configure VirtualBox USB.






  1. Add user to vboxusers group.
    VirtualBox access to the host's USB drives is only ranted to users of the vboxusers group. As root on the host, run the following command to add each VirtualBox user (e.g., peter) to the group.
     $ usermod -aG vboxusers peter
    
  2. Power off VM.
    VirtualBox defaults to using USB Controller 1.1 (OHCI). Modern hardware uses USB Controller 2.0(EHCI) and USB Controller 3.0(xHCI). Before you can change the USB controller protocol, the virtual machine must be powered off.
  3. Open VirtualBox Manager, click Settings, and select USB.
  4. Specify USB Controller.
    Select either USB 2.0 (EHCI) Controller or USB 3.0 (xHCI) Controlleraccording to your actual hardware.

    Note that you can add USB Device Filters to define the types of USB drives which will be automatically made visible to the guest OS. Be forewarned that the USB drive, once made visible to the guest OS, will no longer be available to the host. More on the use of device filters in the next section.

Accessing USB drive

Below is the step-by-step procedure to mount and access a flash drive.
  1. Insert the flash drive into your host machine's USB port.
  2. Unmount the flash drive (if it is auto-mounted on your host).
    Making it available to the guest will automatically and instantly unmount it from the host. To avoid any data loss due to pending writes to the drive, it is a good practice to explicitly unmount the drive prior to handing control to the guest.
  3. Power on the system as guest.
  4. Assign USB drive to guest OS.

    Open the virtual system console, and right click the USB drive icon.

    Click to select your USB drive.

    Note that this is a 1-time assignment only. Please see instructions at the end of the section on how to automatically assign this particular USB drive for all subsequent sessions.
  5. Login to system, and mount the drive.
    You can mount a MS-DOS based flash drive by running the following commands as root. Replace /dev/da0s1 with the proper device identifier for your USB drive. (You can find out the exact device ID by first running dmesg to identify the device name, e.g. da0, andfdisk to reveal the disk partition structure, e.g., s1.)
     # mkdir -p  /media/usb
     # mount -t msdosfs  /dev/da0s1  /media/usb
    
    To unmount the drive,
     # umount /media/usb
    
To always automatically assign a particular USB drive to the guest OS, open the VirtualBox Manager, click Settings, and then USB.







Finally, click Add USB device filter (with the + sign) icon, and select the USB drive that is currently inserted in the host.

Reset admin password ubuntu

How to reset administrative password in ubuntu

  1. start machine.
    After the BIOS screen appears, press down the left shift key to enter GRUB. This step can be quite finicky, and you may need to repeat it several times until you get the timing just right.
  2. Select the Advance Option for ubuntu
  3. .
  4. In Grub Version list, scroll down to the top Recovery mode line, and press Enter.
    If the Linux kernel image had been upgraded on the machine before, you would see multiple recover mode lines on the screen. Select the recovery mode line that corresponds to the latest Linux image(that is nearest to the top).
  5. In the Recovery Menu screen click on root 'Drop to root shell prompt'.
  6. Press Enter again.
    If you have set root a password, you would be prompted to enter it at this step. Otherwise, just press Enter to continue.
  7. Remount filesystem.
    After all the hard work, you are now at the root shell prompt. The filesystem at this point is read-only. Remount the file system to add write permission.
    $ mount -o rw,remount /
    
  8. Reset admin user password now.
    Use the passwd command to change the password for the admin user.
    $ passwd <username>
    
    Press Control-D to return to the recovery menu.
  9. Click on resume.
  10. Exit recovery.
    Press OK to exit recovery mode and continue booting.
Here you Go.

Password resetted successfully.

SSH not working with password after upgrade ubuntu 22.04 or above

In recent upgrade of ubuntu 22.04 and above we are not able to login server with SSH password. but when we try to login with key then it all...