PDA

View Full Version : Background process question


tmw
August 20th, 2009, 18:44
Hello,
I have a question, when i run some script in background for example:

./script &

This process should live till it ends or user (who run this script) decided to logoff, to avoid process kill when user exit sheel we use nohup. But it turns out that process put in background will survive without nohup command. I tried this on bash, ksh, sh and process is still working with ppid = 1. Can anyone explain it?

graudeejs
August 20th, 2009, 19:06
I find it normal....

tmw
August 20th, 2009, 19:31
When shell quit it sends SIGHUP to all child processes and they should died i think (nohup makes process ignore SIGHUP signal). So suppose this behavior that i mentioned is normal what for is nohup command?

SirDice
August 20th, 2009, 20:01
Perhaps the script handles SIGHUP?

tmw
August 20th, 2009, 20:15
It can be for example

sleep 500 &

And it still run even when shell that exectute this command was killed. I repat my question what for is nohup when processes will survive.

aragon
August 20th, 2009, 23:07
Nowadays the hangup signal is rarely sent by anything other than a human using kill. The nohup command exists from the days of serial terminals where the UART driver in the tty subsystem should send the userland process a SIGHUP if the line hangs up. When the shell receives a SIGHUP, it sends a SIGHUP to all its children too (you can test this manually), which kills them unless they're started with nohup or otherwise handle SIGHUP.

Shells can also send SIGHUP on normal exit, but I don't know of any that do this by default.

Alt
August 21st, 2009, 04:26
This process should live till it ends or user (who run this script) decided to logoff, to avoid process kill when user exit sheel we use nohup.`we` its linux users? =) In freebsd this is normal action that process stays work

aragon
August 21st, 2009, 04:47
Nowadays the hangup signal is rarely sent by anything other than a human using kill.
Excuse me. SSH does, in fact, send a SIGHUP to the shell if the network connection dies, just like the UART driver does in the case of a serial terminal hanging up. So nohup may still be useful today, but I don't think it's ever been useful for leaving a process running after you explicitly and cleanly exit a shell. Shells only HUP their children in response to being HUP'd themselves.

tmw
August 21st, 2009, 08:49
Thanks for answers. So i can safely exit shell when i have processes in background with no risk. Im little confused because i read in "bash Cookbook: Solutions and Examples for bash Users" that nohup must be used when you have background process and leaves shell otherwise the background process will die.

ironmikie
August 21st, 2009, 09:41
To avoid this kind of problems I always start important processes in screen. Only then I know for sure the process won't be killed, unless the server reboots or screen dies ofcourse.

tmw
August 21st, 2009, 13:03
In bash there is an option "hugonexit", it is by defailt off in bash but when i turn this option on, background processes died when shell dies. I think in the past dying process in background when shell died was normal behavior but now this option is off by default in most shells.

SirDice
August 21st, 2009, 13:38
In bash there is an option "hugonexit",
You mean huponexit ;)

tmw
August 21st, 2009, 14:04
Hehe yes me mistake :)