Krisduck
Posts: 4
Joined: Thu Jun 06, 2013 6:14 am

Problem with rm on cron job?

Thu Jun 06, 2013 6:47 am

Hi there.

I seem to be having issues with what should be a fairly simple exercise. I've tried searching all over and can't find a definitive answer.

I've got a file called ftpupload.sh that's run from a cron job. (Loaded through crontab -e as root) It uploads a series of files to an ftp server, then deletes them. (At least when run from command line). When run from the cron job, it uploads them, but doesn't delete them.

When I check the error mail, at the end of the mail is this:

/root/ftpupload.sh: 8: [: 0: unexpected operator

The script is as follows:

Code: Select all

#!/bin/sh
FTPU="Username" # ftp login name
FTPP="Password" # ftp password
FTPS="ftp.myserver.com" # remote ftp server
FTPF="/" # remote ftp server directory for $FTPU & $FTPP
LOCALD="*.jpg"
ncftpput -m -u $FTPU -p $FTPP $FTPS  $FTPF $LOCALD
if [ $? == 0 ]; then
    /bin/rm /root/*.jpg
fi
And the crontab entry is:

Code: Select all

0 */4 * * * /root/ftpupload.sh
I'm sure it's something simple, but I can't seem to find the right syntax. Is it a permissions thing? I think I've got the right folder structure.

ShiftPlusOne
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 6074
Joined: Fri Jul 29, 2011 5:36 pm
Location: The unfashionable end of the western spiral arm of the Galaxy

Re: Problem with rm on cron job?

Thu Jun 06, 2013 6:57 am

The problem is in the script. Pay close attention to the actual error message.

It says there is a problem in ftpuplod.sh on line 8, near the [ character. That tells you there is a syntax problem.

It has been a long while since I've done any bash scripting so the problem doesn't stand out to me, but go through this page and see if you can spot it http://tldp.org/LDP/Bash-Beginners-Guid ... 07_01.html

Krisduck
Posts: 4
Joined: Thu Jun 06, 2013 6:14 am

Re: Problem with rm on cron job?

Thu Jun 06, 2013 7:04 am

You beat me. I was just coming back to say I think I figured it out. Mess with it for two days, post my complaint and figure it out a few minutes later.

And yea, if I had actually registered that was the line number that was the problem, I would have figured it out sooner. Duh. It was the '==' that was the problem, it seems. I think only bash uses ==, and I think I was using sh, therefore it didn't like it. (Correct me if I'm wrong). I switched it to a single '=' and it seems to properly delete the files now.

Thanks for helping me along the right path. I'm pretty excited I've managed to schedule cron jobs via email, and now this works.
This whole Linux/Pi thing is pretty cool. :)

Thanks again,

Kris

User avatar
rpdom
Posts: 15577
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Problem with rm on cron job?

Thu Jun 06, 2013 7:06 am

"==" is a bash specific operator (a "bashism" as they are known). When you specifiy /bin/sh in a cron it will use a different shell and == won't work.

Either change your #! line to use bash instead of sh, or use [ $? = 0 ] or [ $? -eq 0 ] for the tests.

Return to “General programming discussion”