nohup &

I would like to run a script in the background and to keep running even after I log out.

To run a command or script in the background use an ampersand, e.g.,

# ./long-running-script.sh &

This is equivalent to suspending the process with Ctrl-Z, and then issuing bg,

# ./long-running-script.sh
^Z
[1]+  Stopped                 ./long-running-script.sh
# bg
[1]+ ./long-running-script &
#

However, to keep the command running even after logging out use nohup, e.g.,

# nohup ./long-running-script.sh > ./long-running-script.log &
[1] 31163
# nohup: ignoring input and redirecting stderr to stdout

To manage processes in the background, the commands jobs, bg, and fg are useful

# jobs
[1]-  Running                 nohup long-running-script.sh > ./long-running-script.log &
[2]+  Running                 nohup some-other-script.sh &
# fg 1
...
This entry was posted in bash, shell tips. Bookmark the permalink.

Comments are closed.