qnap

QNAP’s Cron isn’t the best they’ve accomplished… It’s buggy, and you have to know what you’re doing in order to get it working, because it’s not working as it does on a regular Debian server like Ubuntu i.e.

Some examples that confused us

Just to give you some examples; on every reboot of the NAS the entries are deleted if not saved properly. Also, normally you just use crontab -e to edit the scheduled jobs, but it’s impossible to write something in that file for some reason. Other than that, QNAPs Wiki isn’t updated since 2013 and different guides on the Internet gives different instructions. You could get gray hairs for less.

You’re welcome

We spent several hours on this “issue” before we sorted it out, and to save you guys some time – just follow the guide below.

  1. Edit the crontab file

    We will provide two options here, both options are doing the same thing, but in different ways. Pick the one that you prefer.

    A: Edit the crontab located in /etc/config/ directly inside the crontab

    vi /etc/config/crontab

    The crontab file will look something like this

    10 1 * * * /bin/sh /share/CACHEDEV1_DATA/en0ch.se/backup.sh
    10 2 * * * /bin/sh /share/CACHEDEV1_DATA/FSGO/backup.sh
    0 4 * * * /sbin/hwclock -s
    0 3 * * * /sbin/vs_refresh
    0 3 * * * /sbin/clean_reset_pwd
    0-59/15 * * * * /etc/init.d/nss2_dusg.sh
    30 7 * * * /sbin/clean_upload_file
    0 2 * * * /sbin/qfstrim
    0-59/10 * * * * /etc/init.d/storage_usage.sh
    30 3 * * * /sbin/notice_log_tool -v -R
    0 3 * * 0 /etc/init.d/idmap.sh dump
    10 15 * * * /usr/bin/power_clean -c 2>/dev/null
    30 7 * * * /usr/local/sbin/version_cleaner -t 0 > /dev/null 2>/dev/null
    9 3 * * * /usr/bin/qcloud_cli -c
    0 0 * * 1 /etc/init.d/antivirus.sh scan 1
    0 0 * * * /etc/init.d/antivirus.sh archive_log
    28 0 */1 * * /etc/init.d/antivirus.sh update_db
    4 3 * * 3 /etc/init.d/backup_conf.sh

    As you can see, the bold entries are the ones we made. To add the entries with the VI editor you first have to press “i” for insert, add your commands, and then [CTRL+C] to stop insert, and last but not least press “:wq” to write and quit.

    B: Use echo to store your command in the crontab file from the command line

    $~: echo "1 4 * * * /bin/sh /share/CACHEDEV1_DATA/your-backup-folder/backup.sh" >> /etc/config/crontab

    This command will run backup.sh 1 minute past 4 AM.

  2. Load the crontab file to make the changes stick

    To make the crontab persistent during reboot, you have to execute this command

    $~: crontab /etc/config/crontab

    Please note that you cannot save the script in /etc/, or /bin/ or some other directory outside of your HDD directories. In other words, always save your script in /share/CACHEDEV1_DATA/your-backup-folder. If you don’t, the script will be deleted upon reboot.

  3. Restart crontab
    $~: /etc/init.d/crond.sh restart
  4. Wait for the cron to run, and see if it works

    If it doesn’t work, it could be that you missed something. Read the important hints below, and if it still doesn’t work send Daniel an email.

    Important hints

    1. Always start your script with the correct SHEBANG. On my QNAP TS-269 PRO that is
    #!/bin/sh
    2. Always start your command with
    * * * * * /bin/sh [command]

    If you don’t, the scripts will not be executed.

    3. Don’t forget to give your script the correct permissions in order to be able to be executed by cron.
    $~: chmod +x /path-to-script/backup.sh
    4. Always end your script with one empty line.

 

 Pro tip

Install ownCloud and host your own private cloud. Download the pre-configured ownCloud VM today!

Click here for some more examples on how to schedule a cronjob to execute at your preferred time (scroll to the middle of the page).