user233428
user233428

Reputation: 177

How do I start the service automatically when Ubuntu starts?

I'm using Ubuntu 16 and I want to start the service. The service should start automatically when the system starts. The service starts the django server.

[Unit]
Description=service

[Install]
WantedBy=multi-user.target

[Service]
ExecStart=/usr/bin/python /home/ubuntu/wiki/Backend/manage.py python runserver 0.0.0.0:8000
Type=simple

In the console error:

● wiki.service - service

Loaded: loaded (/etc/systemd/system/wiki.service; enabled; vendor preset: enabled)

Active: failed (Result: exit-code) since Fri 2017-09-22 11:10:44 UTC; 3min 36s ago

Main PID: 1144 (code=exited, status=1/FAILURE)

systemd[1]:Started service.

python[1144]:Traceback (most recent call last):

python[1144]:File "/home/ubuntu/wiki/Backend/manage.py", line 17, in <module>

python[1144]: ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?

systemd[1]: wiki.service: Main process exited, code=exited, status=1/FAILURE

systemd[1]: wiki.service: Unit entered failed state.

systemd[1]: wiki.service: Failed with result 'exit-code'.

Upvotes: 1

Views: 1952

Answers (1)

David Jenkins
David Jenkins

Reputation: 471

By default, all systemd services run as root. So, it uses the root user's environment for everything during the startup of the service. This might be the problem here. It may be that the django stuff needs to be run as you. To check this you can have systemd start the service as a specific user. Make the following change to the service file:

[service]
User=<whatever your username is>
ExecStart=/usr/bin/python /home/ubuntu/wiki/Backend/manage.py python runserver 0.0.0.0:8000
Type=simple

See if that resolves the issue

The service stanza can also have a Group parameter. Might not hurt to include that.

Upvotes: 2

Related Questions