Tuesday, June 20, 2017

Linux Shell script to check is this very script still running or not, with cron job

I was told to write a shell script (in Redhat Linux 7) to schedule few big processes and also told to check if this script still running no need to run again even through next cron job time start.

Means, checking this script status first then decide to run or not (from this script on the run time). this was little tricky. if it was to check another process's status then it was easy.

So here is my solution-

1) save this as check_run.sh
/home/root/scripts/check_run.sh

-------------------------this script will check for  check_run.sh ---------------------
#!/bin/bash
echo "This script name is $(basename $0)" 
if [[ "`/sbin/pidof -x $(basename $0) -o %PPID`" ]]; then
    echo "This script is still running with PID `/sbin/pidof -x $(basename $0) -o %PPID`"
    exit  #exit still running
else
   echo "This script/process is not running"

   # Write your main processes/commands here to execute
   #sleep 1m  --for testing purpose
    sleep 30 #---30 second
echo "Schdule job Completed"
fi
---------------------------------------------------------

2) to check from command line ( open two terminal and run as below with 30 second)

root# cd /home/root/scripts
root@scripts# ./check_run.sh     # or root@scripts# sh -c check_run.sh

  (then only it will work. remember, if you call like root# sh check_run.sh then it will not work. you have to call it directly as cron job do, so it will work from cron job. tested)


Description of the script for newbies:-
------------------------------------
pidof                   : show the process ID of running instance.
-x                         : Tells pidof to include scripts, these are usually excluded.
$(basename $0) : this is for being replaced by the name of your script when you execute it.
-o                        : Omits a given PID
%PPID                 : PID of this very script. So when this script is run it will not count itself.




3) How to add in cron job?
===========================

a) Adding in crontab

root# crontab -e
   (add follwing line with vi editor commands) (esc --> I for insert -->esc --> :wq --> enter)  


*/5 * * * * /home/root/scripts/check_run.sh >> /home/root/scripts/check_run_logs.log 2>&1


Here I have scheduled it for every minutes to run, you can change as yours. see in cron job docs.

b) Making it executable

root# chmod +x /home/root/scripts/check_run.sh

c) You can add the script's path to PATH environmental variable in .bash_profile if you see error in log file (/home/root/scripts/check_run_logs.log) like "No such file or directory" or "command not found".

/home/root/.base_profile

PATH=$PATH:$HOME/bin:/home/root/scripts

then Log out root user and login again. now it should work.


d)  Testing-
Check cron job log like below to see running time and script name as below


root# cat /var/log/cron

e) Check your created log like below to see successful status

root# cat /home/root/scripts/check_run_logs.log

===================The End=====Cheers=================

No comments: