Rorschach
Rorschach

Reputation: 3802

.sh script run by cronjob does not send email

I have a cronjob that runs an .sh script which should make a .txt file and send it as an email.

I use sudo crontab -e

Here is the script:

send_update.sh, -rwxrwxrwx

#!/bin/sh
python /home/myusername/backend/mypy.py > update.txt
sed -i '1s/^/Subject: Daily Update /' update.txt
sendmail [email protected] < /home/myusername/backend/update.txt
echo "tester" >> tesert.txt

Interestingly, this file is properly run in that the echo does print to the file but I do not recieve an email.

If I run the file from the command line by typing sudo ./send_update.sh It does send an email.

EDIT 1

The cron file line looks like:

* * * * * cd /home/myusername/backend && ./send_update.sh

EDIT 2

tesert.txt is in the same folder as send_update.sh and it does get printed into by the cron job

EDIT 3

All files involved have permissions: -rwxrwxrwx

EDIT 4

#!/bin/sh
python py2db.py > update.txt
sed -i '1s/^/Subject: Daily Update /' update.txt
sed -i '$ d' update.txt
date >> update.txt
sendmail [email protected] < update.txt

This is the new send_update.sh file. It successfully prints the most recent minute to update.txt every minute, but it never sends the email.

EDIT 5

As suggested I changed my file to

#!/bin/sh
python py2db.py > update.txt || echo "error python" >>/tmp/error.log
sed -i '1s/^/Subject: Daily Update /' update.txt || echo "error sed1" >>/tmp/error.log
sed -i '$ d' update.txt || echo "error sed2" >>/tmp/error.log
date >> update.txt || echo "error date" >>/tmp/error.log
sendmail [email protected] < update.txt || echo "error sendmail" >>/tmp/error.log

and after it runs cat /tmp/error.log reads error sendmail

EDIT 6

After looking through the logs I have found:

sendmail: not found

I am now trying to update the path to include sendmail. If anyone can direct me on how to do that it would be much appreciated.

EDIT 7

changing sendmail to /usr/sbin/sendmail fixed the issue and the suggested error checking led me there.

Upvotes: 1

Views: 1386

Answers (1)

neb0
neb0

Reputation: 144

Add debug to your script and log errors in a file, for example :

#!/bin/sh
python py2db.py > update.txt || echo "error python" >>/tmp/error.log
sed -i '1s/^/Subject: Daily Update /' update.txt || echo "error sed1" >>/tmp/error.log
sed -i '$ d' update.txt || echo "error sed2" >>/tmp/error.log
date >> update.txt || echo "error date" >>/tmp/error.log
sendmail [email protected] < update.txt || echo "error sendmail" >>/tmp/error.log

Maybe you don't have the path for one of your command, try to do an export PATH= too.

Upvotes: 2

Related Questions