Results 1 to 4 of 4
when running a shell script
I want to make it wait for a signal somewhere inside the script,
like
Code:
function pause(){
read -p "$*"
}
echo "start"
pause
echo ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 10-30-2012 #1Just Joined!
- Join Date
- May 2012
- Posts
- 82
make shell script wait and send signal to recover it
when running a shell script
I want to make it wait for a signal somewhere inside the script,
like
and then send a signal(when some conditions are satisfied) to make the script continueCode:function pause(){ read -p "$*" } echo "start" pause echo "stop"
is it possible or not?
thanks!
- 10-30-2012 #2Trusted Penguin
- Join Date
- May 2011
- Posts
- 3,673
sure, use bash's trap function and a for or while loop.
e.g.:
untested code, but that is the general idea.Code:#!/bin/bash function some_command() { echo "sent $0 a SIGUSR1 signal, so doing command..." } trap "some_command" SIGUSR1 while :; do echo sleeping sleep 1 done
- 11-07-2012 #3Just Joined!
- Join Date
- May 2012
- Posts
- 82
- 11-08-2012 #4Trusted Penguin
- Join Date
- May 2011
- Posts
- 3,673
The trap bash function will detect when a signal is sent to the script in which it has been set. You can send a signal to any program (or script) using "kill -$SIGNAL" where $SIGNAL is one of the signals listed by "kill -l" (lower case L).
There is an updated version of the script at the bottom of the post. Copy it to a file called "script.sh". In one terminal, run this script. e.g.:
it will show output like this:Code:./script.sh
then in another terminal, send the pid of the script (16501 in this case) the signal you define in script (i use SIGUSR1 but you can use others) using the kill command, e.g.:Code:program script.sh, pid 16501 is sleeping... program script.sh, pid 16501 is sleeping...
and you'll see this in the output of the script.sh in the first terminal:Code:kill -SIGUSR1 16501
obviously you'll want to modify script.sh to run what you want.Code:program trap.sh, pid 16501 is sleeping... sent ./trap.sh a SIGUSR1 signal, so doing command... do something, like kill some app, or quit this app i think i will exit
here's the code:
Code:#!/bin/bash prog=${0##*/} function some_command() { echo "sent $0 a SIGUSR1 signal, so doing command..." pause=1 } trap "some_command" SIGUSR1 while :; do if test $pause; then echo do something, like kill some app, or quit this app echo i think i will exit exit fi echo program $prog, pid $$ is sleeping... sleep 1 done


Reply With Quote

