Convert "run at startup" script from upstart to systemd for Ubuntu 16

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]

env MY_ENVIRONMENT_VAR='/path/to/file.config'
chdir /path/to/script/
exec python

How would I achieve the same thing using the systemd service management system in Ubuntu 16.04?

Submit an answer

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!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Figured it out - here’s the equivalent systemd script:

Description=simple python script

Environment= MY_ENVIRONMENT_VAR =/path/to/file.config


This file was in my app directory, so I created a dynamic link to it in the systemd directory:


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/ to /etc/systemd/system/mysystemd.service.