1. place your python script in the following folder: /usr/local/sbin/
2. create a new file in /etc/init.d/ in this case it will be example, so sudo nano /etc/init.d/example
3. Use the following code for the example file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#! /bin/sh # /etc/init.d/example case "$1" in start) echo "Starting example" # run application you want to start python /usr/local/sbin/example.py & ;; stop) echo "Stopping example" # kill application you want to stop killall python ;; *) echo "Usage: /etc/init.d/example{start|stop}" exit 1 ;; esac exit 0 |
4. give the file execute rights, sudo chmod 755 example
5. now execute: sudo update-rc.d example defaults
When you reboot debian it should automatically start the python script, else try service example start and service example stop
Found it while searching a method to start automatically a python script on my new raspberry pi. Works fine now and records outside temperature to a mysql database.
Thanks for your excellent help
Heinrich
Your welcome :-),
Pieter
Thank you so much. I found it so straight forward and easy to understand.
🙂
After hours of searching the internet this finally helped me
i used it to starts a cooling fan when the r pi boots because when i was connecting it directly to 5v it would continue to spin after the rpi was halted
thanks so much
“killall python” <—– have five! =)
Haha, yes it was a quick solution. I will fix it.
1. place your python script in the following folder: /user/local/sbin/
shouldn’t this be: /usr/local/sbin/
Ah, thanks I didn’t recognize the typo.
Great tutorial, tested on a pcDuino with Ubuntu 12 and working great! Thank you for the tutorial was very useful and simple to follow!
What’s the fix for the killall? How do you stop just 1 of ???
Thanks
I have the new code on my laptop.
I will update it this week.
To kill the specific python script you need to list current process running, grep your script, and keep the process id from the output.
kill $(ps aux | grep “python /usr/local/sbin/example.py” | awl ‘{print $2}’)
Very helpful, thank you.
command above should be ‘awk’
kill $(ps aux | grep “python /usr/local/sbin/example.py” | awk ‘{print $2}’)
LIKE! LIKE! LIKE!
First example that worked for me after having tried some others. It starts my smartmeter monitoring py script. Looks like it restarts the script after being disconnected for some reason. Very nice, thanks a lot.
[…] Testing of my python script that reads the serial output from MotherHub seems stable enough to run fulltime now. What I’ve found is though that everytime I turn my PC off my SSH session will time out and kill the python script from running. What I need is a way to run it all the time and auto start it after a reboot. Peiter Vanos has just the thing! I am reposting here for my own reference later. You can find the original code at http://www.pietervanos.net […]
Could you please help me? Upon entering sudo update-rc.d example defaults, I’m getting an error which says “missing LSB overrides.” Any help would be appreciated as I am SUPER new to Linux
I had the same problem,
Go to the skeleton file in the init.d folder.
If look at the /etc/init.d/skeleton script, in the beginning of it there are two marks : ### BEGIN INIT INFO and ### END INIT INFO
The easiest way to fix the warning, is to copy the LSB tags from the skeleton script to the beginning of your blah script and just change skeleton to blah
I used this to set python data logger to input information from temperature sensor to MySQL db on Raspberry Pi 2 with Debian on it – and it was the first thing that actually worked (other than obvious console run). The problem is that the code run for about 2 minutes and then just stopped. Since I do not see anything about the duration period in the code I was wondering if there was any advice from the more experienced as to what could be the issue here? Thanks in advance!
I’ve two python scripts which run two httpwebservers for listening to incoming requests. I hooked those two python programs to this script and started the service. if I kill the process manually, will the script bring back those again automatically? If not what additional info do I need to add to get this in place. Thanks in advance. And this script works very well to trigger the python programs
I didn’t have much luck with this, although I could create the service, and manually start and stop it. My issue was that I could not get it to start automatically on boot. I think it didn’t work because I’m running a newer version of Debian.
Here is the link to the solution that worked for me.
http://www.raspberrypi-spy.co.uk/2015/10/how-to-autorun-a-python-script-on-boot-using-systemd/
I enjoy assembling useful information, this post has got me even more info!
Execute me, I execute: sudo update-rc.d example defaults had some error
The following is being output… from the update-rc.d error….
insserv: Max recursions depth 99 reached
insserv: loop involving service motion at depth 2
insserv: There is a loop between service 7 and mountkernfs if started
insserv: loop involving service mountkernfs at depth 1
insserv: loop involving service 7 at depth 1
insserv: exiting now without changing boot order!
update-rc.d: error: insserv rejected the script header
Execute me, I execute: sudo update-rc.d example defaults had some error
The following is being output… from the update-rc.d error….
insserv: Max recursions depth 99 reached
insserv: loop involving service motion at depth 2
insserv: There is a loop between service 7 and mountkernfs if started
insserv: loop involving service mountkernfs at depth 1
insserv: loop involving service 7 at depth 1
insserv: exiting now without changing boot order!
update-rc.d: error: insserv rejected the script header
Awesome article. really helpful