Delete old directories using cronjob is not working in LINUX - linux

I have test this deleting directories command in LINUX terminal and it is working fine.
find /home/TEST_/ -maxdepth 0 -mtime +6 -exec rm -r {} ;
printf "deleted IPSIM old directory"
But when i set cronjob to cleanup the directories, i get error as below.
find: missing argument to `-exec'
deleted IPSIM old directory
Crontab:
00 00 * * 3 cd /home/cronjob; sh cleanup_regress_SIM.sh
Can someone help with this and correct me where I am going wrong?

The correct way to put that script command in crontab is:
00 00 * * 3 /home/cronjob/cleanup_regress_SIM.sh
In more detail:
You do not need to use cd, just specify the full path to the bash script.
#!/bin/sh or #!/bin/bash is already defined in the beginning of the script so it will run in the correct environment. No need to use sh
I have tested a copy of this on my own system and it works fine. I don't know what kind of system you run that throws these errors. Here is what works.
test.sh (slightly different)
#!/bin/bash
echo $(find /home/gerge/Documents/Arduino/wifi* -maxdepth 0 -mtime +30 -exec ls {} \;)>>/home/gerge/test.log
echo "command done">>/home/gerge/test.log
crontab (run every minute for testing)
*/1 * * * * /home/gerge/test.sh
The content of test.log
wifiConfigPortal.ino wifiRelayLogin.ino wifiRGB.ino wifiRGBsimple.ino
command done
wifiConfigPortal.ino wifiRelayLogin.ino wifiRGB.ino wifiRGBsimple.ino
command done
wifiConfigPortal.ino wifiRelayLogin.ino wifiRGB.ino wifiRGBsimple.ino
command done
I would recommend checking if other scripts are able to run as cronjobs. If you get the same error there is some bigger issue.

Related

Command running on terminal but not on crontab

I'm trying to run the following command on crontab but for some reason it cuts of a portion of the command. when I check /var/logs/cron. However, it runs when I run it on the terminal.
Command in crontab:
*/30 * * * * user find /home/user/recordings -name '*.pcap,SDPerr' -exec sh -c 'mv "$0" "${0%.pcap,SDPerr}.pcap"' {} \;
from /var/logs/cron:
Jan 10 11:00:01 server CROND[116349]: (user) CMD ( find /home/user/recordings -name '*.pcap,SDPerr' -exec sh -c 'mv "$0" "${0)
What am I missing here, any help would be appreciated.
Your command has a % (percent sign) in it, which has special meaning in crontab. Therefore better to put "\" before "%" to escape it.

Will missed shebang stop the script for running

I have a crontab job to purge the logs from /var/log/nginx folder. The crontab was set up like this:
15 23 * * * /scripts/logcleanup.sh > /dev/null 2>&1
The logcleanup.sh script is very simple, it only has two line:
find /var/log/nginx -mtime +5 -type f -delete;
find /var/log/nginx -size +50M -type f -delete;
I supposed the script will be run every night at 23:15. However, it doesn't get executed and the files larger than 50 MB are still inside the log folder. Is this caused by the missing Shebang "#!/usr/bin/env bash" ?
Thanks.
Redirect the output to a log file instead of /dev/null to get feedback:
15 23 * * * /scripts/logcleanup.sh > ~/logcleanup.sh.log 2>&1
All the errors will be logged and hopefully, you will find out what is wrong.

Schedule cronjob to remove files modified before X days

I want to create and schedule cronjob to remove files modified before x days
I have taken following steps for that
Created Shell script (Named: Script.sh) as bellow
#!/bin/sh
15 2 * * 2-6 find /usr/sch/cbm/files/newui/log -type f -mtime +2 exec rm {} \;
I have put this file in "/var/spool/cron/crontabs" and usr/bin folder because i wasn't sure where to place exactly.
When i check with Crontab -e command get as in bellow image
But i didn't found any effect on my files. I am not sure is my job scheduled or i required to do anything else still
please guide me
crontab <file>
where file is what you have above will put the file in the right place and make sure everything is happy for cron to run it.
crontab -l
will display the list of cron jobs for the currently logged in user.
Note that the file itself does not execute so no need for #!/bin/sh as the first line. It is just a data file that cron interprets.
man 5 crontab
and
man 1 crontab
for more information.
You have to insert cron job line in cron file of user who executes that job.
su - username
crontab -e
add cron job line
save and exit
Example for removing files before x days
00 00 * * * find /path/to/folder -mtime +x -exec rm {} \;
or you can do that for some files exist in folder
For example remove files end with .log
00 00 * * * find /path/to/folder/*.log -mtime +x -exec rm {} \;

Bash script not deleting files in given directory

I found this bash script online that I want to use to delete files older than 2 days:
#!/bin/bash
find /path/to/dir -type f -mtime +2 -exec rm {} \;
I setup a cronjob to run the script (I set it a couple of minutes ahead for testing, but it should run once every 24 hours)
54 18 * * * /path/to/another/dir/script.sh
I exit correct so it updates the cronjob.
Why does it not delete the files in the directory?
What if you try dumping an echo at the end of the script and log the output
cron1.sh >> /var/log/cron1.log
You could try this but I'm not sure it will work
--exec rm -rf {}
Most cron jobs do not have PATH set. You must fully qualify the find command.
#!/bin/bash
/usr/bin/find /path/to/dir -type f -mtime +2 -exec rm {} \;
If you capture the stdout and stderr as recommended by damienfrancois, you'd probably see the message "command not found: find". If you didn't capture the stdout and stderr, cron usually will send the output to the cron job owner's email, unless configured not to do so.

Delete the oldest five files with cron or launchctl

I'm currently backing up a project into a tarball. I'd only like to keep the five most recent backups. Currently, the script reads:
tar -cjf $HOME/projects/foo.$(date +%Y%m%d%H%M%S).tar.bz2 $HOME/projects/foo > /dev/null 2>&1
find $HOME/projects -maxdepth 1 -name "foo*.tar.bz2" | ghead -n -5 | xargs rm > /dev/null 2>&1
# CR and blank line
The tarballs are created, but the old ones are never removed. Odd thing is, when I copy and paste the second line into a shell, the files are deleted as expected. The script functions as expected when called manually via the command line. Is the script not reaching the second line, or are there some rules about running commands via cron I'm not aware of?
Mac OS X 10.8 with gnu-coreutils. Attempted under cron and using launch services, with the same results.
The following script now works when called from cron or an equivalent. Thanks to #ansh0l for the clue.
PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin
cd $HOME/projects
tar -cjf foo.$(date +%Y%m%d%H%M%S).tar.bz2 foo
find ./ -maxdepth 1 -name "foo*.tar.bz2" | ghead -n -5 | xargs rm

Resources