【问题标题】:How to autorun a python script at startup in Linux如何在 Linux 启动时自动运行 python 脚本
【发布时间】:2020-02-04 20:43:27
【问题描述】:

我有一个需要在启动时调用的客户端脚本。如果我使用 systemctl start dummy.service 手动启动脚本,该脚本运行良好。服务器脚本正在另一台机器上运行。 但是,如果我重新启动我的机器,它不会启动,并且在检查状态时它显示为 failed with result exit-code。当我启动我的机器并且在进入登录屏幕之前 linux 正在加载所有服务时,它显示 未能启动 dummy.service,网络无法访问。究竟是什么问题?

这是我的 dummy.service 代码

路径 /lib/systemd/system/dummy.service

Description=Dummy Service
Wants=network-online.target
After=network.target network-online.target
Conflicts=getty@tty1.service

[Service]
Type=forking
ExecStart=/usr/bin/python3 /usr/bin/client.py
StandardInput=tty-force

[Install]
WantedBy=multi-user.target

我在 /usr/bin 中的 python 脚本是

#!/usr/bin/python3
import socket
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(("192.168.1.105",1234))
msg=s.recv(1024)
print(msg.decode("utf-8"))

【问题讨论】:

    标签: python python-3.x linux sockets python-sockets


    【解决方案1】:
    systemctl enable dummy.service
    

    如果您不是 root 用户:

    chmod 744 the_pythonscript.py
    chmod 644 dummy.service
    

    【讨论】:

    • systemct enable dummy.service ,当通过终端调用 manullay 时工作正常,但是当我重新启动 PC 时它不会自动启动。 我是 root 用户。当我在 linux 启动时重新启动机器时,它显示 无法启动 dummy.service,网络无法访问。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-21
    • 1970-01-01
    相关资源
    最近更新 更多