How do I run a command in the background on linux?
-
Also see: Man page for bash
To demonstrate we will use the
sleep
command as an example. The sleep command make the process, task or thread inactive for a period of time. See in depth explanation here: https://en.wikipedia.org/wiki/Sleep_(system_call)Try running sleep with a time values in seconds. It sleeps for 3 seconds and comes back to the shell prompt.
$ sleep 3 $
To run a command in the background we use the ampersand
&
at the end of the command.Notes:
Unless some automated way of supplying the user input; any command that requires user input will stop for the user input and not designed to run in the background.
Exiting your current bash session with also terminate any background jobs. There are ways to prevent this and will be shown in another article.
This is why the
sleep
command is being used to demonstrate, we can have it run for a long time and it does not require interactive use input.To run a job in the background. This has given the background process job number 1 and has the process ID 3603.
$ sleep 50 & [1] 3603
To see the process running run the ps command.
$ ps PID TTY TIME CMD 2304 pts/0 00:00:00 bash 3603 pts/0 00:00:00 sleep 3618 pts/0 00:00:00 ps
© Lightnetics 2024