Add items to crontab: Difference between revisions

From QNAPedia
Jump to navigation Jump to search
m (catchg)
No edit summary
Line 1: Line 1:
== Background ==
== Background ==


==== Skills required ====
==== Skills required ====


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


==== crontab ====
==== 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.  
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:  
To view the content of the crontab, login to your qnap using telnet and type:
<pre>crontab -l
<pre>crontab -l
</pre>  
</pre>
You will see a (small) list of crontab entries.


Editing the crontab is also easy, on the console type:
You will see a (small) list of crontab entries.
<pre>crontab -e</pre>
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:  
Editing the crontab is also easy, on the console type:
<pre>/etc/init.d/crond.sh restart</pre>  
<pre>crontab -e</pre>
See [http://en.wikipedia.org/wiki/Cron Cron on Wikipedia]


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


When using crontab -l on a default QNAP, QNAPs has some default crontab jobs.  
Once you have edited the crontab, execute the following to activate it:
<pre>/etc/init.d/crond.sh restart</pre>


Usually you will want to know, what default jobs are running on the QNAP based on schedule.<br>
See [http://en.wikipedia.org/wiki/Cron Cron on Wikipedia]


The following table is meant to bring some light into those built-in jobs.The list is by no means complete, jobs may depend on the firmware version, start times may vary. If you find a job on your QNAP, that is missing in the table, add it. The list could help to identify unwanted jobs on the QNAP&nbsp;that regularly wake up your harddisk(s).<br>
== Stock crontab jobs ==
 
When using crontab -l on a default QNAP, QNAPs has some default crontab jobs.
 
Usually you will want to know, what default jobs are running on the QNAP based on schedule.
 
The following table is meant to bring some light into those built-in jobs.The list is by no means complete, jobs may depend on the firmware version, start times may vary. If you find a job on your QNAP, that is missing in the table, add it. The list could help to identify unwanted jobs on the QNAP&nbsp;that regularly wake up your harddisk(s).


{| width="100%" cellspacing="1" cellpadding="1" border="1"
{| width="100%" cellspacing="1" cellpadding="1" border="1"
|-
|-
! scope="col" | Default Time<br>  
! scope="col" | Default Time<br/>
! scope="col" | Job<br>  
! scope="col" | Job<br/>
! scope="col" | Explanation<br>
! scope="col" | Explanation<br/>
|-
|-
| 0 4 * * *  
| 0 4 * * *
| /sbin/hwclock -s  
| /sbin/hwclock -s
| http://linux.die.net/man/8/hwclock<br>
| [http://linux.die.net/man/8/hwclock http://linux.die.net/man/8/hwclock]<br/>
|-
|-
| 0 3 * * *  
| 0 3 * * *
| /sbin/vs_refresh  
| /sbin/vs_refresh
| vs_refresh is for share folder space usage calculation (for Network Share Management)<br>
| vs_refresh is for share folder space usage calculation (for Network Share Management)<br/>
|-
|-
| 0 3 * * 0  
| 0 3 * * 0
| /etc/init.d/idmap.sh dump  
| /etc/init.d/idmap.sh dump
| related to SAMBA Active Directory configuration. Used if security is set to ADS.<br>
| related to SAMBA Active Directory configuration. Used if security is set to ADS.<br/>
|-
|-
| 0 3 * * *  
| 0 3 * * *
| /sbin/clean_reset_pwd  
| /sbin/clean_reset_pwd
| (unclear) might be the new behaviour of the network recycle bin (purging files after a certain amount of days)<br>
| (unclear) might be the new behaviour of the network recycle bin (purging files after a certain amount of days)<br/>
|-
|-
| 0-59/15 * * * *  
| 0-59/15 * * * *
| /etc/init.d/nss2_dusg.sh  
| /etc/init.d/nss2_dusg.sh
| is a job of the Surveillance Station<br>
| is a job of the Surveillance Station<br/>
|-
|-
| 10 15 * * *  
| 10 15 * * *
| /usr/bin/power_clean -c 2&gt;/dev/null  
| /usr/bin/power_clean -c 2>/dev/null
| &nbsp;?<br>
| &nbsp;?<br/>
|-
|-
| 4 3 * * 3  
| 4 3 * * 3
| /etc/init.d/backup_conf.sh  
| /etc/init.d/backup_conf.sh
| does an automatic backup of several config files in /etc/config. <br>
| does an automatic backup of several config files in /etc/config.<br/>
|-
|-
| 0 3 * * * <br>  
| 0 3 * * *<br/>
| /bin/rm -rf /mnt/HDA_ROOT/twonkymedia/twonkymedia.db/cache/*<br>  
| /bin/rm -rf /mnt/HDA_ROOT/twonkymedia/twonkymedia.db/cache/*<br/>
| Will clear the Twonky media cache<br>
| Will clear the Twonky media cache<br/>
|-
|-
| 5 0 * * 1<br>  
| 5 0 * * 1<br/>
| /etc/init.d/antivirus.sh scan 1<br>  
| /etc/init.d/antivirus.sh scan 1<br/>
| If Antivirus is enabled, this will scan the files for viruses<br>
| If Antivirus is enabled, this will scan the files for viruses<br/>
|-
|-
| 0 0 * * *<br>  
| 0 0 * * *<br/>
| /etc/init.d/antivirus.sh archive_log<br>  
| /etc/init.d/antivirus.sh archive_log<br/>
| If Antivirus report jobs are enabled<br>
| If Antivirus report jobs are enabled<br/>
|-
|-
| 0 0 */1 * *<br>  
| 0 0 */1 * *<br/>
| /etc/init.d/antivirus.sh update_db<br>  
| /etc/init.d/antivirus.sh update_db<br/>
| If Antivirus is enabled, this will check&nbsp; clamav.net for updates<br>
| If Antivirus is enabled, this will check&nbsp; clamav.net for updates<br/>
|-
|-
| <br>  
| <br/>
| /etc/init.d/startup<br>  
| /etc/init.d/startup<br/>
| If a restart/powerof job is configured in power management<br>
| If a restart/powerof job is configured in power management<br/>
|-
|-
| <br>  
| <br/>
| /etc/init.d/poweroff<br>  
| /etc/init.d/poweroff<br/>
| If a restart/powerof job is configured in power management<br>
| If a restart/powerof job is configured in power management<br/>
|}
|}


<br>


== 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!'''  
== 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).  
Here is an example which assumes you are ssh'd / telnet'd into your nas (as admin).
<pre>echo "1 4 * * * /share/custom/scripts/custom1.sh" &gt;&gt; /etc/config/crontab
<pre>echo "1 4 * * * /share/custom/scripts/custom1.sh" &gt;&gt; /etc/config/crontab
echo "40 5 * * * /share/custom/scripts/custom2.sh" &gt;&gt; /etc/config/crontab
echo "40 5 * * * /share/custom/scripts/custom2.sh" &gt;&gt; /etc/config/crontab
crontab /etc/config/crontab</pre>  
crontab /etc/config/crontab</pre>
== Method 2: autorun.sh ==
 
== 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.''  
''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.''


<br> 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!  
<br/>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!


<br> To prevent the loss of your work, use [[Autorun.sh]] or [http://www.qnap.box.cx/ AutoRunMaster] and a custom script for changing the crontab:  
<br/>To prevent the loss of your work, use [[Autorun.sh|Autorun.sh]] or [http://www.qnap.box.cx/ AutoRunMaster] and a custom script for changing the crontab:
<pre>#!/bin/sh
<pre>#!/bin/sh
# location: /share/custom/scripts/contab.sh
# location: /share/custom/scripts/contab.sh
Line 129: Line 133:
# restart crontab
# restart crontab
/etc/init.d/crond.sh restart
/etc/init.d/crond.sh restart
</pre>  
</pre>
Make sure your script is called when the qnap is started by adding it to [[Autorun.sh]] or by using [http://www.qnap.box.cx/ AutoRunMaster]  
 
Make sure your script is called when the qnap is started by adding it to [[Autorun.sh|Autorun.sh]] or by using [http://www.qnap.box.cx/ AutoRunMaster]
 
 


<br>
== Method 1 bis:&nbsp; /etc/config/crontab, load and restart&nbsp; ==


== Method 1 bis:&nbsp; /etc/config/crontab, load and restart&nbsp; ==
In a post on&nbsp;[http://forum.qnap.com/viewtopic.php?f=144&t=32519&p=253371#p253371 http://forum.qnap.com/viewtopic.php?f=144&t=32519&p=253371#p253371]&nbsp;Micke writes&nbsp;:


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


<br>


Don't bother with that kind of script at all. Whenever you add a custom entry then follow this procedure  
Don't bother with that kind of script at all. Whenever you add a custom entry then follow this procedure
<blockquote>1. Edit /etc/config/crontab and add your custom entry. <br>2. Run 'crontab /etc/config/crontab' to load the changes.<br>3. Restart cron, i.e. '/etc/init.d/crond.sh restart' </blockquote>  
<blockquote>1. Edit /etc/config/crontab and add your custom entry.<br/>2. Run 'crontab /etc/config/crontab' to load the changes.<br/>3. Restart cron, i.e. '/etc/init.d/crond.sh restart'</blockquote>
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.  
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<br>
/Mike


Tested on 3.6.0&nbsp;: works fine.&nbsp;  
PS.&nbsp;Don't forget to make your scripts executable&nbsp;! Otherwise they&nbsp;would not start!
<pre>chmod +x filename.sh</pre>


Tested on 3.6.0&nbsp;: works fine.&nbsp;
[[Category:Adding new services]]
[[Category:Adding new services]]

Revision as of 19:16, 6 March 2016

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

Stock crontab jobs

When using crontab -l on a default QNAP, QNAPs has some default crontab jobs.

Usually you will want to know, what default jobs are running on the QNAP based on schedule.

The following table is meant to bring some light into those built-in jobs.The list is by no means complete, jobs may depend on the firmware version, start times may vary. If you find a job on your QNAP, that is missing in the table, add it. The list could help to identify unwanted jobs on the QNAP that regularly wake up your harddisk(s).

Default Time
Job
Explanation
0 4 * * * /sbin/hwclock -s http://linux.die.net/man/8/hwclock
0 3 * * * /sbin/vs_refresh vs_refresh is for share folder space usage calculation (for Network Share Management)
0 3 * * 0 /etc/init.d/idmap.sh dump related to SAMBA Active Directory configuration. Used if security is set to ADS.
0 3 * * * /sbin/clean_reset_pwd (unclear) might be the new behaviour of the network recycle bin (purging files after a certain amount of days)
0-59/15 * * * * /etc/init.d/nss2_dusg.sh is a job of the Surveillance Station
10 15 * * * /usr/bin/power_clean -c 2>/dev/null  ?
4 3 * * 3 /etc/init.d/backup_conf.sh does an automatic backup of several config files in /etc/config.
0 3 * * *
/bin/rm -rf /mnt/HDA_ROOT/twonkymedia/twonkymedia.db/cache/*
Will clear the Twonky media cache
5 0 * * 1
/etc/init.d/antivirus.sh scan 1
If Antivirus is enabled, this will scan the files for viruses
0 0 * * *
/etc/init.d/antivirus.sh archive_log
If Antivirus report jobs are enabled
0 0 */1 * *
/etc/init.d/antivirus.sh update_db
If Antivirus is enabled, this will check  clamav.net for updates

/etc/init.d/startup
If a restart/powerof job is configured in power management

/etc/init.d/poweroff
If a restart/powerof job is configured in power management


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

PS. Don't forget to make your scripts executable ! Otherwise they would not start!

chmod +x filename.sh

Tested on 3.6.0 : works fine.