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:
I have 2 and 15 because i was testing what i mentioned above.
My shell is tcsh.
It works , 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
#!/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
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).
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:
but it does not work
My shell is tcsh and i got freebsd-8.2-Release
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:
But this test may be unnecessary as you appear to want polipo stopped after tor has exited, regardless of any errors.
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)
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)
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)
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)
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)
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)
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)
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)