简体   繁体   English

启动后尝试使用 systemd 运行服务

[英]Trying to run a service using systemd after startup

I have listed below the steps as I entered them.我在下面列出了我输入它们的步骤。 I cannot get the Pi (Raspbian GNU/Linux Buster (10)) to launch alarm.service after startup.我无法让 Pi(Raspbian GNU/Linux Buster (10))在启动后启动 alarm.service。 When I log into the Pi and launch alarm.service manually from the GUI it works, as does the script it runs -- (The script is written to loop) Only problem is I can't get alarm.service to launch automatically without the GUI.当我登录到 Pi 并从 GUI 手动启动 alarm.service 时,它运行的脚本也是如此——(脚本被编写为循环)唯一的问题是我无法让 alarm.service 在没有图形用户界面。 Be great to have it launch last after all other services are up and running.在所有其他服务都启动并运行之后让它最后启动真是太好了。

1. sudo nano /home/pi/alarm_service.py - (fyi: this script runs nicely when i launch it manually from command prompt after pi has booted and i am logged in)
2. sudo nano /etc/systemd/system/alarm.service

[Unit]
Description=Alarm Service
After=multi-user.target
Conflicts=getty@tty1.service
[Service]
Type=simple
ExecStart=/home/pi/alarm_service.py
StandardInput=tty-force
[Install]
WantedBy=multi-user.target

3. sudo chmod 644 /etc/systemd/system/alarm.service
4. sudo systemctl daemon-reload
5. sudo systemctl enable alarm.service
6. sudo systemctl start alarm.service

Error log: alarm.service - Alarm service Loaded: loaded (/etc/systemd/system/alarm.service; enabled; vendor preset: enabled) Active: failed (Result: exit-code) since Fri 2021-05-14 18:57:24 AEST;错误日志:alarm.service - 警报服务已加载:已加载(/etc/systemd/system/alarm.service;已启用;供应商预设:已启用)活动:自 2021 年 5 月 14 日星期五 18 日以来失败(结果:退出代码):美国东部标准时间 57:24; 1min 18s ago Process: 501 ExecStart=/home/pi/alarm_service.py (code=exited, status=1/FAILURE) Main PID: 501 (code=exited, status=1/FAILURE) 1 分钟 18 秒前 进程:501 ExecStart=/home/pi/alarm_service.py (code=exited, status=1/FAILURE) Main PID: 501 (code=exited, status=1/FAILURE)

May 14 18:57:24 raspberrypi alarm_service.py[501]: File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 10 May 14 18:57:24 raspberrypi alarm_service.py[501]: sock = self._create_socket_connection() May 14 18:57:24 raspberrypi alarm_service.py[501]: File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 35 May 14 18:57:24 raspberrypi alarm_service.py[501]: return socket.create_connection(addr, source_address=source, timeout=sel May 14 18:57:24 raspberrypi alarm_service.py[501]: File "/usr/lib/python2.7/socket.py", line 575, in create_connection May 14 18:57:24 raspberrypi alarm_service.py[501]: raise err May 14 18:57:24 raspberrypi alarm_service.py[501]: socket.error: [Errno 101] Network is unreachable May 14 18:57:24 raspberrypi systemd[1]: alarm.service: Main process exited, code=exited, status=1/FAILURE May 14 18:57:24 raspberrypi systemd[1]: alarm.service: Failed with result 'exit-code'. May 14 18:57:24 raspberrypi systemd[1]: Failed to start Alar 5 月 14 日 18:57:24 raspberrypi alarm_service.py[501]:文件“/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py”,第 10 行 5 月 14 日 18:57:24 raspberrypi alarm_service.py[501]: sock = self._create_socket_connection() 5 月 14 日 18:57:24 raspberrypi alarm_service.py[501]: 文件“/usr/local/lib/python2.7/dist-packages/paho/mqtt /client.py”,第 35 行 5 月 14 日 18:57:24 raspberrypi alarm_service.py[501]:返回 socket.create_connection(addr, source_address=source, timeout=sel 5 月 14 日 18:57:24 raspberrypi alarm_service.py[501 ]:文件“/usr/lib/python2.7/socket.py”,第 575 行,在 create_connection 5 月 14 日 18:57:24 raspberrypi alarm_service.py[501]:引发错误 5 月 14 日 18:57:24 raspberrypi alarm_service。 py[501]: socket.error: [Errno 101] Network is unreachable May 14 18:57:24 raspberrypi systemd[1]: alarm.service: Main process exited, code=exited, status=1/FAILURE May 14 18: 57:24 raspberrypi systemd[1]:alarm.service:失败,结果为“exit-code”。5 月 14 日 18:57:24 raspberrypi systemd[1]:无法启动 Alar m service.米服务。

The principle I follow is to make the environment that systemd creates for the script to run as similar to what I use to develop it as possible.我遵循的原则是使 systemd 为脚本创建的环境尽可能类似于我用于开发它的环境。

For the sake of testing I just ran shutdown -r now on a VM where I do this exact thing.为了测试,我只是在我执行此操作的 VM 上运行了shutdown -r now On boot, voila.在启动时,瞧。

$ systemctl status mypython
● mypython.service
   Loaded: ...etc.

The file I have set is我设置的文件是

less /etc/systemd/system/multi-user.target.wants/mypython.service

Content内容

[Unit]
        Description=mypython
        
        [Service]
        WorkingDirectory=/home/me
        ExecStart=/usr/bin/python3 /home/me/mypython.py ./config.yaml
        Restart=always
        RestartSec=5
        User=me
        Group=me

        [Install]
        WantedBy=multi-user.target

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Systemd 服务有时会在启动时崩溃 - Systemd service sometimes crashes on startup Pygame GUI 在 Ubuntu 18.04 上使用 systemd 服务启动 - Pygame GUI on startup with systemd service on Ubuntu 18.04 在使用systemd启动时运行.py文件后,如何通过SSH访问Raspberrypi? - How to access Raspberrypi via SSH after running a .py file at startup using systemd? Python systemd 服务启动后很快停止 - Python systemd service stops soon after started 如何使用Ansible轻松显示Systemd服务的启动错误? - How to easily display startup errors of systemd services using ansible? 如何通过 python 和 dbus (systemd-run like) 创建 systemd 瞬态计时器和服务? - Howto create systemd transient timer and service via python and dbus (systemd-run like)? 作为 systemd/systemctl 服务运行时,python 导入本地模块失败 - python import of local module failing when run as systemd/systemctl service PyInstaller filenotfounderror 并且仅在作为 systemd 服务运行时无法导入名称 - PyInstaller filenotfounderror and cannot import name only when run as systemd service 无法在 Centos 7 服务器上使用 systemd 将 flask 应用程序作为服务运行 - Unable to run flask application as a service with systemd on Centos 7 server 作为systemd服务运行时,Python脚本不会写入文件 - Python script doesn't write to file when run as a systemd service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM