What does "&" at the end of a linux command mean?

LinuxShellUnix

Linux Problem Overview


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.

Linux Solutions


Solution 1 - Linux

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.

Solution 2 - Linux

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.

Solution 3 - Linux

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.

Solution 4 - Linux

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.

Solution 5 - Linux

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 &

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRohitView Question on Stackoverflow
Solution 1 - LinuxmgarciaisaiaView Answer on Stackoverflow
Solution 2 - LinuxTimothy JannaceView Answer on Stackoverflow
Solution 3 - LinuxZack SView Answer on Stackoverflow
Solution 4 - LinuxKaustubh KislayView Answer on Stackoverflow
Solution 5 - LinuxOm SaoView Answer on Stackoverflow