Creating Python Cron Job On Synology

From Littledamien Wiki
Revision as of 14:06, 24 September 2022 by Video8 (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Objective

To install a python script on Synology NAS and run it as a cron job. The python script relies on packages distributed on both PyPI and GitHub.

Prerequisites

Git

Git is needed if python packages are installed from GitHub.

DSM > Package Center > Utilities > Third-Party > GIT Server > Install

python

Install Python3 using the Package Center app in the DSM.

pip

SSH to the DiskStation and install pip with

$ wget https://bootstrap.pypa.io/get-pip.py 
$ sudo python3 get-pip.py
$ rm get-pip.py

This will install pip at /volume1/@appstore/py3k/usr/local/bin/, but the pip command isn't automatically available.

$ pip
-sh: pip: command not found
$ sudo ln -s /volume1/@appstore/py3k/usr/local/bin/pip /usr/local/bin/pip
$pip --version
pip 9.0.3 from /volume1/@appstore/py3k/usr/local/lib/python3.5/site-packages (python 3.5)

virtualenv

Once pip is installed, use it to install virtualenv, and make sure the command is available from the command line:

$ sudo pip install virtualenv
$ sudo ln -s /volume1/@appstore/py3k/usr/local/bin/virtualenv /usr/local/bin/virtualenv

Create a virtual environment for the python script:

$ virtualenv -p /volume1/@appstore/py3k/usr/local/bin/python3 venv

SSH key for GitHub

  • Generate a SSH key
  • Add the SSH key to the GitHub acccount
    • The instructions on that page say to use apt-get to install xclip. apt-get is not available on Synology. [1] xclip is being installed to copy the contents of the ssh key file from the command line, however xclip doesn't seem to be readily available for Synology, so open up the key file and manually copy its contents instead.

Installing python packages

I experienced an issue on DS213 where at least one PyPI package (lxml) could not be compiled. Something to the effect that there was no gcc or cc compiler available. Not sure why a python package needs that, but I spent a while trying to install the various things that were reported missing without ultimately succeeding.

The more recent DiskStation on the other hand was able to install lxml right away with pip.

For instructions on how to install python packages from GitHub see wiki article on Installing Python Packages from GitHub.

Scheduled task

Crontab format

Edit /etc/crontab to add task.

Columns are minute', hour, day of month, month, day of week.

* runs at every interval, e.g. every minute of the hour, or every day of the month.

*/4 runs every fourth interval, e.g. every four hours, or every four days.

9,12,15 specifies specific values, e.g. 9, 12, and 15 minutes past the hour, or 9 am, 12 pm, and 3 pm.

For days of the week, 1 - 6 stand for Monday through Saturday. 0 and 7 both equal Sunday.

Running a script in a virtual environment

Cron runs a sh shell, not a bash shell, so source is not available to invoke the virtual environment. Use . instead.

Full paths are required for all files and commands since cron runs as root.

Output from the command, such as errors, should be redirected to a log file for fixing errors.

To run the python command, run it from within the virtualenv path, e.g.:

. /var/services/homes/username/scriptdir/venv/bin/activate && /var/services/homes/username/scriptdir/venv/bin/python /var/services/homes/username/scriptdir/run.py > /var/services/homes/username/scriptdir/cron.log
  • . /var/services/homes/username/scriptdir/venv/bin/activate runs the virtual environment.
  • && runs python within the virtual environment.
  • Full path to python within the virtual environment.
  • Full path to python script to run.
  • Redirect output to log file, using full path.

Docker dependent tasks

Docker containers must be restarted after a system reboot.

  • DSM > Package Center > Installed > Docker > Open button
  • Container tab > Settings button

All the Docker containers are displayed and can be restarted from this window.

Notes

References

See also