Monday, April 29, 2024
 Popular · Latest · Hot · Upcoming
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 9916  / 3 Years ago, fri, august 27, 2021, 1:04:34

I have a cron job that executes a shell script like following



00 01 * * * sh /backup/script.sh


Now I need to add a functionality in cron so that if this script doesn't exist then it generates an alert to me through email using sendmail utility.



Something like



00 01 * * * find script and execute script or if find no result then email.

More From » cron

 Answers
1

I see two general solutions. One is to make cron notify you of the results of commands it runs. Specifically, the crontab(5) man page states that




cron(8) will look at MAILTO if it has any reason to send mail as a
result of running commands in ``this'' crontab. If MAILTO is
defined (and non-empty), mail is sent to the user so named.




Just put a line like the following in the head of your crontab file:



[email protected]


Alternatively, if you want a more specialized solution, you can create a script and put it in place where it is guaranteed to be found (like /bin). The script itself would check whether the actual script is present, if yes, run it, if not, send you a notification. Something along the following lines:



#!/bin/bash

myscript=/path/to/your/script
[email protected]

if [ -f "$myscript" ] ; then
exec "$myscript"
else
mail -s "Error running $myscript" $myemail <<EOF
There was an error running the script
$myscript
The script could not be found
EOF
fi


You could even make it generic:



#!/bin/bash

myscript=$1
shift 1
params=$*
[email protected]

if [ -f "$myscript" ] ; then
exec "$myscript $*"
else
mail -s "Error running $myscript" $myemail <<EOF
There was an error running the script
$myscript
The script could not be found
EOF
fi

[#29507] Saturday, August 28, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
icielight

Total Points: 158
Total Questions: 92
Total Answers: 93

Location: San Marino
Member since Thu, Jun 30, 2022
2 Years ago
icielight questions
;