0
votes

I have a problem with start python script just after startup raspberry pi. I've try with init.d, rc.local and cron. No way worked.

My script waiting for input and save it to file:

import datetime
path = '/my/path/to/file.csv'
while 1:
    name = input()
    date = datetime.datetime.now().strftime('%Y-%m-%d %H-%M-%S')
    presence = str(name) + ";" + str(date) + '\n'
    print(presence)

    file = open(path, "a+")
    file.write(presence)
    file.close()

How I can run it after startup and the script will be waiting for input all the time.

Cron:

sudo crontab -e

@reboot python /home/pi/Desktop/myscript.py

rc.local:

python /home/pi/Desktop/myscript.py

1
How do you break out of the while loop? Is it meant to continually ask for input?Chris
Please clarify your post by editing it to provide the following information. Is your script works as you wish when run via command line? but not working when set it up for running via rc.local or cron? how do you setup the rc.local or cron?hcheung
@Chris while continually ask for input but when when the word exit appears on the input the loop is interrupted.Lime
@hcheung when I run this script in command line everything is ok, but when i trying run script just after start up, the script is run but it does not wait for input, it is interrupted with an error that there is nothing on the input.Lime
See my answer on here on using rc.local and cron @reboot.hcheung

1 Answers

1
votes

Note that input() reads from stdin. A program launched from init.d, rc.local, or cron will have stdin open on /dev/null. Which means input() will raise a EOFError. Also, input() evals the line it reads. Which is probably not what you want. So you have at least two problems with your code.

I can't provide a solution because you haven't provided enough information. What do you mean "waiting for input all the time"? Input from where? If the input produces a continuous stream of data do you really want the body of your while loop running as fast as it can execute? Having said that you probably want to replace the input() with a simple sys.stdin.readline() to avoid the implicit eval().