3

We have a regular job on a server that creates some files in /tmp and doesn't delete them after it is done. For reasons I don't want to get into, we can't modify the job to delete the files. So I was thinking to create a cronjob that deletes those files regularly. To not interfere with running jobs, it should delete only files that are older than a day. I came up with the following command:

find /tmp/myprefix* -mtime +1 -delete

This works just fine if I test it in a terminal so I scheduled it with cron:

0 1 * * * find /tmp/myprefix* -mtime +1 -delete

Now if this runs at 1 AM, it seems to ignore the -mtime parameter and deletes all files starting with myprefix, thus interfering with the running job.

Does anyone have an idea why this is happening?

As a remark: Since the job only runs at night, my tests where all performed while no jobs where running. I just checked that the files from last nights finished job remain. Maybe that's the reason and the modifiy time of the file is set in a strange way while the file is still written on?

I know the obvious solution would be to schedule the cleanup during the day but I'm still interested in the cause of my problem.

EDIT

According to Kusalanandas suggestion I changed the cron entry for last night to:

0 1 * * * find /tmp/myprefix* -mtime +1 -ls > /tmp/find.out

The file /tmp/find.out is empty this morning. This is expected behaviour since there were no files old enough. But according to past observations, if I had run this with -delete the "too young" files would have been deleted.

EDIT 2

After the last test I changed the command for the next night to

0 1 * * * find /tmp/myprefix* -mtime +2 -delete

With the +2 I was expecting, that none of the files created the night before would be deleted. While those files actually did remain, the files from this night were deleted. Now I'm sure that the -mtime check doesn't behave as expected if the file is still written. It remains a mistery, why the -ls didn't output anything the night before. Maybe I'll try another run with -exec and ls for possibly more detail. But I just have one more try before my 2 weeks of holydays :-)

  • Can you view the associated log, e.g. via journalctl -r --unit=cronie.service? – Rastapopoulos Dec 18 '18 at 16:24
  • As a way of debugging this, make it not delete the files and use -ls in place of -delete. Then investigate the output (this would be mailed to the owner of the cronjob if the system is sanely configured) and compare the modification timestamp against the time the job was run. – Kusalananda Dec 18 '18 at 17:57
  • @Rastapopoulos there are no entries in the log – André Stannek Dec 18 '18 at 18:30
  • @Kusalananda I've set it up with -ls for tonight. Will get back to you tomorrow. – André Stannek Dec 18 '18 at 18:31
  • @Kusalananda see my edit. It's getting stranger... – André Stannek Dec 19 '18 at 8:50
0

Cron entries only look like another bash line, but are not. Try put your find syntax to separate script and then execute, e.g.:

echo "find /tmp/myprefix* -mtime +1 -delete" > /my/path/cronscript.sh chmod 755 /my/path/cronscript.sh and edit cron line to: 0 1 * * * /my/path/cronscript.sh Some chown / chgrp adjustments can be require as well. Also, if you work under /etc/crontab file, additional username information will be needed: 0 1 * * * username /my/path/cronscript.sh

  • There's, AFAIK, nothing in the cron job specification of this job that would make it not run as expected. For example, there are no % in the command line, and it's a simple one-command job. – Kusalananda Dec 20 '18 at 10:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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