What's new

Cron job to move files created yesterday

  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

perla

New Around Here
My IP camera can only upload images to the same directory (/mnt/NAS/IPC_FTP/) using FTP. I would like to organize these snapshots by date, so I wrote the following script to automatically move all images from yesterday to a new directory (for example to /mnt/NAS/IPC_FTP/2015-07-14/):

Code:
#!/bin/sh

DIR="/mnt/NAS/IPC_FTP/"$(date -d @$(( $(date +"%s") - 86400)) +"%Y-%m-%d")"/"
NAME=".*"$(date -d @$(( $(date +"%s") - 86400)) +"%Y%m%d")".*"

mkdir $DIR

find /mnt/NAS/IPC_FTP/ -type f -regex $NAME -exec mv {} $DIR \;

A date field is a part of the filename, so it was easy the find all images uploaded on yesterday.

A cron job is added to services-start to schedule the script:

Code:
#!/bin/sh

cru a ipc_ftp "1 4 * * * /jffs/scripts/ipc_ftp.sh"

sleep 10
/opt/etc/init.d/rc.unslung start

However, it only works if I run the script manually from the console...
I can see in the router's log that the cron job runs the script at the specified time, but it has no effect. What can be the problem? Has the cron job had the necessary permissions to move files?

Thank you very for the help in advance!
 
An update:
By redirecting the cron job's output:
Code:
cru a ipc_ftp "1 4 * * * /jffs/scripts/ipc_ftp.sh > /var/log/cron.log 2>&1"

I get the following error in the log file for the find command:
unrecognized: -type

I have tried to remove that parameter, but now I get:
unrecognized: -regex

It is very strange, because like I said by executing the script from the console manually it runs with no problem...
 
Do you have entware/optware installed? The base find/busybox does not support the type and regex options. Maybe when you running from the command line you are picking up the other 'ware version?
 
As john9527 points out, you're picking up the wrong version of find. The cron environment is not the same as the interactive shell environment, so you need to add a PATH statement to your script that includes your optware/entware binaries.
 
If you're like ppl said above and have optware/entware/etc installed, replace "find" with "/opt/bin/find" in your script. That is one simple fix to your error.

asuswrt has a hard coded default PATH/environment for non-interactive shells. Seems no more polish for a long time.
 
Thank you everyone for the help!
Yes, I have entware installed, so that "caused" my problem.

However, for my simple task the "dumber" find command is more than enough. I finally used the -name function:
Code:
#!/bin/sh

DIR="/mnt/NAS/IPC_FTP/"$(date -d @$(( $(date +"%s") - 86400)) +"%Y-%m-%d")"/"
NAME="*"$(date -d @$(( $(date +"%s") - 86400)) +"%Y%m%d")"*"

mkdir $DIR

find /mnt/NAS/IPC_FTP/ -name "$NAME" -exec mv {} $DIR \;

I have learned something, so thanks again!
 

Similar threads

Latest threads

Support SNBForums w/ Amazon

If you'd like to support SNBForums, just use this link and buy anything on Amazon. Thanks!

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Top