简体   繁体   中英

How to autorun a python script at startup in Linux

I have a client script which needs to get invoked at start up. The script runs fine if I start it manually using systemctl start dummy.service . The server script is running on another machine. But if I reboot my machine it does not get start and when checked the status it shows as failed with result exit-code . When I start my machine and when linux is loading all the services before going into log in screen, It shows that failed to start dummy.service, Network is unreachable . What could be the problem exactly?

Here is my dummy.service code

path /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

And my python script in /usr/bin is

#!/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"))
systemctl enable dummy.service

if you are not root user:

chmod 744 the_pythonscript.py
chmod 644 dummy.service

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM