Running Linux commands in the background: ampersand (&) or screen
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:
- Manually bring the program to the foreground using
fg
.
Or fg jobnumber. (Seejobs
). Disadvantages: 1) Extra work 2) How do you know it is waiting for input unless you tail -f nohup.out? - Use Screen.
- Use tmux.
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.