• Uncategorized

About linux : What-does-at-the-end-of-a-linux-command-mean

Question Detail

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.

Question Answer

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.

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):

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.

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.

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.

 [email protected]:~$ <command> &


[email protected]:~$ edit &
[email protected]:~$ eclipse &

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.