启动时启动Python脚本

1 startup scripting ubuntu python

我有一个运行的脚本

python3 /path/script.py
Run Code Online (Sandbox Code Playgroud)

我不知道如何使脚本在启动时运行。

任何指点都会很棒!

Ste*_*art 5

将其作为服务运行。

创造/etc/systemd/system/myscript.service

[Unit]
Description=My Script

[Service]
ExecStart=/usr/bin/python3 /path/script.py

[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)

然后运行它:

sudo systemctl start myscript    # Runs the script now
sudo systemctl enable myscript   # Sets the script to run every boot
Run Code Online (Sandbox Code Playgroud)

您还可以执行许多其他操作,例如使用 使其以特定用户身份运行User=、使用 使其仅在网络可用后运行After=networking.target或许多其他操作。如果它启动 GUI,那么您可能希望将其作为用户服务运行。

请参阅man systemd.unitman systemd.service了解更多选项。