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.

So I have a custom in-house app developed by a 3rd party. When the app is running, I can verify it is running with the "screen -ls" command. As long as screens are running for rails and freeswitch then I know the app is properly up and running.

We have a specific bash script to stop the services related to the app, and 2nd script to start the services related to the app.

My question is how can I combine both these scripts to restart the app into 1 script that works like the following:

  1. Run script 1 - stop app
  2. Wait until app has shutdown (screen processes are no longer running)
  3. Run script 2 - start app
  4. Wait until app has started
  5. Check "screen" sockets to make sure a rails and freeswitch process is running. If not, then go back to step 1 and repeat.

Right now to restart the app:

  1. I manually run the stop script via /tools/stop_app.sh
    • This then outputs to the terminal to show services shutting down.
    • Once complete, it returns me back to the terminal prompt.
  2. Now I manually run the start script via /tools/start_app.sh
    • This doesn't output anything, but once it has completed it returns me to the terminal prompt.
  3. I then run screen -ls to verify all services for the app is running. (sometimes a service such as freeswitch doesn't start.)
  4. If not, then I re-run the stop/start scripts.

It might be asked why don't I just put everything into one script. Well this custom app is very finicky and due to limited support from the developers, we need to make sure to utilize the exact tools they provided. Hence 1 script that calls the 2 separate scripts provided by the developers.

By integrity check I am referring to checking the "screen" processes to make sure that the ruby and freeswitch screens are running. For the cron, I would like to perform this app restart automatically on a weekly basis.

Note, when I say bash script I am not sure if it correct to say bash or shell. I have no script preference as long as it is a language that usually comes installed by default in Ubuntu Linux.

share|improve this question
    
why do you mention cron and an integrity check? –  strugee Aug 6 '13 at 4:43
    
My apologies, I updated my question to include "By integrity check I am referring to checking the "screen" processes to make sure that the ruby and freeswitch screens are running. For the cron, I would like to perform this app restart automatically on a weekly basis. " –  Damainman Aug 6 '13 at 4:51
    
What is the question? –  suspectus Aug 6 '13 at 5:25
    
I put it in bold. Basically I want to be able to manually run one script that calls a different script, waits for it to run/complete, and then runs a second script. This why I only need to run one script which executes both the start/stop script instead of needing to manually run those scripts individually. –  Damainman Aug 6 '13 at 5:56
add comment

1 Answer

You should be able to do something like this:

#/usr/bin/env bash

## We will use this function later to check if 
## everything has been correctly started.
function check_if_init_OK {
    ## You will need to edit this to add whatever services
    ## you have to check for.
    c=0; ## This is a counter, initialized to 0

    ## For each of the service names you are interested in.
    ## to add more, just put them after freeswitch, separated by a
    ## space they way they are now (e.g. a b c).
    for service in freeswitch foo bar baz; do

      ## Every time this loop is executed, $service will be
      ## one of the services you put in the list above. The
      ## script will run screen -ls and search for the name of the
      ## service. If it finds it, it will increment the counted $c by one.
      ## That is the meaning of '&&' in bash, x &&y means do y if x 
      ## was successful.
      screen -ls | grep $service >/dev/null 2>/dev/null && let c++; 
    done
    ## This just makes the function return $c which at this point
    ## will be how many of the the services you gave in the list have 
    ## been found.
    echo $c
}

## Run the first script -> stop app
script1.sh &

## Wait until it has stopped running
while screen -ls | grep script1.sh; do sleep 1; done 

## Run the second script -> start app and wait 15 seconds
script2.sh && sleep 15

## Check that everything has started OK. The function
## will return the number of services of interest that
## are up and running. While this is less than the number
## of services of interest, re-run script2.sh and check again.
## This loop will run the check_if_init_OK function until the 
## number returned (the number of running services of interest) is
## 3. You should change the 3 to reflect the actual number of services 
## you are looking for. So, as long as some services have not started,
## run script1.sh and then script2,sh and check if this time eveything
## has started OK. This loop will only exit when everything is working OK.
while [ "$(check_if_init_OK)" -ne 3 ];do
   script1.sh &&  script2.sh
done
share|improve this answer
    
Thank you for the extensive reply :). Would you be able to provide a reference so I can see what the code inside the check_if_init_OK function does as well as the code relating to waiting until script1 has finished before moving on to script2? The only thing is if all the services did not properly start, I need to run script1(stop) again before running script2(start) because if I run script2 without running script1, it sometimes causes services such as rails to appear twice instead of once in the screen -ls. –  Damainman Aug 6 '13 at 17:53
    
@Damainman you're very welcome. Is it clearer now? I'm not sure what level of bash knowledge you have. –  terdon Aug 6 '13 at 18:12
    
Awesome! Definitely makes more sense. I really have no bash scripting knowledge at the moment. So I was expecting the while function at the bottom to loop the entire script if all the services weren't running. I have to wait until after work hours to test this out, but two quick question.. 1. what does the sleep cmd do in the "## Wait until it has stopped running" section? 2. Is there a way to say something like wait 15 seconds after running script2.sh before checking if all the services are running? I have noticed sometimes the processes do not appear in screen -ls until a few seconds. –  Damainman Aug 6 '13 at 18:27
1  
@Damainman In general in the Unix world, man command gices you the manual page for command. Have a look at man sleep, sleep simply tells bash to wait the specified amount of seconds. So, sleep 1 is there so you don't flood the CPU with requests but wait a second between each. The function does not loop the script, but the script is run again if the function does not show that all is well. I have added a sleep 15 to make it wait 15 seconds. –  terdon Aug 6 '13 at 18:34
    
Awesome! will update this post as soon as I get a chance to utilize it tonight. –  Damainman Aug 6 '13 at 18:49
show 2 more comments

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.