I have a bash script that waits 3 minutes before the daemon signal. During these 3 minutes I need him to exit if he received SIGINT .
My current script works when run from bash, however, when I run it from another (C) program using a call to system() , it does not exit when it is sent to SIGINT .
Here is my current script:
#!/bin/bash trap 'exit' INT sleep 180 & wait trap '' INT /etc/init.d/myd sync
This is how I run it:
kill -INT `pgrep myscript.sh` 2>/dev/null; ! pgrep -x "myscript.sh" > /dev/null && /opt/my/scripts/myscript.sh &
The same liner does not work when launched using the system() call.
PS: Basically, I use this mechanism to run the /etc/init.d/myd sync command after it was only called for the last time, if it was called several times in 3 minutes.
EDIT 1
C code as requested:
system("kill -INT `pgrep myscript.sh` 2>/dev/null; ! pgrep -x \"myscript.sh\" > /dev/null && /opt/my/scripts/myscript.sh &");
The C program is quite huge (spanning dozens of files), so I just insert a specific call here. The program is supposed to run as a daemon, but I get this problem even if I don't run it as a daemon (using the command line).
I can reproduce this with the following trivial C code:
#include <stdlib.h> int main(int argc, char *argv[]) { system("kill -INT `pgrep myscript.sh` 2>/dev/null; ! pgrep -x \"myscript.sh\" > /dev/null && /opt/my/scripts/myscript.sh &"); return 0; }
source share