5

I have directories in my home folder that hold reports uploaded via FTP. The daily folders are named like YYYYMMDD. At 00:01, I want to delete the folder from four days ago. At 01:01, I want to change the access to 755 on the previous day's folder.

The statements work when entered on the command-line, but not from root's cron. I tried using find, but I got the same behavior. The single line python statement is preferable because I don't need to scan the entire folder; this is on a model b, so using find is slow, and unnecessary since I know exactly what I want to work with.

rm -rf /home/me/reports/`/usr/bin/python -c 'import datetime; print (datetime.datetime.now()+datetime.timedelta(days=-4)).strftime("%Y%m%d")'`
chmod -R 755 /home/me/reports/`python -c 'import datetime; print (datetime.datetime.now()+datetime.timedelta(days=-1)).strftime("%Y%m%d")'`

These are the lines from root's crontab:

1 0 * * *     rm -rf /home/me/reports/`/usr/bin/python -c 'import datetime; print (datetime.datetime.now()+datetime.timedelta(days=-4)).strftime("%Y%m%d")'`
1 1 * * *     chmod -R 700 /home/me/reports/`/usr/bin/python -c 'import datetime; print (datetime.datetime.now()+datetime.timedelta(days=-1)).strftime("%Y%m%d")'`
3
  • Does your crontab file has an empty line at the end? (You want it to. Each line in the crontab needs to end with a newline. So there should always be an empty line at the end.) if it does and it still won't run, what's the result of running sudo crontab -l | grep -v '#') Commented Jun 27, 2015 at 23:09
  • Yeah it ends with a newline. The lines I posted above are the output of sudo crontab -l | egrep -v '^#'
    – user38537
    Commented Jun 27, 2015 at 23:16
  • You seem to have a typo in either chmod 755 or chmod 700. Also, since you're using chmod -R, you're marking all your reports as executable scripts. Perhaps you want chmod -R go-rw (revoke group's and others' read and write permissions for the directory and everything in it) instead?
    – n.st
    Commented Jun 28, 2015 at 9:09

2 Answers 2

3

Cron may not support backtick command expansion. put your commands in a script and run that from cron instead. much easier to debug.

3
  • Yeah I didn't get back ticks to work. I put everything in a script and it worked fine. I was just curious about why it didn't work as I had it.
    – user38537
    Commented Jun 28, 2015 at 8:55
  • @user38537 The backticks probably weren't a problem; the % signs definitely were. Of course, moving the commands out of the crontab and into a script removes the special meaning of % and thereby fixes your problem.
    – n.st
    Commented Jun 28, 2015 at 9:17
  • @hildred I just got around to actually testing this: The cron on my Raspberry (version 3.0pl1-124 from the default Raspbian image) supports both backticks and $().
    – n.st
    Commented Jun 28, 2015 at 13:40
3

% signs have a special meaning in crontabs and need to be escaped as \% -- here's the relevant part from man 5 crontab:

Percent-signs (%) in the command, unless escaped with backslash (\), will be changed into newline characters, and all data after the first % will be sent to the command as standard input.


Also note that the $(…) notation should be preferred over backticks.

And depending on the version of date installed on your system, you might also be able to get rid of the Python script and just use date -d "now - 4 days" +%Y%m%d (again escaping the % signs for the crontab, of course).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.