Hi All,
Greetings of the day!!
I have made a shell script to check a application service to be running and if found not running, then it should start the service. I did run the script manually on the server however when I schedule it to run every 5 minutes through cron, it does not. Could sone please check and suggest if I am missing something.
Script used:-
#!/bin/bash
# Define the name of the service
SERVICE_NAME="trcbroker.service"
# Check if the service is running
if systemctl is-active --quiet $SERVICE_NAME ; then
echo "$SERVICE_NAME is running."
else
# Attempt to start the service
systemctl start $SERVICE_NAME
# Check if the service started successfully
if systemctl is-active --quiet $SERVICE_NAME ; then
echo "$SERVICE_NAME started successfully."
else
echo "Failed to start $SERVICE_NAME."
fi
fi
In cron I am using the below line to make the script run which I have stored in /tmp with 755 permissions:-
*/5 * * * * /path/to/monitor_trcbroker.sh
Regards,
VT
Hi VT, welcome to the Forums!
So, two things âŚ
First, this is re-inventing the wheel a little. There is a standard tool called âmonitâ which does this (and many many other things) which doesnât need cron for scheduling. Even better, it provides an optional remote API if you want it to feed a dashboard of some kind. (and itâs really easy to use)
Second, in the case of this script, the if-test needs to evaluate the result of the systemctl command, rather than (effectively) a string value, so it should look something like this;
if `systemctl is-active --quiet $SERVICE_NAME`
then
echo "Running"
fi
i.e. you need the back-ticks around the shell command to evaluate itâs result. OR, you could use $() nomenclature like this;
if $(systemctl is-active --quiet $SERVICE_NAME)
then
echo "Running"
fi
Historically I would have surrounded the expression with ââ but in this instance itâs not needed. You can check this easily by running it twice against something you know is or isnât running, use a â!â to negagate the result.
if ! $(systemctl is-active --quiet $SERVICE_NAME)
then
echo "NOT Running"
fi
You can find "monit here;
https://mmonit.com/monit/
The client (which does what you want) is Open Source and available in standard repoâs (apt get monit), the authors also sell a non-OS grapical dashboard.
Sample âmonitâ config file;
check process nginx with pidfile /var/run/nginx.pid
group www
group nginx
start program = "/etc/init.d/nginx start"
stop program = "/etc/init.d/nginx stop"
if 5 restarts with 5 cycles then timeout
1 Like
Thanks a LOT in taking time out and responding to my query
What I did, I updated the script as suggested and saved it to my local profile and gave 755 and it worked with Cron and started the service however I will definitely explore the monit as well.
regards,
VT
OK - I admit, this mightnât help with this specific query, but Iâll add it anyway!
Because I use multiple different distros, and quite frequently rebuild my laptop⌠I used to frequently hit this kind of problem.
i have learned to set all commands as variables within scriptsâŚ
example:
#!/bin/sh
testFile=${1}
ECHO=$(which echo)
RM=$(which rm)
if [ -f ${testFile} ]; than
${RM} ${testFile}
if [ ${?} -eq 0]; then
${ECHO} deleted
else
${ECHO} problem
fi
else
${ECHO} nothing to delete
fi
Remember that not all distros use the same shell, so I also recommend you ensure you use the same shell, installing bash, ash (or whatever) and ensuring the symlink (at /bin/sh) points to it (or your scripts do!)
these actions, will make your scripts more transferable, from one distro/computer to another.