【问题标题】:How can I make systemd run a python script on boot如何让 systemd 在启动时运行 python 脚本
【发布时间】:2020-10-11 02:28:18
【问题描述】:

我正在尝试让 systemd 在启动时执行 python3 脚本,但它失败说由于权限而没有打开一些日志文件,我找不到任何其他解决方案。 如果我使用systemctl start LoginAlert 手动启动它,该脚本工作正常,但在启用它以在启动时启动(使用systemctl enable LoginAlert)后,它会给我下面的错误消息。 我使用自定义 .service 脚本来执行脚本。

[Unit]
Description=Login alert system.

[Service]
Type=simple
ExecStart=/usr/bin/python3 /usr/bin/LoginAlert.py

[Install]
WantedBy=multi-user.target


● LoginAlert.service - Login alert system.
     Loaded: loaded (/etc/systemd/system/LoginAlert.service; enabled; vendor preset: disabled)
     Active: failed (Result: exit-code) since Sat 2020-06-20 22:25:30 EDT; 2min 57s ago
    Process: 622 ExecStart=/usr/bin/python3 /usr/bin/LoginAlert.py (code=exited, status=1/FAILURE)
   Main PID: 622 (code=exited, status=1/FAILURE)

Warning: some journal files were not opened due to insufficient permissions

【问题讨论】:

    标签: linux systemd systemctl


    【解决方案1】:

    我有同样的问题。关键是警告:由于权限不足而没有打开一些日志文件并不是真正的问题。这只是表示您无法看到该服务的日志。

    如果您运行 sudo systemctl status your-service.service,该消息将消失,您可以看到该服务的日志。

    谈到在启动时运行 python 脚本,我遇到了由于“权限”而没有执行 python 脚本的问题。我就是这么想的。

    事实上,当脚本以 root 用户运行时(因为它在启动时执行),ROOT 没有我的脚本需要的 python3 库。因此,我不需要使用 pip3 install selenium 安装它们(例如),我需要运行 * sudo pip3 install selenium* 以便 root 用户拥有这些库。

    希望对您有所帮助,问候!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-31
      • 2022-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-07
      相关资源
      最近更新 更多