Editing
Creating Python Cron Job On Synology
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
[[Category:Synology]][[Category:Python]] == 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 <syntaxhighlight lang="bash"> $ wget https://bootstrap.pypa.io/get-pip.py $ sudo python3 get-pip.py $ rm get-pip.py </syntaxhighlight> This will install `pip` at `/volume1/@appstore/py3k/usr/local/bin/`, but the `pip` command isn't automatically available. <syntaxhighlight lang="bash"> $ 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) </syntaxhighlight> === virtualenv === Once `pip` is installed, use it to install `virtualenv`, and make sure the command is available from the command line: <syntaxhighlight lang="bash"> $ sudo pip install virtualenv $ sudo ln -s /volume1/@appstore/py3k/usr/local/bin/virtualenv /usr/local/bin/virtualenv </syntaxhighlight> Create a virtual environment for the python script: <syntaxhighlight lang="bash"> $ virtualenv -p /volume1/@appstore/py3k/usr/local/bin/python3 venv </syntaxhighlight> === SSH key for GitHub === * [https://help.github.com/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent/#platform-linux Generate a SSH key] * [https://help.github.com/articles/adding-a-new-ssh-key-to-your-github-account/#platform-linux 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. <ref>[[Synology Package Managers]]</ref> `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 [[Python_Packaging#Installation_from_GitHub|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.: <syntaxhighlight lang="bash"> . /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 </syntaxhighlight> * `. /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 === <references /> === See also === * [https://crontab.guru/every-5-minutes Every 5 Minutes] - Crontab Guru (lists syntax for specifying values within each column) * [https://www.lifewire.com/crontab-linux-command-4095300 How To Edit the Crontab File to Schedule Jobs] - Lifewire * [https://stackoverflow.com/questions/18919151/crontab-day-of-the-week-syntax Crontab Day of Week Syntax] - StackOverflow
Summary:
Please note that all contributions to Littledamien Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Littledamien Wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information