find not working correctly in cron











up vote
1
down vote

favorite












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.










share|improve this question
























  • Can you view the associated log, e.g. via journalctl -r --unit=cronie.service?
    – Rastapopoulos
    yesterday










  • 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
    yesterday












  • @Rastapopoulos there are no entries in the log
    – André Stannek
    yesterday










  • @Kusalananda I've set it up with -ls for tonight. Will get back to you tomorrow.
    – André Stannek
    yesterday










  • @Kusalananda see my edit. It's getting stranger...
    – André Stannek
    17 hours ago















up vote
1
down vote

favorite












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.










share|improve this question
























  • Can you view the associated log, e.g. via journalctl -r --unit=cronie.service?
    – Rastapopoulos
    yesterday










  • 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
    yesterday












  • @Rastapopoulos there are no entries in the log
    – André Stannek
    yesterday










  • @Kusalananda I've set it up with -ls for tonight. Will get back to you tomorrow.
    – André Stannek
    yesterday










  • @Kusalananda see my edit. It's getting stranger...
    – André Stannek
    17 hours ago













up vote
1
down vote

favorite









up vote
1
down vote

favorite











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.










share|improve this question















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.







find cron






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 17 hours ago

























asked yesterday









André Stannek

4931514




4931514












  • Can you view the associated log, e.g. via journalctl -r --unit=cronie.service?
    – Rastapopoulos
    yesterday










  • 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
    yesterday












  • @Rastapopoulos there are no entries in the log
    – André Stannek
    yesterday










  • @Kusalananda I've set it up with -ls for tonight. Will get back to you tomorrow.
    – André Stannek
    yesterday










  • @Kusalananda see my edit. It's getting stranger...
    – André Stannek
    17 hours ago


















  • Can you view the associated log, e.g. via journalctl -r --unit=cronie.service?
    – Rastapopoulos
    yesterday










  • 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
    yesterday












  • @Rastapopoulos there are no entries in the log
    – André Stannek
    yesterday










  • @Kusalananda I've set it up with -ls for tonight. Will get back to you tomorrow.
    – André Stannek
    yesterday










  • @Kusalananda see my edit. It's getting stranger...
    – André Stannek
    17 hours ago
















Can you view the associated log, e.g. via journalctl -r --unit=cronie.service?
– Rastapopoulos
yesterday




Can you view the associated log, e.g. via journalctl -r --unit=cronie.service?
– Rastapopoulos
yesterday












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
yesterday






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
yesterday














@Rastapopoulos there are no entries in the log
– André Stannek
yesterday




@Rastapopoulos there are no entries in the log
– André Stannek
yesterday












@Kusalananda I've set it up with -ls for tonight. Will get back to you tomorrow.
– André Stannek
yesterday




@Kusalananda I've set it up with -ls for tonight. Will get back to you tomorrow.
– André Stannek
yesterday












@Kusalananda see my edit. It's getting stranger...
– André Stannek
17 hours ago




@Kusalananda see my edit. It's getting stranger...
– André Stannek
17 hours ago















active

oldest

votes











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f489708%2ffind-not-working-correctly-in-cron%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f489708%2ffind-not-working-correctly-in-cron%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Entries order in /etc/network/interfaces

新発田市

Grub takes very long (several minutes) to open Menu (in Multi-Boot-System)