I'm trying to use a cronjob to download a file from a URL, but add a step to email if there is any exit status other than 0. wget is using a .wgetrc for the login credentials to pass to the URL auth. The .wgetrc is located in the same dir where the job is running from.
Here is the job
Code:
37 20 * * * /home/user1/dwnld > /tmp/dwnld.log 2>&1
Script
Code:
#!/bin/bash
export HOME=/home/user1
set -e
set -o pipefail
retval=0
cd /home/user1
retval=$(wget http://location.tar.gz)
if [ $? -ne 0 ] ; then
cat /tmp/dwnld.log | mail -s "Error Report" -r "root@vmname.local" "skagnola@domain.com"
fi
I have the credentials set incorrectly to test the error out email.
This is the out log
Code:
--2018-05-14 20:37:01-- http:location.tar.gz
Resolving location.org (location.org)... [IP ADDR]
Connecting to location.org (location.org)|[IP ADDR]|:80... connected.
HTTP request sent, awaiting response... 401 Unauthorized
Reusing existing connection to location.org:80.
HTTP request sent, awaiting response... 401 Unauthorized
Authorization failed.
Presumably the Authorization failed means EXIT STATUS 6 but its not sending the email.
When I run the mail line alone, it sends. The user running the job has write access to the output dir as well as the dir the job is running from.
If I run it with the correct credentials set, it works. I just need to set something up to notify when something blows up.
I must be doing something wrong with the way I'm presenting this to cron? Maybe perms on the script? Should it be run as a root user?