find not working correctly in cron
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 :-)
find cron
|
show 5 more comments
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 :-)
find cron
Can you view the associated log, e.g. viajournalctl -r --unit=cronie.service
?
– Rastapopoulos
Dec 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 at 17:57
@Rastapopoulos there are no entries in the log
– André Stannek
Dec 18 at 18:30
@Kusalananda I've set it up with-ls
for tonight. Will get back to you tomorrow.
– André Stannek
Dec 18 at 18:31
@Kusalananda see my edit. It's getting stranger...
– André Stannek
Dec 19 at 8:50
|
show 5 more comments
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 :-)
find cron
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 :-)
find cron
find cron
edited Dec 20 at 9:00
asked Dec 18 at 15:28
André Stannek
5131514
5131514
Can you view the associated log, e.g. viajournalctl -r --unit=cronie.service
?
– Rastapopoulos
Dec 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 at 17:57
@Rastapopoulos there are no entries in the log
– André Stannek
Dec 18 at 18:30
@Kusalananda I've set it up with-ls
for tonight. Will get back to you tomorrow.
– André Stannek
Dec 18 at 18:31
@Kusalananda see my edit. It's getting stranger...
– André Stannek
Dec 19 at 8:50
|
show 5 more comments
Can you view the associated log, e.g. viajournalctl -r --unit=cronie.service
?
– Rastapopoulos
Dec 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 at 17:57
@Rastapopoulos there are no entries in the log
– André Stannek
Dec 18 at 18:30
@Kusalananda I've set it up with-ls
for tonight. Will get back to you tomorrow.
– André Stannek
Dec 18 at 18:31
@Kusalananda see my edit. It's getting stranger...
– André Stannek
Dec 19 at 8:50
Can you view the associated log, e.g. via
journalctl -r --unit=cronie.service
?– Rastapopoulos
Dec 18 at 16:24
Can you view the associated log, e.g. via
journalctl -r --unit=cronie.service
?– Rastapopoulos
Dec 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 at 17:57
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 at 17:57
@Rastapopoulos there are no entries in the log
– André Stannek
Dec 18 at 18:30
@Rastapopoulos there are no entries in the log
– André Stannek
Dec 18 at 18:30
@Kusalananda I've set it up with
-ls
for tonight. Will get back to you tomorrow.– André Stannek
Dec 18 at 18:31
@Kusalananda I've set it up with
-ls
for tonight. Will get back to you tomorrow.– André Stannek
Dec 18 at 18:31
@Kusalananda see my edit. It's getting stranger...
– André Stannek
Dec 19 at 8:50
@Kusalananda see my edit. It's getting stranger...
– André Stannek
Dec 19 at 8:50
|
show 5 more comments
1 Answer
1
active
oldest
votes
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 at 10:20
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 at 10:20
add a comment |
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 at 10:20
add a comment |
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
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
edited Dec 20 at 10:13
answered Dec 20 at 9:54
Radek Radek
214
214
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 at 10:20
add a comment |
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 at 10:20
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 at 10:20
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 at 10:20
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Can you view the associated log, e.g. via
journalctl -r --unit=cronie.service
?– Rastapopoulos
Dec 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 at 17:57
@Rastapopoulos there are no entries in the log
– André Stannek
Dec 18 at 18:30
@Kusalananda I've set it up with
-ls
for tonight. Will get back to you tomorrow.– André Stannek
Dec 18 at 18:31
@Kusalananda see my edit. It's getting stranger...
– André Stannek
Dec 19 at 8:50