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

April 23, 2016 20.1k views
System Tools Linux Basics Ubuntu

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 script.py

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

2 Answers

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

Description=simple python script

Environment= MY_ENVIRONMENT_VAR =/path/to/file.config
ExecStart=/usr/bin/python script.py


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/multi-user.target.wants/mysystemd.service 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

Have another answer? Share your knowledge.