1

I made a python script on my raspberry - /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()

It's propose is to get my public ip and store it in a file. I need this script run in a loop so it can update the file as soon the ip changes. If the power fail I want it to run when the raspberry restarts. So, I updated the /etc/rc.local file:

#!/bin/sh -e

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

After that I used sudo reboot to restart the raspberry. I'm using PuTTY from a windows computer to connect to the raspberry. After logging in again I used ps -e | grep script.py to see if my script was runnig but it was not. Then I runned the script manually and it worked!

What would you do to solve this problem?

2
  • Have you given it execution permission ? If not, and you run python /home/pi/bin/script.py manually, then that is your problem. Commented Sep 15, 2015 at 12:25
  • Yes it have execution permission. I used chmod 755 script.py. Commented Sep 15, 2015 at 13:00

3 Answers 3

1

An alternative for running in the cron or init, it to use a userspace monitor.

This tutorial is great showing supervisor.

It is really easy to use.

apt-get install supervisor
service supervisor restart

add to /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

and you can still use supervisorctl to manage it:

$ supervisorctl
> restart ip_update
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Supervisor helped me solve the problem! When reading the error log file I it showed me that the publicip file was not being found. All I needed was to give the full path to the file /home/pi/bin/publicip.
0

First verify your script execution's permission, if it's have a execution permission. After that, you need use & after command of script in (runs to infinite loops), try:

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

More details in raspbian documentation.

2 Comments

Thank you to remember me about the &. But I've done that with the same result.
What the result when do you test the script before execution? as root execute the script. sudo su - then /home/pi/bin/script.py. Check if this way, the script executes.
0

Your other option is to use cron
sudo crontab -e will open the crontab for you
you can set your script to run as often as you like and if you put in the entry:
@reboot /home/pi/bin/script.py
it should run during the boot sequence
other non numeric options are:

@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 * * * *"

The standard entry is:

# 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

Edit:
with reference to your comment, performing it with cron, means that you should take out the timing within your code, as that is what cron is doing. So you would end up up with something like:

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

With reference to your existing code, I notice that you are never closing the file, just an endless loop of opening it with read and then opening it with write, I'm not sure quite how python will handle that offhand but it certainly isn't good practice.

3 Comments

Thank you! I tryied it but still the same problem. The problem is not that the script is not runing. It is running both with the rc.local method and the crontab method. But it then stops. It is suposed to run in an infinite loop and when I log in it should still be running. But the problem, I believe is not in the python code because it runs well manually.
Also for debugging purposes, output text and data at key points in your code. So ouput ip, an error message after the IOERROR, file_value and some "success" text after the write. in this case you will have to write the output to a file. It takes seconds to code and you will be able to use something like tail -f log.txt to watch what the code is doing.
Oops, I have just noticed that you solved the problem. Remember to accept Christiano's answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.