From Linux Shell Scripting Tutorial - A Beginner's handbook
Jump to navigation Jump to search

An & is part of UNIX / Linux job control. It can run the command in the background, and gives back the prompt to user so that you can run other programs.


The syntax is as follows to run process in the background:

command-name &

/path/to/command2 arg1 arg2 &

/path/to/long/script/ arg1 &

Example: Running Background Processes

To background a process called ping, append an & at the end of the ping command line, enter:

ping > /tmp/ping.output &

Sample outputs:

[3] 4598

The 3 indicating job number and 4598 indicating PID set by the Linux or UNIX system.

List Background Processes

To list background processes use the jobs command:


Sample outputs:

[1]+  Stopped                 ping
[2]   Running                 gedit &
[3]-  Running                 ping &>/tmp/ping.output &

A Note About Interactive Programs

Do not put commands and programs that needs input from the keyboard or require some sort of user interaction. The following example will not work as vi text editor require user interaction from the keyboard:

vi demo.c &

The passwd command change user password and the user is first prompted for his/her old password, if one is present. In short the passwd needs input from the keyboard so do not put it into background:

passwd vivek &