Find the answer to your Linux question:
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.
  1. #1
    Just Joined!
    Join Date
    May 2012
    Posts
    85

    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

    Code:
    function pause(){
       read -p "$*"
    }
    echo "start"
    pause
    echo "stop"
    and then send a signal(when some conditions are satisfied) to make the script continue

    is it possible or not?
    thanks!

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,307
    sure, use bash's trap function and a for or while loop.

    e.g.:
    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
    untested code, but that is the general idea.

  3. #3
    Just Joined!
    Join Date
    May 2012
    Posts
    85
    I'm not familiar with this, can you make it more clear?
    thanks

    Quote Originally Posted by atreyu View Post
    sure, use bash's trap function and a for or while loop.

    e.g.:
    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
    untested code, but that is the general idea.

  4. #4
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,307
    Quote Originally Posted by esolve View Post
    I'm not familiar with this, can you make it more clear?
    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.:

    Code:
    ./script.sh
    it will show output like this:
    Code:
    program script.sh, pid 16501 is sleeping...
    program script.sh, pid 16501 is sleeping...
    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:
    kill -SIGUSR1 16501
    and you'll see this in the output of the script.sh in the first terminal:
    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
    obviously you'll want to modify script.sh to run what you want.

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •