Creating Python Cron Job On Synology: Difference between revisions
No edit summary |
No edit summary Tag: wikieditor |
||
| (4 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
[[Category: | [[Category:Synology]][[Category:Python]] | ||
== Objective == | == Objective == | ||
To install a python script on Synology NAS and run it as a cron job. | 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 == | == Prerequisites == | ||
| Line 63: | Line 63: | ||
The more recent DiskStation on the other hand was able to install `lxml` right away with pip. | 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 == | == Notes == | ||
=== References === | |||
<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 | |||
Latest revision as of 14:06, 24 September 2022
Objective[edit]
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[edit]
Git[edit]
Git is needed if python packages are installed from GitHub.
DSM > Package Center > Utilities > Third-Party > GIT Server > Install
python[edit]
Install Python3 using the Package Center app in the DSM.
pip[edit]
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[edit]
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[edit]
- Generate a SSH key
- Add the SSH key to the GitHub acccount
- The instructions on that page say to use
apt-getto installxclip.apt-getis not available on Synology. [1]xclipis being installed to copy the contents of the ssh key file from the command line, howeverxclipdoesn't seem to be readily available for Synology, so open up the key file and manually copy its contents instead.
- The instructions on that page say to use
Installing python packages[edit]
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[edit]
Crontab format[edit]
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[edit]
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/activateruns 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[edit]
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[edit]
References[edit]
See also[edit]
- Every 5 Minutes - Crontab Guru (lists syntax for specifying values within each column)
- How To Edit the Crontab File to Schedule Jobs - Lifewire
- Crontab Day of Week Syntax - StackOverflow