Scheduling a Task on a Synology NAS

March 1st 2019 Synology Bash

Running user-defined scripts as scheduled tasks is the best way to backup external data to your Synology NAS. They can easily be created in the Task Scheduler pane of the Control Panel.

Creating a scheduled user-defined script

However, there are a few important details to be aware of, especially if you're not a versed Linux user:

  • By default, a newly created scheduled task will be set to run as a root user. This gives it full access to the device which you shouldn't need for most tasks. To avoid inadvertently harming your device, you should choose a different user which still has the permissions required for the script to work. Even admin is a much safer choice than root.

    Changing the user for the scheduled task

  • Although you can put your full script directly inside the User-defined script field on the Task Settings tab, I suggest you create a bash file instead and invoke that from the User-defined script field. This will make it easier to test the script on your own computer first and also to update it later if necessary. Don't forget to put the shebang directive in the first line to make it executable:

      #!/bin/bash
      wget ftp://user:pwd123@myserver.com/* -P /volume1/backup/ftp/
    
  • When specifying absolute paths on your device, the information visible in the File Station can be misleading. Its address bar doesn't contain the full path. You must prepend it with a folder matching the volume it belongs to (e.g. the absolute path for /homes/admin would be /volume1/homes/admin). You can find the volume information in the tooltip when hovering over the top level folder in the File Station tree view:

    Tooltip with volume information

  • You can avoid putting sensitive information (e.g. login data) in the bash file by using environment variables:

      #!/bin/bash
      wget ftp://$username:$password@$url/* -P $path
    

    You can then set the values for these variables in the User-defined script field on the Task Settings tab before invoking the bash file:

      export username=user
      export password=pwd123
      export url=myserver.com
      export path=/volume1/backup/ftp/
      /volume1/homes/admin/backup.sh
    

To test the task, you don't need modify its schedule. You can just right-click it in the list and invoke it using the Run command.

Run scheduled task on demand

For troubleshooting, you can enable email notifications on the Task Settings tab to send standard and error output of the script to your email after each run (you need to configure email notifications in the Notification pane of the Control Panel first):

Sending task notification to email

Once your script is working as expected, you can keep the notifications enabled. Just toggle the Send run details only when the script terminates abnormally switch to only receive an email when the task fails.

If for some reason you don't want to use email notifications, you can enable logging to a file for scheduled user-defines scripts in the Settings dialog (click the Settings button at the top of the Task Scheduler pane in the Control Panel to access it):

Logging configuration for scheduled user-defined scripts

For each run, the script contents and the output will be saved in a subfolder created at the specified path. No other configuration options are available, i.e. logging will be enabled for all scheduled user-defined scripts, no matter whether they succeed or fail. That's why I prefer email notifications.

Get notified when a new blog post is published (usually every Friday):

If you're looking for online one-on-one mentorship on a related topic, you can find me on Codementor.
If you need a team of experienced software engineers to help you with a project, contact us at Razum.
Copyright
Creative Commons License