Signal not received by bash script run using system(3)

Viewed 259

I have a bash script which waits for 3 mins before signaling a daemon. During these 3 mins, I need it to exit if it receives SIGINT.

My current script works when run from the bash, however, when I run it from within another (C) program using the system() call, it doesn't exit when it is sent SIGINT.

Here's my current script:

#!/bin/bash

trap 'exit' INT
sleep 180 &
wait
trap '' INT

/etc/init.d/myd sync

Here's how I'm running it:

kill -INT `pgrep myscript.sh` 2>/dev/null; ! pgrep -x "myscript.sh" > /dev/null && /opt/my/scripts/myscript.sh &

The same one liner, when run using the system() call, doesn't work.

PS: Basically, I'm using this mechanism to run the /etc/init.d/myd sync command after only the last time it is called if it is called multiple times within 3 minutes.

EDIT 1

The 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 tens of files), so I'm only pasting here the specific call. The program is supposed to run as a daemon, but I get this problem even when I don't run it as a daemon (using a command line switch).

I'm able to 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;
}
1 Answers
Related