How to correctly setup cron job notifications - cron

I'm trying to setup to receive notification everytime my cron job fails, but i'm not sure if I am doing it correctly. This is my current setup.
The second line, i'm trying to clean my csv file, while the third line, i want that clean file to be copied to google cloud storage. How do I receive a notification when any of the jobs fail?
MAILTO:myemail#gmail.com
*/1 * * * * csvcut -x -e UTF-16 myfile.csv > newfile.csv
*/6 * * * * gsutil mv -r newfile.csv gs://gcsbucket

One thing that I noticed is MAILTO:myemail#gmail.com
To set the MAILTO variable you should use:
MAILTO=username#domain.com
Sending Email Alerts Through Cron
Also your system should be able to send mails. Please check this related question on SO which describes how to configure your system.
How do I set Cron to send emails
To get an email every time a cron job fails you should redirect all of your standard output to /dev/null or to some file and the STDERR messages will be emailed.
Get email when a cron job fails

Related

Linux sendmail command not sending mail while in a cron

I'm trying to run a sendmail command in a Red-hat Linux environment on a bash shell script through a cronjob. I can run this script successfully when it is ran manually and every other job within the shell runs correctly other than the mailing part.I have never used sendmail and am not sure if I need to restructure how it is being presented.
I have tried mail and mailx. I am able to send the emails but the log file contain many weird characters that it put the text format into a att00001.bin attachment on the email which I do not want. The sendmail command seems to be the only one that doesn't send an attachment when ran manually. Other cron jobs works correctly and are able to send emails they just do not have the special characters in the log file.
echo '##################################################'
date
echo '##################################################'
#Run Script and write to log file
/comp/gfb281m.sh > /usr/local/bin/oracle/getload/getload.log 2>&1
#Send log file to developer group
(echo "Subject:GetLoad Shell"; echo; cat
/usr/local/bin/oracle/getload/getload.log) | sendmail -v
exampleEmail#outlook.com exampleEmail2#mail.mil
When ran this cron job should send the contents of the getload.log file to a group a of users.
Fixed the issue thanks to another source. I was not using the sendmail's full path. I was just stating "| sendmail -v email" and not sendmails full path which was "/usr/sbin/sendmail" for me. Not sure if links are allowed here but below is where I found the answer.
https://www.unix.com/red-hat/271632-bash-sendmail-command-not-found.html
crontab sets PATH to /usr/bin:/bin. To avoid typing absolute command names like /etc/sbin/sendmail you can set up the PATH in you crontab:
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
*/30 * * * * sendmail user#example.com%subject: Sample email%%Email body%

Cron output is emailed as a file instead of showing in the body?

I've setup crons through cPanel for a long time and the output is always emailed to me. However, I need a cron to run with root permissions and so I added something like this to crontab:
5 0,12,8,20 * * * php /path/to/file.php 2>&1 | mail -s "my cron job" my#email.com
The command executes and I receive an email, but the output is attached as a file ironically named noname.
How can I get the output to show up in the body of the email instead of in an attached file?
First can you confirm which mail program you are using and which cron program you are using ? E.g. by default most cron software restricts the PATH, unless you have it set at the top of the crontab.
/bin/mail at least from GNU mailutils doesn't support sending attachments without help from another program (uuencode). /bin/mailx from Heirloom which your system may be using supports attachments, and will happily convert mail sent in non interactive mode that contains illegal byte sequences (non ASCII), from text to an octet stream file attachment as per the Changelog
You can get the output to show up in body instead of attached file by
Changing which mail software you use
Removing any illegal byte sequences from your output before piping it to mail. See the existing StackOverflow Question Best way to convert text files between character sets? for how to do that.
I would highly recommend using mutt instead
5 0,12,8,20 * * * php /path/to/file.php 2>&1 |mutt -s "your cron job" -- my#email.com
or if you're really set on using whatever your default mail is, possibly use an intermediary file
5 0,12,8,20 * * * php /path/to/file.php 2>&1 /tmp/outfile; mail -s "your cron job" my#email.com < /tmp/outfile; rm -f /tmp/outfile

"Visiting a page" using a cron job

So I have this php script that sends an email every time someone visits it the page (opens browser, types in www.example.com/email.php and hits enter). I'm trying to find a way to trigger this on a regular basis with a cron job on a shared host. I can configure the cron to run this command: curl --dump http://www.example.com/email.php but it doesn't send the email.
I've confirmed that the php script works (by manually visiting it) and that the cron job runs (I've set it on the host's control panel to 'send email everytime cron runs'), I just can't get them to work together. Any ideas?
As long as your web script works by simply opening the page (no user interaction required) you can use wget to mimick a request:
wget -O /tmp/temp_file.html http://www.example.com/email.php
Try running the "page" (script) with php so it interprets the file directly.
crontab entry:
* * * * * php your/script/location/email.php >/dev/null 2>&1

not getting output when scheduled a jcl program in crontab

dear friends i have a jcl program that uses PCO program .I want to schedule it every minute using crontab .i have done the entry in cronntab like below
* * * * * path/job
but the crontab is not producing any output but when i am seeing /var/log/crontab it is showing that crond has executed the job every minute but i am not getting any output
Plz help how to schedule that job
I have read in internet that it can be because of environment variables not set properly
plz tell if that can be the case
Where do you expect the output to show up ? crontab
If standard output and standard error
are not redirected by commands
executed from the crontab entry, any
generated output or errors shall be
mailed, via an implementation-defined
method, to the user.
So you should redirect the output
* * * * * path/job > /path/to/file
Alternatively, check your mail (mail).

How to test a cron job?

I'm using Ubuntu Linux 10.0.4. I want to run a script every 6 hours, every day. When I issue sudo crontab -e, I see:
# m h dom mon dow command
* 00,06,12,18 * * * /opt/scripts/selenium/run_nis_inf_tests.sh
However, I'm not seeing the expected outcome from my script, and I'm not even sure if its running. Is there a way to test, short of waiting until the specified time, that the script is running properly. Or, how can I view the errors the script is generating? - Dave
You can update the MAILTO variable to your email address, and cron should email you any STDOUT and STDERR output. Also check your syslog file /var/log/messages to see if the script is being executed by cron.
-Tony
Cron should mail it results so it looks like you have a problem.
Here, it seems you are missing a user to run the script as :
00,06,12,18 * * * user_name /opt/scripts/selenium/run_nis_inf_tests.sh
replace user_name by the name of the user the script needs to be run by, verify permissions of "run_nis_inf_tests.sh" and you should be ok.

Resources