1

i need some advice as i am being driven crazy by Cron on my Fedora 8 box.

i have a basic script that is stored in

/tmp/myscript.sh

Here is a copy of the code within that script.

#!/bin/bash

AFolderName="archivefolder"
AFileName="archive.zip"

echo "setting variables"

if [ -d $AFolderName ]
then
rm -r $AFolderName
fi

echo "checked if folder exists"
mkdir $AFolderName
cd $AFolderName

echo "about to zip"
zip -r $(date +%Y%m%d)_$AFileName /var/www/*

echo "zipped"

echo "about to s3"
s3cmd put $(date +%Y%m%d)_$AFileName s3://mytestbucket

echo "s3ed"

cd ..
rm -r $AFolderName

echo "done"

i have done crontab -e and added a job according to the formatting for it, and the job appears to run as i get a log file in the temp folder. however there is nothing in the logfile and nothing gets uploaded to amazon...

here is whats in crontab

*/10 * * * * root /tmp/myscript.sh > /tmp/ms.log

what am i doing wrong? there is no hint anywhere to what the problem is. by the way the script HAS to run as root.

1
  • Also, might want to remove your script from /tmp, just in case Jul 20, 2010 at 15:59

3 Answers 3

1

Agree that you need to login as root or $ sudo su to really become root, then do your # crontab -e and take the "root" out of your crontab line -- as it looks like you found out it doesn't like.

Then you can run your script as root -- pretty much as you already had it.

0

Are you sure that the file has the execute flag flipped, (ls -l /tmp/myscript.sh) and that /tmp isn't mounted noexecute? (mount) The empty log file will be created by the shell, simply due to the redirect. But that doesn't mean the script is actually running. And since you didn't redirect error output, you'll never see that message. (Although it was probably mailed to the root user?)

Besides the above, some things to try:
A) Run the script manually, from the command line. Make sure it actually does something.
B) You may want to import in a profile file at the start of the script. Otherwise, you can wind up missing path and environment variables your script may need.
C) Redirect error output from the cron job. Either to the same file, with 2>&1 at the end, or to a separate file with 2> diff_file.txt. This will probably give you some insight as to what went wrong.


--Christopher Karel

2
  • Hi Christopher, thanks for the reply and suggestions i was messing about with this script whilst waiting for an answer on here. turns out the cron job does not like the ROOT option.. i had to add SUDO to the beginning of the S3CMD line to get it working...
    – Kristiaan
    Jul 20, 2010 at 14:33
  • Hah, I didn't even notice that. That'll certainly cause problems. You could put this into root's crontab, and avoid the need for sudo. That would make the whole script run as root. Jul 20, 2010 at 15:37
0

correct way to-do this was

Here is a copy of the code within that script sudo was needed on the s3cmd line.

#!/bin/bash

AFolderName="archivefolder"
AFileName="archive.zip"

echo "setting variables"

if [ -d $AFolderName ]
then
rm -r $AFolderName
fi

echo "checked if folder exists"
mkdir $AFolderName
cd $AFolderName

echo "about to zip"
zip -r $(date +%Y%m%d)_$AFileName /var/www/*

echo "zipped"

echo "about to s3"
sudo s3cmd put $(date +%Y%m%d)_$AFileName s3://mytestbucket

echo "s3ed"

cd ..
rm -r $AFolderName

echo "done"

crontab needs to have ROOT removed.

*/10 * * * * /tmp/myscript.sh > /tmp/ms.log

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .