LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-13-2011, 01:30 AM   #1
pkjss
LQ Newbie
 
Registered: Sep 2011
Posts: 2

Rep: Reputation: Disabled
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.

Thanks
Pankaj
 
Old 09-13-2011, 01:57 AM   #2
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 326Reputation: 326Reputation: 326Reputation: 326
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:
Code:
pkill -INT app2
The SIGINT signal send the same interrupt as a ctrl-c.
 
Old 09-13-2011, 04:14 AM   #3
pkjss
LQ Newbie
 
Registered: Sep 2011
Posts: 2

Original Poster
Rep: Reputation: Disabled
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.

Thanks
Pankaj
 
Old 09-13-2011, 09:30 AM   #4
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 326Reputation: 326Reputation: 326Reputation: 326
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.
 
Old 09-14-2011, 04:23 AM   #5
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 11.4
Posts: 1,319

Rep: Reputation: 252Reputation: 252Reputation: 252
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
running shell script within expect script xombboxer Linux - Newbie 2 05-01-2011 12:42 PM
start application using shell script Skiltron Linux - Software 2 12-24-2008 12:51 PM
running a shell script which invokes another shell program!!! raghu123 Programming 1 08-04-2008 09:10 AM
executing shell script from java application maheshkodamati Programming 5 02-24-2008 11:39 AM


All times are GMT -5. The time now is 02:04 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration