Clive Walkden

PHP, MySQL, HTML5, CSS3, jQuery code and ideas.

Linux Posts

Running Linux Commands in the Background

Running Linux Commands in the Background

in Linux
by
with 0 Comments

When running a large complex command or time consuming command such as moving a large quantity of files from one drive to another or running a mass rename script, your terminal will become locked and only become available when that command has finished running.

To bypass this and run the command in the background simply append an & to the command. You will then be given a process ID of the command as it runs in the background.

Syntax:

command &

Here is a working example. I want to move all my .arw files from one drive to another, as i have quite a few large arw files this process takes up a lot of time. I also tend to access my linux box via ssh so don't want multiple connections to the terminal. This is the command i run:

mv *.arw /media/photos/ &

To see what jobs you have running use the following command

jobs

The following is a sample output that will be returned by this command

[1]+ Running mv *.arw /media/photos/ &

The [1] returned is the job ID, if you want to see the process ID then pass the -l option as below

jobs -l

This will return the same information as the main command but also include the process ID which is  useful if you want to kill a process. See sample output below:

[1]+ 30125 Running mv *.arw /media/photos/ &

If you want to bring the job to the foreground use the following syntax:

%JOB_ID

so for our example:

%1

This will then show the command that you are running with any output in my case it will just display:

mv *.arw /media/photos/

To send it to the background again simply ctrl + z and then %1 & this will run it again in the background

comments powered by Disqus