Batch / Script for daily file admin

Questions and mods regarding system management may go here
Forum rules
Please note the disclaimer before modifying your Synology Product.

Batch / Script for daily file admin

Postby Jongky » Fri Mar 09, 2012 3:15 am

Hi,

I have the NAS DS211J. I have a directory/folder which I would like to maintain by deleting files that are older than a month. Instead of running a batch / script from the computer, is there a way I can run a batch / script on the NAS itself to do this check on a daily basis ?

Thanks.
Jongky
Trainee
Trainee
 
Posts: 11
Joined: Tue Oct 04, 2011 5:42 am

Re: Batch / Script for daily file admin

Postby maxxfi » Fri Mar 09, 2012 10:13 am

Probably "cron" is the answer.
You can write the script on the NAS, and tell cron with what periodicity to run it.
DS-411 (DSM 4.2-3202) w/ 2x WD10EFRX + 1x HD154UI
DS-106j (DSM 3.0-1357), PATA-to-SATA adapter, 2.5" HM250HI
User avatar
maxxfi
Architect
Architect
 
Posts: 4317
Joined: Sun Dec 27, 2009 12:13 pm
Location: Espoo, Finland

Re: Batch / Script for daily file admin

Postby Jongky » Sun Mar 25, 2012 3:14 pm

Thanks Maxxfi... :)

One more question. How should I write the script into the crontab file if I want to delete files older than 60 days ?
Jongky
Trainee
Trainee
 
Posts: 11
Joined: Tue Oct 04, 2011 5:42 am

Re: Batch / Script for daily file admin

Postby maxxfi » Sun Mar 25, 2012 4:48 pm

I would e.g. create a script /usr/local/cleanoldfiles.sh like this:
Code: Select all
#!/bin/sh

TOPLEVEL=<replace-here-with-top-directory-where-to-delete-files>
MAXDAYS=60

# uncomment only 1st line to test, only second one to remove files
# find $TOPLEVEL -type f -mtime +$MAXDAYS -ls >/tmp/cleanold.log
# find $TOPLEVEL -type f -mtime +$MAXDAYS -exec rm {} \;

Set TOPLEVEL variable and initially, uncomment only the first 'find' line, the one with '-ls' so you can test that it behaves correctly.
Run the command "chmod +x /usr/local/cleanoldfiles.sh" to make it executable

Then, edit /etc/crontab and add a line like the following to run the script e.g. every night at 03:15
15 03 * * * root /usr/local/bin/cleanoldfiles.sh

Finally, run "/usr/syno/etc/rc.d/S04crond.sh stop" and "/usr/syno/etc/rc.d/S04crond.sh start" to restart the cron daemon (to activate your changes in /etc/crontab)

Test to see if it seems to work properly, by running the script and checking in /tmp/cleanold.log that the correct files are candidates for deletion, and if you are happy edit the script again, and uncomment the 'find' line with the '-exec rm'.

That should be all. :)
DS-411 (DSM 4.2-3202) w/ 2x WD10EFRX + 1x HD154UI
DS-106j (DSM 3.0-1357), PATA-to-SATA adapter, 2.5" HM250HI
User avatar
maxxfi
Architect
Architect
 
Posts: 4317
Joined: Sun Dec 27, 2009 12:13 pm
Location: Espoo, Finland

Re: Batch / Script for daily file admin

Postby Jongky » Mon Mar 26, 2012 4:23 am

Thanks Maxxfi... I'll try this out ... :D
Jongky
Trainee
Trainee
 
Posts: 11
Joined: Tue Oct 04, 2011 5:42 am


Return to System Managment Mods

Who is online

Users browsing this forum: No registered users and 2 guests