[英]python script won't run at startup ubuntu
I try to run my python scrip at startup but it doesn't work. 我尝试在启动时运行python脚本,但是它不起作用。
Here is my python script(doesn't work): 这是我的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")
Here is my python script(works): 这是我的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")
Here is my rc.local files(also try crontab and setting up service in /ect/init.d): 这是我的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
It look like by importing paho.mqtt.publish can make my script stop working, I am new to Linux, I have no idea why. 看起来通过导入paho.mqtt.publish可以使我的脚本停止运行,我是Linux的新手,我不知道为什么。 Can someone help me out?
有人可以帮我吗? Thanks for your help.
谢谢你的帮助。
Ubuntu 16.04 Ubuntu 16.04
Let me know if you need more info. 让我知道您是否需要更多信息。
I have faced this problem myself. 我自己已经遇到了这个问题。 For me the problem was path.
对我来说,问题在于道路。 I could get it working by using a shell script to launch a python script and launch the shell script from crontab.
我可以通过使用外壳程序脚本启动python脚本并从crontab启动外壳程序脚本来使其工作。
Here is my launcher.sh
. 这是我的
launcher.sh
。 You may not use sudo if you do not want. 如果不想,则可以不使用sudo。
home/pi/record_data
is the path where my file resides. home/pi/record_data
是我的文件所在的路径。
cd /
cd home/pi/record_data
sudo python record_video.py
In this case record_video.py
is the python file I want to run at startup. 在这种情况下,
record_video.py
是我要在启动时运行的python文件。 In the crontab edit, I added this line below. 在crontab编辑中,我在下面添加了这一行。
@reboot sh /home/pi/record_data/launcher.sh &
Do try this out if it work for you :) Good luck. 如果对您有用,请尝试一下:)祝您好运。 I have not got the logging the error into files working yet though.
我还没有将错误记录到文件中工作。
it looks to me like you need to set/change the write permissions of the file before your python scrypt do this: 在我看来,您需要先设置/更改文件的写权限,然后python scrypt才能执行此操作:
f.write("it works " + t + "\n")
for you is working because (maybe you are the owner of the file). 因为您正在工作,因为(也许您是文件的所有者)。
typical linux file permission is described as: 典型的linux文件权限描述为:
do use the chmod with the property flags, so linux has the rights to write the file too, please refer to the ubuntu help :) 请务必将chmod与属性标志一起使用,因此linux也有权写入文件,请参考ubuntu帮助 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.