Running Linux commands in the background: ampersand (&) or screen

All we need is an easy explanation of the problem, so here it is.

Running an app in the background can be done by adding ampersand (” &”) at the end of the app or via screen (by opening a screen session) if the app needs input now and then.

Is there any other benefit apart from the one above (adding input to the application)?

I’m curious if there’s any other benefit from a low level details perspective – for example I presume the changes for the app to be shut by the kernel are slightly small if it’s run via screen rather than with & at the end.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

Using & will cause the program to run in the background and wait if it needs input. Which makes it only useful for programs that do not need input.

The program will also terminate if you log out, unless you using the nohup command. That will capture the signal 1 (HangUP) and write all output to a file.

If you do need to give input now and then then there are three options:

  1. Manually bring the program to the foreground using fg.
    Or fg jobnumber. (See jobs). Disadvantages: 1) Extra work 2) How do you know it is waiting for input unless you tail -f nohup.out?
  2. Use Screen.
  3. Use tmux.

Method 2

If you run it in screen you can close your terminal and it is still running.

If you run it via “&” the application receives a HUP command, causing it to quit, if you close your current terminal.

You can combine it with nohup:

nohup /bin/mycommand &

to keep it running.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply