166

I am a system administrator and I have been asked to run a linux script to clean the system.

The command is this:

perl script.pl > output.log &

so this command is ending with a & sign, is there any special significance of it?

I have basic knowledge of shell but I have never seen this before.

2
  • 12
    If you're on this page looking for the difference between & and &&: & executes the first command in the background; && executes the first command and conditionally proceeds to the second if it exits with success. Aug 8, 2019 at 3:57
  • Also see windows equivalent.
    – djvg
    Nov 17, 2021 at 14:00

5 Answers 5

225

The & makes the command run in the background.

From man bash:

If a command is terminated by the control operator &, the shell executes the command in the background in a subshell. The shell does not wait for the command to finish, and the return status is 0.

2
  • 1
    Is it possible to wait for all the & calls to finish?
    – chovy
    Jan 11, 2021 at 22:14
  • 1
    @chovy yes, with the wait command.
    – Daniel F
    Jun 5, 2022 at 21:48
32

When not told otherwise commands take over the foreground. You only have one "foreground" process running in a single shell session. The & symbol instructs commands to run in a background process and immediately returns to the command line for additional commands.

sh my_script.sh &

A background process will not stay alive after the shell session is closed. SIGHUP terminates all running processes. By default anyway. If your command is long-running or runs indefinitely (ie: microservice) you need to pr-pend it with nohup so it remains running after you disconnect from the session:

nohup sh my_script.sh &

EDIT: There does appear to be a gray area regarding the closing of background processes when & is used. Just be aware that the shell may close your process depending on your OS and local configurations (particularly on CENTOS/RHEL): https://serverfault.com/a/117157.

19

In addition, you can use the "&" sign to run many processes through one (1) ssh connections in order to to keep minimum number of terminals. For example, I have one process that listens for messages in order to extract files, the second process listens for messages in order to upload files: Using the "&" I can run both services in one terminal, through single ssh connection to my server.

These processes running through the "&" will also "stay alive" after ssh session is closed. Pretty neat and useful if the ssh connection to the server is interrupted and no terminal multiplexer (screen, tmux, byobu) was used.

4
  • 1
    nohup will keep processes alive too.
    – Jerry Chin
    Apr 28, 2017 at 6:15
  • 4
    with & we can get PID as well so we can terminate process if we want and also we can check status of that process Jun 12, 2018 at 16:16
  • How to terminate the process? How to get PID? Nov 28, 2020 at 10:00
  • @EmmanuelGoldstein you can use ps aux and then pipe that output to grep to search for what you want, like by command or user. Just to be clear ps is the command and aux are its arguments. unix.stackexchange.com/questions/106847/…
    – Sgnl
    Jun 15 at 22:53
4

I don’t know for sure but I’m reading a book right now and what I am getting is that a program need to handle its signal ( as when I press CTRL-C). Now a program can use SIG_IGN to ignore all signals or SIG_DFL to restore the default action.

Now if you do $ command & then this process running as background process simply ignores all signals that will occur. For foreground processes these signals are not ignored.

2

If you have a command which executes and doesn't return status 0(control of prompt) quickly.

For example:

  1. command gedit launches the default editor gedit UI.

  2. commandeclipse launches eclipse IDE.

Such commands keep throwing the logs of activities in the terminal and don't return the command prompt.

Question is, how to run such commands in background so that, we will get back command terminal and we can use terminal for other tasks.

Answer is: by appending & after such command.

 user@mymachine:~$ <command> &

Examples:

user@mymachine:~$ edit &
user@mymachine:~$ eclipse &

Not the answer you're looking for? Browse other questions tagged or ask your own question.