1

I have a bash shell script that uses wait() to determine if an application exited. However, I am having a hard time killing the script/application when I want it to. The idea of the script is to restart the application if it dies or a software update needs to happen, but I need a backdoor or signal trap to be able to kill it without restarting the application.

The program will exit if it updates and then restart, it will also restart if the application exits no matter what. Which is what I need. However, I also need a special way to END this script ,without restarting the app, for special purposes

Here is the script:

# Initial Launch of the application
/app &

while true
do

   PID=$! #process ID of app

   # The app background process uses exit(0) to possibly end.
   wait $PID 

   if [ -f $FILE ];
   then
      echo "Update successfully"
      mv appcp app
      chmod "+x" app
      /app & # restart
   else
      #error happened if file does not exist 
      #restart
      /app &
   fi
done
1
  • while ! -f /some/dir/quit-file ?
    – Jeff Schaller
    Oct 7, 2015 at 2:41

1 Answer 1

2

This sort of thing might work. I've added a "trap" to a shell function named "quitter", which gets called if and when the starter script gets a SIGINT.

#!/bin/bash

function quitter {
    echo "SIGINT handler, disowning $PID"
    disown $PID
    exit 1;
}

trap quitter SIGINT

FILE=appcp
./app 1000 &
PID=$! #process ID of app

while true
do
   # The app background process uses exit(0) to possibly end.
   wait $PID
   if [[ -f $FILE ]]
   then
      echo "Update successfully"
      mv $FILE app
      chmod "+x" app
      ./app &
   else
      echo plain restart
      ./app &
   fi
   PID=$! #process ID of app
done

I'm not exactly certain that doing disown inside of the trap-handler function is the right thing to do, but I can't quite figure out what you're trying to do. When I run the script with a compiled program that just sleeps for 1000 seconds as the "app". A control-C of the "starter" program leaves the "app" running. This is bash 4.3.042-3 on an up-to-date x86_64 Arch Linux machine, kernel 4.1.9.

The big problem I can see with this method is that although the "app" is still running, there's no way for the "starter" script to start monitoring it again. You'll have to kill the "app" somehow, then invoke the "starter" script to get a monitored "app" process.

1
  • This is good I think!, I don't have to have the script to monitor the app again until I restart another instance of the script. Basically I need a "Test Mode" where i can kill the monitor script and take it out of the picture so I can kill the app directly for testing purposes. Oct 7, 2015 at 2:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .