0

This question already has an answer here:

I have a python file that I would like my Raspberry Pi 3 B to run, the instant that I plug the power cable in. I dont want to have to log in or connect a computer to it before the file starts running. Ive looked around on the internet and a lot of them say to edit the /etc/inittab file, which is a file I dont have on my Raspberry pi. Ive also tried editing raspi-config and set the login to automatic but it still asks for a password when ssh ing in.

Please could someone help me with this?

edit: I didnt mean the "instant" literally, I just would like for it to run without me doing anything to it after it is plugged in.

marked as duplicate by goobering Mar 7 '17 at 17:57

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

It can not be done.

When power is applied the Pi will boot. The boot process takes several seconds. As part of the boot process the operating system (normally Linux) is launched. You can only run a Python script once the operating system has launched.

Depending on the Pi model this will be, say, 10-30 seconds after power has been applied.

  • Sorry my bad. How do I set it so that it will run 10-30 seconds after it receives power? – mnt Mar 7 '17 at 16:31
  • Many ways. Search for how to launch programs at start-up. Personally I'd add the script to crontab using the @reboot line. – joan Mar 7 '17 at 16:39
  • Ive just tried this raspberrypi-spy.co.uk/2013/07/… after I do sudo reboot, should the program run when I plug and unplug the raspberry pi? – mnt Mar 7 '17 at 16:45
  • Yes, provided you have updated the crontab correctly and your script is suitable to run in the background (i.e. doesn't expect keyboard input and doesn't write results to the screen). – joan Mar 7 '17 at 16:49
  • Thanks for your replies Joan, when I sudo reboot. Then the program only runs when I sign in via ssh. Is this some automatic log in issue? I cant seem to enable it using raspi-config – mnt Mar 7 '17 at 16:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.