我尝试在启动时运行我的python脚本,但它不起作用。python脚本不会在启动时运行ubuntu
这里是我的Python脚本(不工作):
#!/usr/bin/env python
import paho.mqtt.publish as publish
from datetime import datetime
t = str(datetime.now())
print t
with open("/home/james/mqtt/log.txt", "a+") as f:
f.write("it works " + t + "\n")
这里是我的Python脚本(作品):
#!/usr/bin/env python
from datetime import datetime
t = str(datetime.now())
print t
with open("/home/james/mqtt/log.txt", "a+") as f:
f.write("it works " + t + "\n")
这里是我的rc.local文件(也尽量的crontab和设置在/ect/init.d服务):
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# /bin/mqtt_test.py &
# mosquitto_sub -t "mqtt"
/home/james/mqtt/script.sh
# /etc/mqtt/mqtt_test.py
exit 0
它看起来像进口paho.mqtt.publish可以让我的脚本停止工作,我NE w到Linux,我不知道为什么。有人可以帮我吗?谢谢你的帮助。
Ubuntu 16.04
让我知道你是否需要更多信息。
你会得到什么错误信息? – jprockbelly
您可以使用围绕该导入的“try”块得到'paho.mqtt.publish'的错误。我认为脚本在启动时运行时,找不到默认路径中的模块,您可能需要在您的'rc.local'文件中指定它 – kuro
@jprockbelly我没有看到错误信息,或者我没有知道如何获取错误信息。我还应该补充一点,如果我像这样正常运行它,我的脚本就可以正常工作./script.py –