我嘗試在啓動時運行我的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 –