1

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

Lime
  • 13
  • 5
  • 1
    How do you break out of the `while` loop? Is it meant to continually ask for input? – Chris May 27 '18 at 19:50
  • 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 May 28 '18 at 00:34
  • @Chris while continually ask for input but when when the word exit appears on the input the loop is interrupted. – Lime May 29 '18 at 06:18
  • @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 May 29 '18 at 06:23
  • See my answer on [here](https://stackoverflow.com/a/50520948/4902099) on using rc.local and cron @reboot. – hcheung May 29 '18 at 06:27
  • @hcheung thank you! Your answer works for me. I change rc.local and script finally starting after reboot. But I have another problem. It invokes again and again and falls into endless loop. I change my script. When stdin is none script should run max 10 times and stop running. When I run my script manually everything works fine. But after reboot it no longer. Maybe You have any idea how I can fix it? – Lime May 29 '18 at 20:04
  • The new problem your facing has nothing to do with the question and the title of your post anymore. You have an endless loop in your code, is that what your intend or you just want to get the user input once? – hcheung May 30 '18 at 00:25

1 Answers1

1

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().

Kurtis Rader
  • 6,734
  • 13
  • 20
  • Thank you very much! You solve my problem with input error, stdin.readline() works. Unfortunately I have new problem. I change my rc.local in accordance with @hcheung comment and now my script run after reboot but It invokes again and again and falls into endless loop. I change my script. When stdin is none script should run max 10 times and stop running. When I run my script manually everything works fine. But after reboot it no longer. Maybe You have any idea how I can fix it? – Lime May 29 '18 at 20:01
  • I have no idea how to fix it because I have no idea what you intend your program to do. When you run it from rc.local or cron standard input (`sys.stdin`) is opened on the */dev/null* device. Which always returns EOF when you read from it. When you run it manually stdin is open on your tty device which means when you type a line it gets assigned to `name`. So I have no idea why you're doing `while 1: name = input()` when you intend the program to run when stdin is not attached to a tty or file. – Kurtis Rader May 29 '18 at 20:27