python脚本启动重启失败

时间:2016-01-13 11:09:07

标签: python

我在树莓派上有一个python脚本,我想在启动时运行。我创建了一个启动文件来运行它似乎启动我的脚本,但然后脚本失败。 我可以在登录时从终端运行脚本,它可以正常工作。 以下是我从失败的开始回来的内容

● myscript.service
   Loaded: loaded (/lib/systemd/system/myscript.service; enabled)
   Active: failed (Result: exit-code) since Wed 2016-01-13 11:20:14 UTC; 1min 27s ago
  Process: 371 ExecStart=/usr/bin/python /home/pi/python/request.py (code=exited, status=1/FAILURE)
 Main PID: 371 (code=exited, status=1/FAILURE)

Jan 13 11:20:14 raspberrypi python[371]: return session.request(method=method, url=url, **kwargs)
Jan 13 11:20:14 raspberrypi python[371]: File "/usr/lib/python2.7/dist packages/requests/sessions.py", line 457, in request
Jan 13 11:20:14 raspberrypi python[371]: resp = self.send(prep, **send_kwargs)
Jan 13 11:20:14 raspberrypi python[371]: File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 569, in send
Jan 13 11:20:14 raspberrypi python[371]: r = adapter.send(request, **kwargs)
Jan 13 11:20:14 raspberrypi python[371]: File "/usr/lib/python2.7/dist-packages/requests/adapters.py", line 407, in send
Jan 13 11:20:14 raspberrypi python[371]: raise ConnectionError(err, request=request)
Jan 13 11:20:14 raspberrypi python[371]: requests.exceptions.ConnectionError: ('Connection aborted.', gaierror(-2, 'Name or service not known'))
Jan 13 11:20:14 raspberrypi systemd[1]: myscript.service: main process exited, code=exited, status=1/FAILURE
Jan 13 11:20:14 raspberrypi systemd[1]: Unit myscript.service entered failed state.

我的python脚本使用请求连接到

下面的intenet
import requests
import wiringpi2
import time

wiringpi2.wiringPiSetupGpio()
wiringpi2.pinMode(17,1)

wiringpi2.digitalWrite(17,1)

def delay ():
        #wait here for a while
        counter = 0
        while counter < 40:
                counter += 1
                #recheck web text file if now "o" then turn water off end delay
                request = requests.get('http://stevesolarhome.com/WaterControl.txt')
                #if still reading "1" then carry on with delay
                if request.text == "1":
                        time.sleep(3)
                elif request.text == "0":
                        counter = 401
        #delay timed out update web to turn water off
        #send update to text file
        payload = {'status':'0'}
        r = requests.get("http://stevesolarhome.com/water/water.php/get", params = payload)
        wiringpi2.digitalWrite(17,1)
        #digitalWrite turn water off

while 1:
        request = requests.get('http://stevesolarhome.com/WaterControl.txt')
        if request.text == "1":
                wiringpi2.digitalWrite(17,0)
                delay ()

任何人都可以提供帮助。你需要启动脚本吗?我正在按照以下教程进行操作 http://www.raspberrypi-spy.co.uk/2015/10/how-to-autorun-a-python-script-on-boot-using-systemd/

2 个答案:

答案 0 :(得分:1)

简单地说,因为python解释器将sudo视为文件并非如此:

US

答案 1 :(得分:0)

您的命令行错误。

启动时

/usr/bin/python sudo /home/pi/python/request.py
由于python尝试执行sudo文件,因此执行

(当然失败)。

你想要

/usr/bin/python /home/pi/python/request.py

因为启动脚本通常在root用户下执行(如果未指定) 否则在.service文件中。