I have two scripts, the aim of these two scripts is, to check whether a particular script is running or not, if it wont runs, then throw a mail.
How i Achieved this output is, I wrote first script.
I created an infinite while loop which performs below steps
1. It creates a touch file
2. Triggers the script which needs to be monitored if its working or not.
3. Removes the touch file.
If the second step fails, then the remove file command will not happen and the script will stuck there itself.
I created an another script which checks the creation time of the touch file and if it is more than ten minutes, it means the second step in the first script is hanged, which also means that particular script is not working.
So if the creation time is more than 2 minutes the second script will throw a mail.
Below are the two scripts.
Code:
#!/bin/ksh userid="chansd" filename="/apps/log/check.txt" while true ;do touch $filename pass=`/apps/eDMZ/call_st.ksh $userid` sleep 20 rm $filename done
Below script checks the file creation time and throws email if it is older than 2 minutes
Code:
#!/bin/ksh filename="/apps/log/check.txt" if [ -f "${filename}" ] then if test "`find $filename -mmin +2`" then echo "script is not working ! Please act on it" | mail -s "Script is not working" Example@mail.com fi else exit 1 fi
What im going to do is
1. I am going to run the first script in background so it runs forever.
2. I am going to run the second script in cron forevry 5 mins to check the file creation time.
3. So if the first script hangs . I will kill the process using process id and after the issue resolves with the inner script, I will run the main script again.
I am new to Linux, Please let me know if this approach will work as expected.