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

Posted April 23, 2016 137.4k views
Linux BasicsUbuntuSystem Tools

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?

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.

Submit an Answer
3 answers

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 the tip, however I followed your steps and got an error due to symlink the .service when trying to ‘systemctl enable mysystemdscript.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.
  • Thanks, I got the same error in the past and never found the cause. I will update my answer so others don’t run into the same problem

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??