如何在Ubuntu启动时自动启动服务?

时间:2017-09-22 13:33:00

标签: python django ubuntu

我正在使用Ubuntu 16,我想启动该服务。该服务应在系统启动时自动启动。该服务启动django服务器。

[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

在控制台错误中:

● 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'.

1 个答案:

答案 0 :(得分:1)

默认情况下,所有systemd服务都以root身份运行。因此,它在服务启动期间使用root用户的环境来处理所有事情。这可能是这里的问题。可能是django的东西需要像你一样运行。要检查这一点,您可以让systemd以特定用户身份启动服务。对服务文件进行以下更改:

[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

看看是否能解决问题

服务节也可以有一个Group参数。包含它可能没有伤害。