Debugging my cron application
Question
I have a code like this :
set -e
set -x
folderName=$(echo `date +%Y/%m/%d/`);
fileName=x.x.x.x.x.x.x.log
cp x.x.x.x.x.x/$1 $fileName
gzip $fileName
s3cmd put $fileName.gz s3://x.x.x.x.x/$folderName
rm $fileName.gz
This is working fine if I run like this :
./shell logfilelocation
And when I added into the crontab like this :
* * * * * /home/x.x.x/testing/s3 -f x.x.x.log >> /tmp/mys3Log
And I waited! The file mys3Log
gets created. But there is no content in it! I expect the result of command execution ( as I have used set -e ; set -x
in my code ) should go into the mys3Log
file as I'm doing a redirect there.
But something is going wrong. I'm very new to bash programming and cron.
Where I'm making the mistake?
Thanks in advance.
Solution
cron
don't have the same environment like in interactive shell, so at the beginning of the script, after the shebang, add :
source ~/.bashrc || source /etc/profile
And remove set -e
to see what's going on.
In your crontab
, to log errors & output (STDERR
, STDOUT
), you need to do :
* * * * * /home/x.x.x/testing/s3 -f x.x.x.log >> /tmp/mys3Log 2>&1
Moreover, on line 4 of your script, you are using the variable $name
thats is never declared.
Last but not least, like Janauary said, add the #!/bin/bash
shegang on the first line and ensure that your script have executable rights. : chmod +x script.sh
OTHER TIPS
As mentioned in the previous answer, the cron works in different environment than your shell. Which means that the environment variables would differ in shell. For eg:
In shell:
$ echo $PATH
/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/aws/bin
Whereas in if you run the following in cron:
* * * * * echo $PATH > path.out
you'll get /usr/bin:/bin
.
Find out where is your s3cmd executable using the command which s3cmd
and then modify the cron accordingly. Eg:
$ which s3cmd
/usr/local/bin/s3cmd
Then, cron should look like:
PATH=/usr/bin:/bin:/usr/local/bin
* * * * * echo $PATH > path.out