Running a command in the background from the terminal is a regular task when you need to start a time-consuming task and proceed to work with the terminal.
There is a little difference between doing the same thing on Linux and Windows command line, but it still not so complex.
Run a task in the background on Linux, Mac using & (session dependent running)
As easy as this:
[terminal-command] &
Example:
sleep 30 &
In 30 seconds you will see the output like this:
[1] + done sleep 30
Same, but with redirection of stdout and strerr to file:
[terminal-command] &> [output-file-name] 2>&1
Same, but with redirection of stdout and strerr to /dev/null (when you don’t need the output in file = no output):
[terminal-command] &> /dev/null 2>&1
What is 2>&1
means here?
&1
means – redirect stderr to the same place as stdout >
, 2
means stderr. As a result, both stdout and stderr will be redirected to /dev/null
.
To see all processes running, just type:
jobs
You will see output like this:
[1] + running sleep 30
[1] – is a job id, and +
means that this is the last job started or moved to the foreground if you see -
then it means that it’s the second last you run of one that moved to the foreground.
Bring the latest Process to the foreground:
fg
Bring Process to the foreground by id:
fg %[process-id]
Example:
fg %2
To suspend the running process just press Ctrl+Z.
For example, run sleep command:
sleep 90
After the process starts, just press Ctrl+Z and you will see:
^Z
zsh: suspended sleep 90
To bring the suspended process to background, type:
bg
To see if it goes to background, type:
jobs
[1] + running sleep 90
Run a task in the background on Linux, Mac using nohup (session independent running)
When you run a process in the background with &
and bg
command, then the process will stop after you logout from the terminal session. If you want the process to continue running after session logout, use nohup terminal util. It also gives you ability to start executing some command and move to other work in terminal while some command executing in background.
To do that, use this pattern:
nohup [teminal-command-with-options] &
By default, nohup will send output to nohup.out file in the current directory. Errors will be also redirected to nohup.out file.
To change the file name where all the output will be redirected to, use default shell redirection:
nohup [terminal-command] > [output-file-name] &
To see how it works, lets try to create custom log file with nohup output (tailing system logs). Try to execute:
nohup tail -f /var/log/system.log > system.log &
you will see output like this:
[1] 33445
It means that this job goes to background and has id: 1, type jobs
to check that. Output example:
[1] + running nohup tail -f /var/log/system.log > system.log
You can see now, that in your current directory, you have file: system.log
with part of the same content that you have in /var/log/system.log
.
Try now to logout from the shell and log in again. Then type:
ps aux | grep system.log
You will see something like this:
user 33445 0.0 0.0 4277400 560 ?? SN 5:43PM 0:00.01 tail -f /var/log/system.log
Run a task in the background on Windows using START (session independent running)
On Windows Command Prompt there is an alternative of & Linux command called START.
To check how it works, just type in cmd
:
HELP START
You will get on Windows 10 something like this:
Starts a separate window to run a specified program or command.
START ["title"] [/D path] [/I] [/MIN] [/MAX] [/SEPARATE | /SHARED]
[/LOW | /NORMAL | /HIGH | /REALTIME | /ABOVENORMAL | /BELOWNORMAL]
[/NODE <NUMA node>] [/AFFINITY <hex affinity mask>] [/WAIT] [/B]
[command/program] [parameters]
"title" Title to display in window title bar.
path Starting directory.
B Start application without creating a new window. The
application has ^C handling ignored. Unless the application
enables ^C processing, ^Break is the only way to interrupt
the application.
I The new environment will be the original environment passed
to the cmd.exe and not the current environment.
MIN Start window minimized.
MAX Start window maximized.
SEPARATE Start 16-bit Windows program in separate memory space.
SHARED Start 16-bit Windows program in shared memory space.
LOW Start application in the IDLE priority class.
NORMAL Start application in the NORMAL priority class.
HIGH Start application in the HIGH priority class.
REALTIME Start application in the REALTIME priority class.
ABOVENORMAL Start application in the ABOVENORMAL priority class.
BELOWNORMAL Start application in the BELOWNORMAL priority class.
NODE Specifies the preferred Non-Uniform Memory Architecture (NUMA)
node as a decimal integer.
AFFINITY Specifies the processor affinity mask as a hexadecimal number.
To start some command in background, use pattern:
START /B [command-prompt-command] 2>&1
To send output to file, just use:
START /B [command-prompt-command] > [output-file-name] 2>&1
Same, but send output to nowhere (NUL on Windows is same as /dev/null on Linux/Mac):
START /B [command-prompt-command] > NUL 2>&1
2>&1
here means the same thing as it does in Linux.