What is the easiest way to "detach/daemonize" a Bash script? What is the easiest way to "detach/daemonize" a Bash script? bash bash

What is the easiest way to "detach/daemonize" a Bash script?


Section 3.7.6 of the Bash Manual says:

The shell exits by default upon receipt of a SIGHUP. Before exiting, an interactive shell resends the SIGHUP to all jobs, running or stopped. Stopped jobs are sent SIGCONT to ensure that they receive the SIGHUP. To prevent the shell from sending the SIGHUP signal to a particular job, it should be removed from the jobs table with the disown builtin (see Section 7.2 [Job Control Builtins], page 88) or marked to not receive SIGHUP using disown -h.

So, using either nohup or disown should do the trick. Or you can do:

trap "" 1sleep 10say hello

That 'trap' line ignores signal 1, SIGHUP; you can probably also write 'trap "" HUP".


nohup yourscript.sh 10 "hello" &#     ^your script  ^^your parameter 1#                      ^^^^^^^your parameter 2

This will detach the script from the terminal, and it won't be killed when the terminal closes. Note the & at the end; you can pass parameters to your script normally. Then yourscript.sh could be:

#!/bin/bashsleep $1;say "$2";exit;


You need to use nohup and background together. I just tried this on OS-X to verify that it works:

nohup ./say-hello.sh &