Stopping process in /etc/inittab kills spawned process. Doesn't happen in rc.local
I'm trying to execute a firmware upgrade while my programming is running in inittab. My program will run 2 commands. One to extract the installer script from the tarball and the other to execute the installer script. In my code I'm using the system() func开发者_运维技巧tion call. These are the 2 command strings below,
system ( "tar zvxf tarball.tar.gz -C / installer.sh 2>&1" );
system( "nohup installer.sh tarball >/dev/null 2>&1 &" );
The installer script requires the tarball to be an argument. I've tried using sudo but i still have the same problem. I've tried nohup with no success. The installer script has to kill my program when doing the firmware upgrade but the installer script will stay alive.
If my program is run from the command line or rc.local, on my target device, my upgrade works fine, i.e. when my program is killed my installer script continues.
But I need to run my program from /etc/inittab so it can respawn if it dies. To stop my program in inittab the installer script will hash it out and execute "telinit q". This is where my program dies (but thats what I want it to do), but it also kills my installer script.
Does anyone know why this is happening and what can I do to solve it?
Thanks in advance.
My guess what happens here is that init is sending the SIGTERM/SIGKILL not only to the process but to the whole process group. It does this to ensure that all children of a process are properly cleaned up. When your program calls system(), it will internally do a fork()/exec(). This newly forked process is in the same process group as you program so it also gets killed.
You could try to run your installer script in a new session by doing a
system( "setsid nohup installer.sh tarball >/dev/null 2>&1 &" );
If your system doesn't provide the setsid commandline utility you can simply write your own. setsid is just a small wrapper around the setsid() system call.
精彩评论