Page 1 of 1

Problem with rm on cron job?

Posted: Thu Jun 06, 2013 6:47 am
by Krisduck
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 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/ 8: [: 0: unexpected operator

The script is as follows:

Code: Select all

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

Code: Select all

0 */4 * * * /root/
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.

Re: Problem with rm on cron job?

Posted: Thu Jun 06, 2013 6:57 am
by ShiftPlusOne
The problem is in the script. Pay close attention to the actual error message.

It says there is a problem in 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 ... 07_01.html

Re: Problem with rm on cron job?

Posted: Thu Jun 06, 2013 7:04 am
by Krisduck
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,


Re: Problem with rm on cron job?

Posted: Thu Jun 06, 2013 7:06 am
by rpdom
"==" 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.