Community Builder
I have a basic upstart script in /etc/init that is used to run a python script upon system startup/reboot.
description "simple python script"
start on runlevel [2345]
stop on runlevel [06]
respawn
env MY_ENVIRONMENT_VAR='/path/to/file.config'
chdir /path/to/script/
exec python script.py
How would I achieve the same thing using the systemd service management system in Ubuntu 16.04?
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Figured it out - here’s the equivalent systemd script:
[Unit]
Description=simple python script
[Service]
Environment= MY_ENVIRONMENT_VAR =/path/to/file.config
WorkingDirectory=/path/to/script
ExecStart=/usr/bin/python script.py
Restart=always
[Install]
WantedBy=multi-user.target
This file was in my app directory, so I created a dynamic link to it in the systemd directory:
/etc/systemd/system/
by doing:
ln -s /path/to/file/my_systemd_script.service /etc/systemd/system/my_systemd_script.service
If you get a “Too many levels of symbolic links” error: See @nabito answer below, just copy the file into the /etc/systemd/system/ folder with a cp instead of creating a symbolic link.
Then I can start it with:
systemctl daemon-reload
systemctl enable my_systemd_script.service
systemctl start my_systemd_script.service
Thanks, for this conversation i have same problem and i have resolved it but i also want to have a log file for that python script so is there any way to add log file??
Thanks for the tip, however I followed your steps and got an error due to symlink the .service when trying to ‘systemctl enable my_systemd_script.service’
Failed to execute operation: Too many levels of symbolic links.
This is in Ubuntu 16.04.1 LTS upgraded from 14.04. The solution I took is to put the actual .service in /etc/systemd/system. Seems systemctl will try to create another symlink for it, as seen in the success output from ‘systemctl enable’ command.
Created symlink from /etc/systemd/system/multi-user.target.wants/mysystemd.service to /etc/systemd/system/mysystemd.service.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.