Python脚本不能在启动时运行 - raspberry

时间:2015-09-15 10:59:07

标签: python linux raspberry-pi

我在我的树莓上制作了一个python脚本 - /home/pi/bin/script.py:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (!isCreated || mRootView == null) {
        isCreated = true;
        mRootView = inflater.inflate(R.layout.fragment_home_task, container, false);
        ButterKnife.bind(this,mRootView);
        onViewInitialized();
    }
    return mRootView;
}

它的建议是获取我的公共IP并将其存储在一个文件中。 我需要在循环中运行此脚本,以便它可以尽快更新文件 ip改变了。 如果断电,我希望它在覆盆子重新启动时运行。所以, 我更新了 /etc/rc.local 文件:

#!/usr/bin/python

 from urllib2 import urlopen
 from time import sleep

 FILE  = "publicip"
 SITE  = "http://ipecho.net/plain"
 DELAY = 60 # seconds

 def get_ip():
    while True:
        # wait for DELAY seconds
        sleep(DELAY)

        # get my current public ip
        try:
            ip = urlopen(SITE).read()
        except IOError:
            continue
        # compare with the one in file
        file_value = open(FILE).read()
        if ip != file_value:                 # if they are not equal
            open(FILE, "w").write(ip)        # update the ip in file

 if __name__ == "__main__":
    get_ip()

之后我使用 sudo reboot 重新启动了raspberry。我使用的是PuTTY 从Windows计算机连接到覆盆子。再次登录后 我用 ps -e | grep script.py ,看看我的脚本是否运行但是确实如此 不。然后我手动运行脚本,它工作正常!

你会怎么做才能解决这个问题?

3 个答案:

答案 0 :(得分:0)

首先验证脚本执行的权限,如果它具有执行权限。之后,您需要在脚本命令(运行到无限循环)后使用&,尝试:

#!/bin/sh
/home/pi/bin/script.py &

raspbian documentation中的更多详情。

答案 1 :(得分:0)

你的另一个选择是使用cron
sudo crontab -e将为你打开crontab 您可以将脚本设置为随意运行,如果您输入条目:
@reboot /home/pi/bin/script.py
它应该在启动序列中运行
其他非数字选项是:

@yearly        Run once a year, "0 0 1 1 *".
@annually      (same as @yearly)
@monthly       Run once a month, "0 0 1 * *".
@weekly        Run once a week, "0 0 * * 0".
@daily         Run once a day, "0 0 * * *".
@midnight      (same as @daily)
@hourly        Run once an hour, "0 * * * *"

标准条目是:

# Minute Hour Day of Month Month  Day of Week   Command
    0     *       *          *        *         /home/pi/bin/script.py
#Once an hour on the hour
    *     *       *          *        *         /home/pi/bin/script.py
#Every minute

编辑:
参考你的评论,用cron执行它意味着你应该在代码中取出时间,因为这就是cron正在做的事情。所以你最终会得到类似的东西:

#!/usr/bin/python

 from urllib2 import urlopen

 FILE  = "publicip"
 SITE  = "http://ipecho.net/plain"

 def get_ip():
    try:
        ip = urlopen(SITE).read()
    except IOError:
        continue
    # compare with the one in file
    file_value = open(FILE).read()
    if ip != file_value:                 # if they are not equal
        open(FILE, "w").write(ip)        # update the ip in file

 if __name__ == "__main__":
    get_ip()

参考你现有的代码,我注意到你永远不会关闭文件,只是用读取打开它然后用write打开它的无限循环,我不确定python将如何处理但这当然不是一种好的做法。

答案 2 :(得分:0)

在cron或init中运行的替代方法,它使用用户空间监视器。

tutorial非常棒supervisor

这很容易使用。

apt-get install supervisor
service supervisor restart

添加到/etc/supervisor/conf.d/ip_update.conf

[program:ip_update]
command=/home/pi/bin/script.py
autostart=true
autorestart=true
stderr_logfile=/var/log/ip_update.err.log
stdout_logfile=/var/log/ip_update.out.log

您仍然可以使用supervisorctl进行管理:

$ supervisorctl
> restart ip_update