Centos 7 BASH find command not working from cron
Hi all
I'm using this command to find .sql.7z files on a network share and delete files older than three days: Code:
find /mnt/voiceArchive/*.sql.7z -type f -mtime +4 -exec rm -rf {} \; However, if I run the same above command from a crontab, it does not work. Find returns: Code:
find: ‘/mnt/voiceArchive/*.sql.7z’: No such file or directory Since I am using an absolute path, why can't find see network shares if run from cron? I've established that the share is mounted at the time the crontab runs by doing a df -mh and piping the results to a file. How can I get BASH find, when run from a crontab, to "see into" mounted network shares? If I move my test data off a network share onto a "real" directory in the local filing system, find works just fine in trimming files older than 3 days, when called from a crontab. Any help or suggestions appreciated! Thanks |
Having the "*" in your path relies on the wildcard expansion. What happens if you create a small script:
Code:
#!/bin/bash Is there any reason you're using -exec rm -rf {} \; rather than -delete To be honest, the more elegant: Code:
find /mnt/voiceArchive/ -name "*.sql.7z" -type f -mtime +4 -delete |
Quote:
are you sure you want to use Code:
rm -rf (not actually sure what happens when using '-r' on files, not folders) |
Quote:
If I try the first suggestion, I get the behaviour that nothing exists (exactly the failure mode cron exhibits with my original script): Code:
/bin/find: ‘/mnt/voiceArchive/*.sql.7z’: No such file or directory I have tried -delete (the second statement you suggest) - only difference is it now fails silently instead of emitting an error as it does when "exec rm -rf {} \;" is used... i. e. it fails apparently for the same reason (trying to "find" on a NFS network share) in exactly the same way. Files weeks old still stick in the network folder. I find it very strange that it appears true to state that find fails completely on any date-related processing when the filesystem it accesses to check file dates on is mounted via NFS... the NFS mounted filesystem is ext4 (just like the local physically mounted filesystem.) The moment any of the two above statements is run via cron on a locally mounted ext4 filesystem (physically connected to the motherboard, not mounted via NFS / network) it works perfectly. Thanks for the assistance - any other pointers? I think I'll have to give up on find or BASH in total and write a C application or a JAVA app to do this - it seems find is incapable of finding files older than 3 days if you specify a NFS share to be searched. |
As you explained in your first post .sql.7z are filenames, not directories - the syntax you tried is incorrect. Instead try this:
first try to use find only, without delete: Code:
find <dir> -type -f -name <pattern> You cannot use: find <file> ..... (also seem man find about that) If this worked for you you can specify time, add -mtime 4 or whatever you need and check if that prints really what you need. Finally if you want to remove those files add -delete. Do not use rm -rf because it will silently delete anything found (and since you did not specify any filter on filenames it will delete every file without questions). |
Hi Pan64
Thx for replying! I tried what you recommended, but I still have exactly the same problem. E. g. find works correctly, finding files older than 4 days. It then deletes them if I use -delete But this ONLY works if I execute the script containing the find command from a terminal session, manually, by hand, on an NFS mounted share that contains the files. The moment I test or try to run this in a crontab, on an NFS mounted share that contains the files, both the "just list the files" test script and the "actual delete" test script using the find command fails - but ONLY if I try to run it on a NFS mounted share. If I run the script on a local directory, it works fine in both cron and the terminal. The find command itself successfully finds all files it is supposed to delete. If I run the script on an NFS mounted directory, it works only in a terminal session, and never in cron. The find command itself fails to find any files to delete. Other BASH commands (like a simple ls with a pattern) work in both cron and terminal. What gives? Environment variables - what environment variable(s) does find require to be able to list files on NFS shares? |
Quote:
Please post the exact find command you are using in the interactive shell (terminal) including the exact options. My guess is that you are putting find something in cron instead of using the full path like with /bin/find something |
http://www.linuxquestions.org/questi...gs-4175464257/
yes, please post the real commands you tried, what's happened, what did you wanted to achieve exactly and how do you know that did not work? |
Code:
/bin/find /mnt/voiceArchive/ -iname '*.sql.7z' -type f -mtime +4 -delete |
Hi guys
Thanks for replying. Ok, here's it all in detail. All of this is on a Centos 7 box (System A) working on an NFS share /mnt/voiceArchive, shared from a Centos 6.5 box (System B) System A is where the data is generated (MySQL server), to be backed up to System B (the archive machine) System A Code:
df -mh Code:
cat /etc/exports Code:
SHELL=/bin/bash Code:
#!/bin/bash Code:
find $BACKUPDUMPPATH -name "*.sql.7z" -type f -mtime +4 -delete >> $LOGDETAILPATH 2>&1 Code:
# bash -f /root/backup_mysql_to_voice_archive_disk_xtrabackup.sh Code:
SHELL=/bin/bash If put in a crontab, as shown above, and the target files referenced by the script /root/backup_mysql_to_voice_archive_disk_xtrabackup.sh in the lines Code:
find $BACKUPDUMPPATH -name "*.sql.7z" -type f -mtime +4 -delete >> $LOGDETAILPATH 2>&1 So the factor that breaks the above two bash find statements -appears- to be that find in Centos 7 on my system A, cannot find files by date on the NFS share shared from my system B. This is ludicrous and cannot be, so I'm doing something wrong. But what? The error determination procedure is that if the crontab is left to run over a few weeks, files accumulate on the NFS share that are days and weeks older than four days in the past. Files older than four days are never deleted - e. g. the find commands fail to find files older than four days on the NFS share, and fails to delete them. Again, thanks for your time, if you need more specific info please advise I'm happy to provide it. Regards |
(edit : never mind)
|
You might insert these as the second and third lines, to aid in the debugging of the script.
Code:
set -e |
Hi Turbocapitalist
Ok, I'll insert those for tonight's run and see what pops up. Thank you :) |
date is ok on your hosts?
|
I think so:
System A (Centos 7) Code:
# date Code:
# date Code:
# ls -l --full-time Code:
-rwxr-xr-x 1 root root 330491013 2017-01-20 18:12:16.815201573 +0200 verdi_ho_server_db_bak_2017-01-20.sql.7z Code:
find /mnt/voiceArchive/verdi_ho_server_mysql_bak -name "*.sql.7z" -type f -mtime +4 -delete Thx again. |
All times are GMT -5. The time now is 06:31 PM. |