How can I regularly delete certain files in cron?
I'm trying to delete empty files older than 15 minutes in the PHP sessions directory. I've tried several methods, all failed with different error messages.
4-59/10 * * * * root [ -d /var/lib/php/sessions ] && find /var/lib/php/sessions/ -type f -cmin +15 -size 0c -print0 | xargs -n 200 -r -0 rm
xargs doesn't find 'rm'. It also won't find '/bin/rm'.
4-59/10 * * * * root [ -d /var/lib/php/sessions ] && find /var/lib/php/sessions/ -type f -cmin +15 -size 0c -exec rm '{}'
Missing argument for -exec option.
4-59/10 * * * * root [ -d /var/lib/php/sessions ] && find /var/lib/php/sessions/ -type f -cmin +15 -size 0c -delete
-delete option is unknown, find must be old.
Can Ubuntu 16.04 still do that? The first version mentioned worked fine on 14.04. Maybe I have to create a separate shell script file and simply write its name into the cron config if that's what cron can do. All the above commands work fine in an interactive bash shell (maybe except the last). Just not in cron.
To avoid dealing with
cron
's command line syntax, don't put the command in thecrontab
. Write a simplebash
wrapper around the command, and call the script from thecrontab
. This wrapper will let you set up the environment ($PATH
), I/O redirection, error handling, ...Also, I note that you are using the
crontab
format used inroot
's special (/etc/crontab
,/etc/cron.d/
,/etc/cron.daily/
,/etc/cron.hourly/
,/etc/cron.monthly/
,/etc/cron.weekly/
) setup. Is this your intent? Or are you usingsudo crontab
? I disrecommend that.You have:
You need:
You have:
You need an escaped semicolon ("
\;
") to terminate the-exec
:"
find
must be old"? Doesfind --version
look like:What version of Ubuntu are you running?
find ... -delete
has been around for a long time. Are youBusyBox
?Here is a script that I use to illustrate some
cron
issues:(My environment uses
$HOME/bin
and$HOME/var/log
):In $HOME/bin/recordenv:
To use it, call
$HOME/bin/recordenv cron
fromcrontab
.