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.
According to Kusalanandas suggestion I changed the cron entry for last night to:
0 1 * * * find /tmp/myprefix* -mtime +1 -ls > /tmp/find.out
/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.
After the last test I changed the command for the next night to
0 1 * * * find /tmp/myprefix* -mtime +2 -delete
+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
ls for possibly more detail. But I just have one more try before my 2 weeks of holydays :-)