I am a novice to Linux system. My requirement is to run a Linux status script which echos a message (message is either "App is running" or "App is not running").

If the message is "App is running" then run another shell script to stop the App. Then run the status shell script again after a few seconds. When I read "App is not running", then run Deploy script to deploy new application.

The status script is written in Bash:

if [ "$(ps -ef | grep -i cat ${TPSHUB_HOME}/current.pid
        | grep -v grep|awk '{ print $2 }')" ];then 
   echo "TpsHub is RUNNING" 
    echo "TpsHub is NOT running" 
  • 3
    So what do you have so far? Please post some actual code. – Joseph R. Dec 13 '13 at 16:39
  • In addition to @JosephR.'s comment, also can you let us know the platform you are on, please? Just use uname -a – Kevdog777 Dec 13 '13 at 16:55
  • Hi, It is linux machine. We use bash shell. The status script looks like this: #if [ "$(ps -ef |grep -i TpsHub | grep -v grep|awk '{ print $2 }')" ] if [ "$(ps -ef | grep -i cat ${TPSHUB_HOME}/current.pid | grep -v grep|awk '{ print $2 }')" ] then echo "TpsHub is RUNNING" else echo "TpsHub is NOT running" fi exit – user3100063 Dec 13 '13 at 17:39
  • I am told it is zLinix machine. – user3100063 Dec 13 '13 at 18:19
  while true ; do
     if .... (your condition)
            echo "TpsHub is RUNNING" 
            . /path/to/stopping/script
            sleep 5
            echo "TpsHub is NOT running" 
            . /path/to/deploy/script
            exit 0

Will keep running in loop until condition is satisfied, then exit upon finishing deploy. If it doesn't stop within 5s, stop script is rerun

  • Thank you Very much for your help. I greatly appreciate it. – user3100063 Dec 13 '13 at 19:15

Here's another solution that shortens your method of checking whether the PID exists or not by a call to ps -p with your PID:

while true; do
    if ps -p "$(<$TPSHUB_HOME/current.pid)" &> /dev/null; then
        # announce running, kill tpshub and sleep 
        # announce not running, start tpshub and quit

Note that you can also use xargs with ps and a redirection to use the PID value stored in the file:

if xargs ps -p < "$TPSHUB_HOME/current.pid" &> /dev/null; then ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.