Empty email from cron job

Hi All

I had this script running just fine on my old FreePBX server - however there is something odd happening running this from cron. I need to check that the correct handover of the queue has completed correctly, as I have users with fat fingers logging into the queue!

When running this manually, the email is sent with the contents of queue.txt.

When running from cron, the email is sent but is empty. Any ideas because I am stumped!

# script to email status of On Call Queue every hour
# at quarter past the hour
cd /etc/
NOW=$(date +"%m-%d-%y %H-%M")
asterisk -rx "queue show 700" > /etc/queue.txt
mail -r "[email protected] (XXXXXXXX FreePBX)" -s "On Call Queue Status $NOW" [email protected] < /etc/queue.txt
rm /etc/queue.txt

Likely permissions. Try running it manually with sudo -u asterisk or whatever user the cron runs as and see what it does.

You probably need to use a full path to asterisk, or $(which asterisk)

Unless you have write permissions to the /etc directory as the specific cron user, this script will fail.

Also, use full pathnames on the script, and make sure the script is executable.

Thanks Guys

It’s running from root crontab.

The script executes fine when run from the CLI.

cronies runs i’jnfer /bin/sh not /bin/bash , the PATH eeeds to cover all binaries called, or make them specific.

NOW=$(date +"%m-%d-%y %H-%M")
/usr/sbin/asterisk -rx "queue show 700" > queue.txt
mail -r "[email protected] (XXXXXXXX FreePBX)" -s "On Call Queue Status $NOW" [email protected] < queue.txt
rm queue.txt

Sorted - added the full path to asterisk - thanks for the tips everyone!

If it was me, I’d add a “sleep 5” right before the “rm queue.txt”.

It will gave the mail command enough time to make sure it has copied the entire file.

Also, including the full path name on your mail command wouldn’t hurt you.

1 Like