I got stuck with this cronjob that just won't work. I left it for a day to troubleshoot it again with some fresh idea's but still no luck.
I tried to find my answer on this great post, but not everything is clear to me and at the end it still refuses to work. And to make it all worse, there are no logs, no errors to find concerning cron in /var/log.
What do I try to achieve?
Automate the removal of directories that are populated by an ip cam. In the directories are the snapshots. Here is a view of the list.
pi@raspberrypi:/media/pi/USB/Dahua/Dahua $ ll total 24K drwxr-xr-x 3 xxxxxxxx xxxxxxxxx 4.0K Sep 2 05:59 2018-09-02d drwxr-xr-x 3 xxxxxxxx xxxxxxxxx 4.0K Sep 3 00:57 2018-09-03d drwxr-xr-x 3 xxxxxxxx xxxxxxxxx 4.0K Sep 4 02:03 2018-09-04d drwxr-xr-x 3 xxxxxxxx xxxxxxxxx 4.0K Sep 5 01:20 2018-09-05d drwxr-xr-x 3 xxxxxxxx xxxxxxxxx 4.0K Sep 6 00:20 2018-09-06 -rw-r--r-- 1 xxxxxxxx xxxxxxxxx 4.0K Sep 6 22:28 DVRWorkDirectory
I would like to keep the x newest files and get rid of the rest. All this with a cronjob that would run lets say every week or every day at a certain time.
Seems not to be difficult but I just can't make it work. It's true, my Linux knowledge is basic and will probably be the cause of my problem.
Step 1: My script, the permissions and file location.
In my research I found out that these three topics where important. So this should be valuable info.
-rwxr--r-- 1 root staff 183 Sep 6 15:22 dahuapurge.sh /usr/local/bin #!/bin/bash PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin # # #Keep the Dahua pics for max x time. cd /media/pi/USB/Dahua/Dahua/ sudo rm -rf `ls -tl | tail -n +8`
Step 2: My cronjob config.
To create the cronjob I used the command crontab -e. In the file every line is commented except my manually written rule. I know, I set it to run every hour for the moment.
# AUTHOR: - xxxx xxxx # DATE: - 31/08/2018 # DESCR: - Purge Cam pics # LINK: - # 0 * * * * /usr/local/bin/dahuapurge.sh
I might have found one of my errors or maybe THE error. But I was quite far with my post so I share it anyway.
I added the option -rf to my rm command because I try to delete directories and not files. I modified my script and it should remove 1 directory again in about 30 mins.
#!/bin/bash PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin # # #Keep the Dahua pics for max x time. cd /media/pi/USB/Dahua/Dahua/ sudo rm -rf `ls -tl | tail -n +7`
Still not working but I'll try the suggestions that where posted and update this asap.