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")'`
user38537
  • 203
  • 1
  • 5

2 Answers2

3

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

hildred
  • 916
  • 1
  • 6
  • 21
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).

n.st
  • 526
  • 4
  • 11