Sh script sends SIGINT to a process


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sh script sends SIGINT to a process
# 1  
Old 03-02-2011
Sh script sends SIGINT to a process

Hello,

What I want to accomplish is this:
I have made a very simple script that runs 2 commands, polipo proxy and tor. The script runs successfully and the output of tor is visible to the screen.
I am using the trap command in order to catch the ctrl+c button combo in order to stop polipo (/usr/local/etc/rc.d/polipo stop). This does not work because, i think, tor catches the signal and exits gracefully and polipo stays running.

I think i have 2 solutions, neither of which i know how to implement:
1. The script runs tor in the "background" (if possible with visible tor output as it was), catches the SIGINT signal and executes the "/usr/local/etc/rc.d/polipo stop" command AND sends the SIGINT signal to the tor process as well, so both exit gracefully.

2. Both the script and the tor process catch the SIGINT signal and exit gracefully.

The script is this:
Code:
#!/bin/sh

echo "Initializing Tor..."

/usr/local/etc/rc.d/polipo onestart

# Start tor
tor

# catch ctrl^c to close polipo as well
trap '/usr/local/etc/rc.d/polipo stop' 2 15

I have 2 and 15 because i was testing what i mentioned above.
My shell is tcsh.

Thanks

Last edited by redsolja; 03-02-2011 at 03:52 PM..
# 2  
Old 03-02-2011
If Tor is catching the SIGINT and exiting gracefully can't you just:

Code:
#!/bin/sh
echo "Initializing Tor..."
/usr/local/etc/rc.d/polipo onestart

# catch ctrl^c to close polipo as well
trap '/usr/local/etc/rc.d/polipo stop; quit' 2 15

# Start tor
tor
 
echo "Cleaning up Tor..."
/usr/local/etc/rc.d/polipo stop

# 3  
Old 03-03-2011
It works Smilie, thank you very much.
The thing is that i do not know why it does. Just because you changed the order of the commands executed? I do not get why it works now and why it didnt in the first place... Would be grateful if you could explain Smilie

#!/bin/sh echo "Initializing Tor..." /usr/local/etc/rc.d/polipo onestart # catch ctrl^c to close polipo as well trap '/usr/local/etc/rc.d/polipo stop; quit' 2 15 # Start tor torThats the script im using, i do not stop polipo with another command

Thanks much anyway.

PS: the quit command is not necessary, i get a "command not found error", but the script does work
# 4  
Old 03-03-2011
Yes the quit was a typo it should probably be something like exit 2, still it's good to know that your trap code is not executing. The exit is nice as it allows anything that calls this script to know if an interupt was trapped or not.


Things to note is that polipo is a background daemon (I assumed this because it's being started by an rc.d script). tor on the otherhand is running in the foreground control dosn't return to your script until tor exits (as you said usually as a result of the users typing ctrl-c).

So you can see that traping interupts after tor exists is closing the barn door after the horse has bolted.

I moved the trap to before tor starts and also added a clean shutdown after tor, this is for instances where tor exist with a ctrl-c interupt (e.g. tor fails to start due to error, crashes, or some user input causes tor to exit without the need for ctrl-c).
# 5  
Old 03-04-2011
Thank you very much, i really do appreciate your answer.
Gave me quite an insight about sh signals for now:O
Ty again

---------- Post updated at 07:00 AM ---------- Previous update was at 03:24 AM ----------

One more thing, from what you said, wouldnt it be better if i had an if clause to check whether tor started successfully and then instruct the /usr/local/etc/rc.d/polipo stop? How is that implemented to the above script? Im kinda messed up with this check, what i tried is:
Code:
if [ $? -eq 1]; then
/usr/local/etc/rc.d/polipo stop;
fi

but it does not work Smilie
My shell is tcsh and i got freebsd-8.2-Release
# 6  
Old 03-06-2011
The code you posted is more bash/ksh style, you use tcsh which is a C shell (csh) compatible script (see csh programming considered harmful). If you must stick with tcsh then you need csh style constructs:

Code:
if ( $? != 0 ) then
    /usr/local/etc/rc.d/polipo stop
endif

But this test may be unnecessary as you appear to want polipo stopped after tor has exited, regardless of any errors.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Always pass SIGINT in ksh

The following command will run and wait for input from the user. /usr/sap/SAP/webdisp/wdispmon pf=/usr/sap/SAP/webdisp/profile What I would like to do is (in one command): - Add the above line to a ksh script - Receive the output - and send a SIGINT I have seen many posts on how to... (3 Replies)
Discussion started by: sapsid
3 Replies

2. UNIX for Dummies Questions & Answers

SIGINT issue

May i know what are the possible causes for SIGINT other than ctrl-c? Thanks (17 Replies)
Discussion started by: pandeesh
17 Replies

3. Shell Programming and Scripting

Trapping SIGINT after restarting program.

Tried to add a function to my control_c interrupt here. It works but has one little bug. If the user selects to run the function instead of exiting, the program restarts itself without forking as it should. However, after that control_c no longer works again. I wanted to allow the user to run... (1 Reply)
Discussion started by: Azrael
1 Replies

4. Shell Programming and Scripting

Script to Alert a file and sends an email

Hello All, I need a script which alerts me when a files arrive in a directory. I don't need every file. but i need some specific pattern file. And i need to get automatic email gettin as an alert For Example: /a/b/c/ : directory format of file should take regualr expression or manually... (2 Replies)
Discussion started by: krux_rap
2 Replies

5. UNIX for Dummies Questions & Answers

BASH script that sends text message or email

Hi, I have a BASH shell script that batch processes data. I often start this script before I leave to go home for the day, and leave it processing over night. It has come to my attention that it would be very useful for me to add the capability of making the script notify me about certain things... (2 Replies)
Discussion started by: msb65
2 Replies

6. Shell Programming and Scripting

Intercepting SIGINT in a bash script

I've written a bash script which captures video with DVgrab. Because of the nature of the tapes that I am digitizing, sometimes I want to quit capturing before the time that I set for DVgrab. When this is the case I press Ctrl-c and DVgrab exits cleanly, my problem is that there is additional... (5 Replies)
Discussion started by: Starcast
5 Replies

7. Shell Programming and Scripting

Running script that sends an html formatted email fails when its run as cronjob

Hi Im very new at working with unix and this problem I simply can not understand. I know there are a lot of threads about problems with shell scripts behaving differently when run from a terminal and from a cronjob. I have tried everything(almost) but I still havent cracked this problem. Im... (15 Replies)
Discussion started by: Nightowl
15 Replies

8. Shell Programming and Scripting

write the script to get the file size and sends an email

hi all Anybody have an idea to write the script to get the file size and sends an email when file size increse more than 10mb. thanks (10 Replies)
Discussion started by: s_linux
10 Replies

9. Shell Programming and Scripting

PHP Script that sends mail - Postfix breaks it

I have a PHP Script that works perfectly on a server that uses Sendmail. However I tried to port it to a new server that has the Postfix to Sendmail compatibility interface, and it doesn't work properly. The php.ini file has the path for sendmail on both servers set to: /usr/sbin/sendmail -t... (0 Replies)
Discussion started by: boopfm523
0 Replies

10. Programming

Problem with handling SIGINT

For a program I am designing, which involves handling the keyboard input Ctrl^c (SIGINT), it is taking ages for the program to actually recognise and perform the corresponding action whenever I run it and hit Ctrl^C at the CL. I have to do at least 3 Ctrl^Cs before the program will actually... (3 Replies)
Discussion started by: JamesGoh
3 Replies
Login or Register to Ask a Question