启动停止服务并通过python脚本关闭

时间:2015-02-24 09:39:05

标签: python html raspberry-pi

首先,我在这里完全是诺布,请原谅我如果这是一个愚蠢的问题或打破了一些网络礼节规则。这是我在unix /编程世界的尝试 我正在尝试制作一个基于网络摄像头的机器人。 我想通过网页/ python脚本来控制它。 我正在尝试按照本教程http://blog.ramon.no/2013/06/build-simple-internet-controlled-webcam.html

我在网页上设置了运动而不是mjpeg-streamer的覆盆子。现在我试图停止启动流并从网页关闭覆盆子。 这是我在阅读论坛后看到的,但它不起作用。我设法连续三次关闭树莓但我改变了一些东西,现在我不能再做了

elif action == "shutdown":
print "Attempting to shut down"
call(["sudo", "shutdown", "-h", "now"])
elif action == "startstream":
    import os
os.system("sudo service motion start")
elif action == "stopstream":
    import os
os.system("sudo service motion stop" )

如果我按下启动/停止流,如果我按下关闭没有任何反应,我会收到500错误

我授予用户权限www

sudo nano /etc/sudoers
www-data ALL=(ALL) NOPASSWD: ALL

制作脚本exec

chmod +x rambot.py

这是apache错误日志

[Tue Feb 24 09:37:38 2015] [error] [client 192.168.0.5]   File ", referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:38 2015] [error] [client 192.168.0.5] /usr/lib/cgi-bin/rambot.py, referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:38 2015] [error] [client 192.168.0.5] ", line , referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:38 2015] [error] [client 192.168.0.5] 87, referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:38 2015] [error] [client 192.168.0.5] , referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:38 2015] [error] [client 192.168.0.5]     , referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:38 2015] [error] [client 192.168.0.5] elif action == "stopstream":, referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:38 2015] [error] [client 192.168.0.5]     , referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:38 2015] [error] [client 192.168.0.5]  , referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:38 2015] [error] [client 192.168.0.5]  , referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:38 2015] [error] [client 192.168.0.5]  , referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:38 2015] [error] [client 192.168.0.5] ^, referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:38 2015] [error] [client 192.168.0.5] SyntaxError: invalid syntax, referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:38 2015] [error] [client 192.168.0.5]   File ", referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:38 2015] [error] [client 192.168.0.5] /usr/lib/cgi-bin/rambot.py, referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:38 2015] [error] [client 192.168.0.5] ", line , referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:38 2015] [error] [client 192.168.0.5] 87, referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:38 2015] [error] [client 192.168.0.5] , referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:38 2015] [error] [client 192.168.0.5]     , referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:38 2015] [error] [client 192.168.0.5] elif action == "stopstream":, referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:38 2015] [error] [client 192.168.0.5]     , referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:38 2015] [error] [client 192.168.0.5]  , referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:38 2015] [error] [client 192.168.0.5]   ^, referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:38 2015] [error] [client 192.168.0.5] SyntaxError: invalid syntax, referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:38 2015] [error] [client 192.168.0.5] Premature end of script headers: rambot.py, referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:38 2015] [error] [client 192.168.0.5] Premature end of script headers: rambot.py, referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:39 2015] [error] [client 192.168.0.5] File does not exist: /var/www/css, referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:41 2015] [error] [client 192.168.0.5]   File ", referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:41 2015] [error] [client 192.168.0.5] /usr/lib/cgi-bin/rambot.py, referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:41 2015] [error] [client 192.168.0.5] ", line , referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:41 2015] [error] [client 192.168.0.5] 87, referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:41 2015] [error] [client 192.168.0.5] , referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:41 2015] [error] [client 192.168.0.5]     , referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:41 2015] [error] [client 192.168.0.5]   File ", referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:41 2015] [error] [client 192.168.0.5] elif action == "stopstream":, referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:41 2015] [error] [client 192.168.0.5]     , referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:41 2015] [error] [client 192.168.0.5]    ^, referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:41 2015] [error] [client 192.168.0.5] SyntaxError: invalid syntax, referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:41 2015] [error] [client 192.168.0.5] /usr/lib/cgi-bin/rambot.py, referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:41 2015] [error] [client 192.168.0.5] ", line , referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:41 2015] [error] [client 192.168.0.5] 87, referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:41 2015] [error] [client 192.168.0.5] , referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:41 2015] [error] [client 192.168.0.5]     , referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:41 2015] [error] [client 192.168.0.5] elif action == "stopstream":, referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:41 2015] [error] [client 192.168.0.5]     , referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:41 2015] [error] [client 192.168.0.5]  , referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:41 2015] [error] [client 192.168.0.5]   ^, referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:41 2015] [error] [client 192.168.0.5] SyntaxError: invalid syntax, referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:41 2015] [error] [client 192.168.0.5] Premature end of script headers: rambot.py, referer: http://192.168.0.10:8083/
[Tue Feb 24 09:37:41 2015] [error] [client 192.168.0.5] Premature end of script headers: rambot.py, referer: http://192.168.0.10:8083/
[Tue Feb 24 09:38:18 2015] [error] [client 192.168.0.5] File does not exist: /var/www/css, referer: http://192.168.0.10:8083/
[Tue Feb 24 09:38:19 2015] [error] [client 192.168.0.5]   File ", referer: http://192.168.0.10:8083/
[Tue Feb 24 09:38:19 2015] [error] [client 192.168.0.5] /usr/lib/cgi-bin/rambot.py, referer: http://192.168.0.10:8083/
[Tue Feb 24 09:38:19 2015] [error] [client 192.168.0.5] ", line , referer: http://192.168.0.10:8083/
[Tue Feb 24 09:38:19 2015] [error] [client 192.168.0.5] 87, referer: http://192.168.0.10:8083/
[Tue Feb 24 09:38:19 2015] [error] [client 192.168.0.5] , referer: http://192.168.0.10:8083/
[Tue Feb 24 09:38:19 2015] [error] [client 192.168.0.5]     elif action == "stopstream":, referer: http://192.168.0.10:8083/
[Tue Feb 24 09:38:19 2015] [error] [client 192.168.0.5]        ^, referer: http://192.168.0.10:8083/
[Tue Feb 24 09:38:19 2015] [error] [client 192.168.0.5] SyntaxError: invalid syntax, referer: http://192.168.0.10:8083/
[Tue Feb 24 09:38:19 2015] [error] [client 192.168.0.5]   File ", referer: http://192.168.0.10:8083/
[Tue Feb 24 09:38:19 2015] [error] [client 192.168.0.5] /usr/lib/cgi-bin/rambot.py, referer: http://192.168.0.10:8083/
[Tue Feb 24 09:38:19 2015] [error] [client 192.168.0.5] ", line , referer: http://192.168.0.10:8083/
[Tue Feb 24 09:38:19 2015] [error] [client 192.168.0.5] 87, referer: http://192.168.0.10:8083/
[Tue Feb 24 09:38:19 2015] [error] [client 192.168.0.5] , referer: http://192.168.0.10:8083/
[Tue Feb 24 09:38:19 2015] [error] [client 192.168.0.5]     , referer: http://192.168.0.10:8083/
[Tue Feb 24 09:38:19 2015] [error] [client 192.168.0.5] elif action == "stopstream":, referer: http://192.168.0.10:8083/
[Tue Feb 24 09:38:19 2015] [error] [client 192.168.0.5]     , referer: http://192.168.0.10:8083/
[Tue Feb 24 09:38:19 2015] [error] [client 192.168.0.5]  , referer: http://192.168.0.10:8083/
[Tue Feb 24 09:38:19 2015] [error] [client 192.168.0.5]  , referer: http://192.168.0.10:8083/
[Tue Feb 24 09:38:19 2015] [error] [client 192.168.0.5]  , referer: http://192.168.0.10:8083/
[Tue Feb 24 09:38:19 2015] [error] [client 192.168.0.5] ^, referer: http://192.168.0.10:8083/
[Tue Feb 24 09:38:19 2015] [error] [client 192.168.0.5] SyntaxError, referer: http://192.168.0.10:8083/
[Tue Feb 24 09:38:19 2015] [error] [client 192.168.0.5] : , referer: http://192.168.0.10:8083/
[Tue Feb 24 09:38:19 2015] [error] [client 192.168.0.5] invalid syntax, referer: http://192.168.0.10:8083/
[Tue Feb 24 09:38:19 2015] [error] [client 192.168.0.5] Premature end of script headers: rambot.py, referer: http://192.168.0.10:8083/
[Tue Feb 24 09:38:19 2015] [error] [client 192.168.0.5] Premature end of script headers: rambot.py, referer: http://192.168.0.10:8083/
[Tue Feb 24 09:38:59 2015] [error] [client 192.168.0.5]   File ", referer: http://192.168.0.10:8083/
[Tue Feb 24 09:38:59 2015] [error] [client 192.168.0.5] /usr/lib/cgi-bin/rambot.py, referer: http://192.168.0.10:8083/
[Tue Feb 24 09:38:59 2015] [error] [client 192.168.0.5] ", line , referer: http://192.168.0.10:8083/
[Tue Feb 24 09:38:59 2015] [error] [client 192.168.0.5] 87, referer: http://192.168.0.10:8083/
[Tue Feb 24 09:38:59 2015] [error] [client 192.168.0.5] , referer: http://192.168.0.10:8083/
[Tue Feb 24 09:38:59 2015] [error] [client 192.168.0.5]     , referer: http://192.168.0.10:8083/
[Tue Feb 24 09:38:59 2015] [error] [client 192.168.0.5] elif action == "stopstream":, referer: http://192.168.0.10:8083/
[Tue Feb 24 09:38:59 2015] [error] [client 192.168.0.5]        ^, referer: http://192.168.0.10:8083/
[Tue Feb 24 09:38:59 2015] [error] [client 192.168.0.5] SyntaxError: invalid syntax, referer: http://192.168.0.10:8083/
[Tue Feb 24 09:38:59 2015] [error] [client 192.168.0.5] Premature end of script headers: rambot.py, referer: http://192.168.0.10:8083/
[Tue Feb 24 09:39:01 2015] [error] [client 192.168.0.5]   File ", referer: http://192.168.0.10:8083/
[Tue Feb 24 09:39:01 2015] [error] [client 192.168.0.5] /usr/lib/cgi-bin/rambot.py, referer: http://192.168.0.10:8083/
[Tue Feb 24 09:39:01 2015] [error] [client 192.168.0.5] ", line , referer: http://192.168.0.10:8083/
[Tue Feb 24 09:39:01 2015] [error] [client 192.168.0.5] 87, referer: http://192.168.0.10:8083/
[Tue Feb 24 09:39:01 2015] [error] [client 192.168.0.5] , referer: http://192.168.0.10:8083/
[Tue Feb 24 09:39:01 2015] [error] [client 192.168.0.5]     , referer: http://192.168.0.10:8083/

3 个答案:

答案 0 :(得分:2)

我看到缩进很远了。如果这不是拼写错误,请缩进如下:

elif action == "shutdown":
    print "Attempting to shut down"
    call(["sudo", "shutdown", "-h", "now"])
elif action == "startstream":
    import os
    os.system("sudo service motion start")
elif action == "stopstream":
    import os
    os.system("sudo service motion stop" )

如果这不是问题,抱歉我不确定。

答案 1 :(得分:0)

您可以使用OS库中包含的流程管理功能来实现它:

http://docs.python.org/2/library/os.html#process-managment

答案 2 :(得分:0)

建议使用子进程模块执行命令,而不是os.system()函数。 参考:https://docs.python.org/2/library/os.html#os.system

  

子流程模块提供了更强大的工具来生成新流程并检索其结果;使用该模块比使用此功能更可取

此外,在脚本开头加载所需的模块更为标准。

你可以这样做:

import subprocess
[...]
elif action == "shutdown":
    print "Attempting to shut down"
    return_code = subprocess.call(['sudo', 'shutdown', '-h', 'now'])
elif action == "startstream":
    return_code = subprocess.call(['sudo', 'service', 'motion', 'start'])
elif action == "stopstream":
    return_code = subprocess.call(['sudo', 'service', 'motion', 'stop'])

如果您想获得命令的输出,可以使用subprocess.check_output()代替subprocess.call()

cmd_output = subprocess.check_output(['sudo', 'service', 'apache2', 'status']) 
print(cmd_output)
cmd_output = cmd_output.decode('utf-8').strip('\n\r')
print(cmd_output)

打印:

b'Apache2 is running (pid 2024).\n'
Apache2 is running (pid 2024).

注意:我只使用python3,我希望在这里使用python2没有区别。