0

I am trying to start a python script called "irremote.py" on startup (currently in my home directory).

I am running raspbian-stretch on a Pi Zero W, and I would like the script to start without a login or other user input. I will not be using this Pi Zero for anything else.

I've done some research into the topic and found a variety of different methods, some which appear to be outdated.

What is the best way I can start my python 3 script?

  • Choose the best for you. If it doesn't work please show what you have done and we may be able to help. – joan Dec 17 '17 at 09:42
  • It looks a bit like a déjà vu: https://raspberrypi.stackexchange.com/q/28199/13700 –  Dec 17 '17 at 18:15

2 Answers2

1

On a system without a systemd daemon running you can put a startup script calling your python program in /etc/init.d - e.g. /etc/init.d/go-go-go and then soft-link to it from inside the desired runlevel - check what runlevel your machine is in after booting by issueing the command

runlevel

You'll get an answer like

N 3

or

N 5

Whatever - say it's 2 - then you'd go to /etc/rc2.d and softlink like so:

ln -s /etc/init.d/go-go-go S99go-irremote

For the systemd approach you need to create a service file in /etc/systend/system - e.g. irremote.service:

[Unit]
Description=do the IR remote stuff I like

[Service]
ExecStart=/home/pi/irremote.py

[Install]
WantedBy=multi-user.target

In any case I suggest reading

man runlevel
flowtron
  • 111
  • 4
  • I tried using the systemd approach, and it won't start the script until I login –  Dec 17 '17 at 17:34
  • Okay it's not the service that's starting the program on login, it's something else I setup earlier. When I check the status of my service, it says it failed with result 'exit-code' and has a status=203/EXEC. Is there a way to see the output of my program. Also, do I need to add something like "#!" at the start of my irremote.py file? –  Dec 17 '17 at 18:14
  • Of course you need the shebang - "#!/usr/bin/python" - and be sure to make it executable. Regarding output I would suggest logging to a file from inside the script. – flowtron Dec 19 '17 at 08:09
0

Look for @reboot in man 5 crontab.

Using @reboot in the user's crontab (see man 1 crontab, especially crontab -e) you can wire a script to be run after reboot without needing superuser privileges to define or run this.

I'm using this way to start screen after reboot:

@reboot screen -c ~/.screenrc.cron-autostart -dmS `hostname`

I see no reason why python3 should misbehave when started with a script this way:

@reboot python3 path/to/script.py script arguments

If your program needs to be nannied from time to time or if you want to see its output, starting it in screen or just redirecting its output to somewhere or logging may be needed.