Status check of service and restart if its not up

samx

New Member
Joined
Sep 29, 2022
Messages
1
Reaction score
0
Credits
17
Hello Friends, i have few .sh scripts in different directories (like shown below) which is checking status of different services and output status. Current script is not restarting services if its not up. I would like to add this extra check that if service is not up then restart so we don't have to go to individual dir and restart if something is not up. Any example would be helpful.


/script/test1/script1.sh
/script/test2/script2.sh
/script/test3/script3.sh

i believe i need to trick below piece of code to add logic ,

#example for script-1
SCRIPT_DIR=/script/test1
SCRIPT_LOG=/script/logs
SCRIPT_CONTEXT="NULL"

if [ -x "$SCRIPT_DIR/script1.sh"]
then
{
start_script1;
if ["$?" -eq 0 ] ;
then $Logmsg "service1_is_up";
else $Logmsg "service1_is_down";
fi;
}
else
{
$Logmsg "$service1_script_access"
$Logmsg "$service1_is_down"
}
fi
 
Last edited:


samx wrote:
Current script is not restarting services if its not up. I would like to add this extra check that if service is not up then restart

Here's one form of logic, as I understand it, in a testable form.
The program to test whether a particular script is running or not, is called "progRunOrNot".
The particular script to be tested for running status or not, is called "runscript".
"runscript" was created to show how the commmand: progRunOrNot, works.

There are two conditions to test progRunOrNot in, one where runscript is running and one where it's not.

runscript has the following contents:
Code:
#!/bin/bash
while :
do
:
done

Running runscript in a terminal just runs but does nothing.
To stop it, use cntl+c.
It's fit for purpose in this procedure.

The program progRunOrNot has the following contents:
Code:
#!/bin/bash

AA="$(ps -C runscript -o pid=)"

echo $AA

if [ "$AA" > 0 ]
then
{
  echo "runscript running"
  echo "$(date) runscript running" >> /home/flip/logFile
 
}
else
   echo "$(date) not-running" >> /home/flip/logFile
   echo "restarting runscript"
   ./runscript
   echo "$(date) runscript restarted" >> /home/flip/logFile
fi
To test when runscript is running, it's easiest to run it in a terminal separate from where progRunOrNot is started.
If runscript is running, progRunOrNot outputs:
Code:
[flip@flop ~]$ ./progRunOrNot
106586
runscript running
If runscript is not running, it gets started and the output is the following:
Code:
[flip@flop ~]$ ./progRunOrNot

restarting runscript
^C
The ^C is there because runscript was actually started there, as intended, but stopped after starting by the user.
In both cases a date stamped entry is written to the log file.

The above carries the logic of what you are interested in achieving as I understand it.
In progRunOrNot I've preferred to use the ps command to determine whether runscript is running or not.
Of course this is only a schema to work on the logic, rather than produce an end product.
 
Last edited:
Oops... I thought I moved this yesterday. Ah well... I just moved it to 'command line' sub-forum, which is where we place scripting questions. We probably could do with a scripting sub-forum, but we do not have one.
 

Members online


Top