Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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" 
else 
    echo "TpsHub is NOT running" 
fi 
exit
share|improve this question
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
add comment

2 Answers

  while true ; do
     if .... (your condition)
            echo "TpsHub is RUNNING" 
            . /path/to/stopping/script
            sleep 5
     else
            echo "TpsHub is NOT running" 
            . /path/to/deploy/script
            exit 0
    fi
 done

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

share|improve this answer
    
Thank you Very much for your help. I greatly appreciate it. –  user3100063 Dec 13 '13 at 19:15
add comment

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 
    else
        # announce not running, start tpshub and quit
    fi
done

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 ...
share|improve this answer
    
Thank you very much –  user3100063 Dec 19 '13 at 14:14
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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