-   Programming (
-   -   Shell Script: Getting signal handlers to work when running an application by script (

pkjss 09-13-2011 01:30 AM

Shell Script: Getting signal handlers to work when running an application by script
Ok my subjetc line doesn't clarify my problem so much i agree... here is detailed problem:

I have an application (App_1) which has to run another application (App_2). I can achieve this through a script (/path/to/App_1 /path/to/App_2) and it works, no problem. But my App_2 has some handlers (like if App_2 is running and i issue a "Ctrl-C" during its run i get the status of App_2, like memory used, time remaining before application will finish its job etc). If i run App_2 independent of App_1, it handles those "Ctrl-C" fine, but when i run it through App_1, "Ctrl-C" handler of App_2 doesn't work and it kills the script while App_2 is still running as if nothing happened.

My problem is that in such case when i run App_2 through App_1, how do i make sure that handlers of App_2 are handled properly.

I hope i have made myself clear...but please let me know if you guys have still some doubt.


macemoneta 09-13-2011 01:57 AM

The problem is that the second application has been forked and is running background, while the first application remains in the foreground. The signal that you generate at the terminal goes to the foreground application. To send a signal to app2, you can:

pkill -INT app2
The SIGINT signal send the same interrupt as a ctrl-c.

pkjss 09-13-2011 04:14 AM

I don't want to kill App_2, i can do that by ps aux | grep app_2.

Ok here i go again:

When i run app_2 independently, it runs fine (shows output during runtime) and there are some signals which when given to app_2 during runtime like "CTRL-C", it gives some more switches like:
sw1 - Get runtime left
sw2 - Get memory usage

So after entering Ctrl-C i get above options and when i enter sw1 or sw2 i get further details and then run continues.

Now, if i run app_2 through app_1 like: ./app_1 [options] app_2
it runs fine now and shows the usual output of app_2 on screen, but pressing CTRL-C will now kill app_1 (actually it is expected, as interrupt is now going to app_1 and not to app_2).

What i need is that when i run app_2 through app_1 (./app_1 [OPTIONS] app_2) if i issue CTRL-C interrupt, it goes to app_2 and then app_2 gives me usual options of sw1, sw2 etc.


macemoneta 09-13-2011 09:30 AM

The pkill and kill commands simply sends signals. You can select which signal you want to send. Ctrl-c is the signal called INT (SIGINT), which is what your terminal sends when you hit ctrl-c.

Reuti 09-14-2011 04:23 AM

As you mention, that you use a script: the shell script needs to avoid the default behavior by the trap builtin. Then you can just send the signal to app2 from there (in case you know the pid of app2).

Or, if you don’t know the pid of app2: send the signal to app1. app1 needs a signal handler too, just to forward the signal to app2 and not to do anything, i.e. not to have the default behavior of terminating.

Or, send the ^C to the complete process group and just ignore it in app1.

All times are GMT -5. The time now is 03:17 PM.