Checking for file redirections is not robust, since nohup
can be (and often is) used in scripts where stdin, stdout and/or stderr are already explicitly redirected.
Aside from these redirections, the only thing nohup
does is ignore the SIGHUP
signal (thanks to Blrfl for the link.)
So, really what we're asking for is a way to detect if SIGHUP
is being ignored. In linux, the signal ignore mask is exposed in /proc/$PID/status
, in the least-significant bit of the SigIgn
hex string.
Provided we know the pid of the bash script we want to check, we can use egrep
. Here I see if the current shell is ignoring SIGHUP
(i.e. is "nohuppy"):
$ egrep -q "SigIgn:\s.{15}[13579bdf]" /proc/$$/status && echo nohuppy || echo normal
normal
$ nohup bash -c 'egrep -q "SigIgn:\s.{15}[13579bdf]" /proc/$$/status && echo nohuppy || echo normal'; cat nohup.out
nohup: ignoring input and appending output to `nohup.out'
nohuppy