…生活與工作…個人的生活雜記….

Add items to crontab

Add items to crontab

From QNAPedia

Jump to: navigation, search

Contents

[hide]

Background

Skills required

You must be able to connect to your qnap using telnet and edit using vi.

crontab

The crontab can be used to execute tasks (scripts) based on a reoccuring schedule; by default the crontab is used, for example, to restart the webserver during the night.

To view the content of the crontab, login to your qnap using telnet and type:

crontab -l

You will see a (small) list of crontab entries.

Editing the crontab is also easy, on the console type:

crontab -e

The crontab will be shown inside a vi session. Edit the crontab as you wish.

Once you have edited the crontab, execute the following to activate it:

/etc/init.d/crond.sh restart

See Cron on Wikipedia

Method 1: /etc/config/crontab

Edit /etc/config/crontab directly, then tell the cron service to reload the crontab. These changes will persist past a reboot so be careful!

Here is an example which assumes you are ssh’d / telnet’d into your nas (as admin).

echo "1 4 * * * /share/custom/scripts/custom1.sh" >> /etc/config/crontab
echo "40 5 * * * /share/custom/scripts/custom2.sh" >> /etc/config/crontab
crontab /etc/config/crontab

Method 2: autorun.sh

Some peoples have reported this method as the working method and others have reported that this does not. Perhaps it is an outdated way to add cron jobs.

Editing the crontab directly using ‘crontab -e’ as described above is possible on the QNAPs and it will function as expected. However as soon as your QNAP is restarted, you will be surprised to learn that all your custom entries will be gone from the crontab!

To prevent the loss of your work, use Autorun.sh or AutoRunMaster and a custom script for changing the crontab:

#!/bin/sh
# location: /share/custom/scripts/contab.sh
# script name: crontab script
# purpose: add entries to the crontab, which will survive a QNAP reboot
# designed for Qnap TS-201
tmpfile=/tmp/crontab.tmp

# read crontab and remove custom entries (usually not there since after a reboot
# QNAP restores to default crontab:
crontab -l | grep -vi "custom1.sh" | grep -vi "custom2.sh" > $tmpfile

# add custom entries to crontab
echo "1 4 * * * /share/custom/scripts/custom1.sh" >> $tmpfile
echo "40 5 * * * /share/custom/scripts/custom2.sh" >> $tmpfile

#load crontab from file
crontab $tmpfile

# remove temporary file
rm $tmpfile

# restart crontab
/etc/init.d/crond.sh restart

Make sure your script is called when the qnap is started by adding it to Autorun.sh or by using AutoRunMaster

 

Method 1 bis:  /etc/config/crontab, load and restart

In a post on http://forum.qnap.com/viewtopic.php?f=144&t=32519&p=253371#p253371 Micke writes :

 

Don’t bother with that kind of script at all. Whenever you add a custom entry then follow this procedure

1. Edit /etc/config/crontab and add your custom entry. 2. Run ‘crontab /etc/config/crontab’ to load the changes. 3. Restart cron, i.e. ‘/etc/init.d/crond.sh restart’

If you follow this procedure then the changes survive a reboot (even a firmware upgrade), i.e. no need for any script to restore them.

/Mike

Tested on 3.6.0 : works fine.