首页 文章

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

提问于
浏览
1

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

#!/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()

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

#!/bin/sh -e

/home/pi/bin/script.py
exit 0

之后我用 sudo reboot 重新启动了覆盆子 . 我正在从Windows计算机上使用PuTTY连接到树莓 . 再次登录后,我使用 ps -e | grep script.py 来查看我的脚本是否已运行但不是 . 然后我手动运行脚本,它工作了!

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

3 回答

  • 0

    首先验证您的脚本执行's permission, if it'是否具有执行权限 . 之后,你需要在脚本命令之后使用 & (运行到无限循环),尝试:

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

    raspbian documentation中的更多细节 .

  • 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
    
  • 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将如何处理它,但它肯定不是'好的做法 .

相关问题