Hi,
When executing my python script it seems to pause or something when I close down Putty and stops running properly.
How can I execute this so it continues to run?
Thanks
Re: Keep python script running?
hmm, it looks like it actually quits rather than runs! and leaves the GPIO pins on which I set to HIGH.
Re: Keep python script running?
make it run as a daemon from an init script, plenty of posts about it on here.
here's my init script for a gpio python program, you'll need to make some small modifications above the "test -x" line, then put it in /etc/init.d/ and run "sudo update-rc.d pi-radio defaults" or whatever you call the file
here's my init script for a gpio python program, you'll need to make some small modifications above the "test -x" line, then put it in /etc/init.d/ and run "sudo update-rc.d pi-radio defaults" or whatever you call the file
Code: Select all
### BEGIN INIT INFO
# Provides: pi-radio
# Required-Start: autofs $all
# Required-Stop: $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Starts and stops the Raspberry Radio
# Description: Starts and stops the Raspberry Radio
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/bin/pi-radio
NAME=pi-radio
DESC="Raspberry Radio"
test -x $DAEMON || exit 0
. /lib/lsb/init-functions
set -e
case "$1" in
start)
echo -n "Starting $DESC: "
start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid --background --make-pidfile --exec $DAEMON
echo "$NAME."
;;
stop)
echo -n "Stopping $DESC: "
start-stop-daemon --stop --oknodo --quiet --pidfile /var/run/$NAME.pid
echo "$NAME."
;;
restart)
$0 stop
sleep 1
$0 start
;;
*)
echo "Usage: $0 {start|stop|restart}" >&2
exit 1
;;
esac
exit 0
Re: Keep python script running?
thanks, ah yes thanks 

Re: Keep python script running?
hmm, I have the below script but it doesn't seem to run the python script.
If I try stopping the service it cannot kill the process because it doesn't exist... I tried creating another script that does "sudo python temper.py" but it still does the same result?
If I try stopping the service it cannot kill the process because it doesn't exist... I tried creating another script that does "sudo python temper.py" but it still does the same result?
Code: Select all
### BEGIN INIT INFO
# Provides: piTemp
# Required-Start: autofs $all
# Required-Stop: $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Starts and stops the Raspberry Radio
# Description: Starts and stops the Raspberry Radio
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/home/pi/Projects/temper.py
NAME=piTemp
DESC="Raspberry Temperature"
test -x $DAEMON || exit 0
. /lib/lsb/init-functions
set -e
case "$1" in
start)
echo -n "Starting $DESC: "
start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid --background --make-pidfile --exec $DAEMON
echo "$NAME."
;;
stop)
echo -n "Stopping $DESC: "
start-stop-daemon --stop --oknodo --quiet --pidfile /var/run/$NAME.pid
echo "$NAME."
;;
restart)
$0 stop
sleep 1
$0 start
;;
*)
echo "Usage: $0 {start|stop|restart}" >&2
exit 1
;;
esac
exit 0
Re: Keep python script running?
i think Provides, NAME and DAEMON all have to be the same and the same as the python filename (remove the .py extension) and init script
also don't put the python file in your $HOME directory, put it somewhere in the $PATH e.g. /usr/local/bin
and have you got the shebang in the python file - i.e. #!/usr/bin/python as the first line, and does it have 755 permissions?
also don't use sudo, this is an init script that runs as root anyway!
also don't put the python file in your $HOME directory, put it somewhere in the $PATH e.g. /usr/local/bin
and have you got the shebang in the python file - i.e. #!/usr/bin/python as the first line, and does it have 755 permissions?
also don't use sudo, this is an init script that runs as root anyway!
Re: Keep python script running?
OK, All sorted!
Yes I forgot the shebang at the beginning! and I also placed it in /usr/local/bin, makes a bit more sense!
Thank you
Yes I forgot the shebang at the beginning! and I also placed it in /usr/local/bin, makes a bit more sense!
Thank you
Re: Keep python script running?
I just use screen
sudo apt-get install screen
then you SSH into your pi, type screen and bobs your uncle. Then when you close the SSH session and wish to come back, SSH into your pi, type screen -r and you will be back where you left off.
sudo apt-get install screen
then you SSH into your pi, type screen and bobs your uncle. Then when you close the SSH session and wish to come back, SSH into your pi, type screen -r and you will be back where you left off.
Re: Keep python script running?
not really a workable solution, an init script is the way to go, saves you having to login first too.Leestons wrote:I just use screen
sudo apt-get install screen
then you SSH into your pi, type screen and bobs your uncle. Then when you close the SSH session and wish to come back, SSH into your pi, type screen -r and you will be back where you left off.
Re: Keep python script running?
Yes it is. I use this method all the time with my weather station Pi, on which I have two python programs running on different ttys, one for the ADC and one for the LCDsej7278 wrote:not really a workable solution, an init script is the way to go, saves you having to login first too.
Alex Eames RasPi.TV, RasP.iO
Re: Keep python script running?
well ok its not the "proper" way to do it then, same as nohup, its a hack.alexeames wrote:Yes it is. I use this method all the time with my weather station Pi, on which I have two python programs running on different ttys, one for the ADC and one for the LCDsej7278 wrote:not really a workable solution, an init script is the way to go, saves you having to login first too.
Re: Keep python script running?
Depends what you want. If I want to log in and see what the latest readout is, I think it's the best way of doing the job. If that's a hack, then OK.sej7278 wrote:well ok its not the "proper" way to do it then, same as nohup, its a hack.alexeames wrote:Yes it is. I use this method all the time with my weather station Pi, on which I have two python programs running on different ttys, one for the ADC and one for the LCDsej7278 wrote:not really a workable solution, an init script is the way to go, saves you having to login first too.
Alex Eames RasPi.TV, RasP.iO
Re: Keep python script running?
ok solved my autofs problem now on startup i hav:
File "/etc/init.d/rfidsql", Line 12
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
^
SyntaError: invalid syntax
[FAIL] startpar: service(s) returned failure: rfidsql ... failed!
File "/etc/init.d/rfidsql", Line 12
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
^
SyntaError: invalid syntax
[FAIL] startpar: service(s) returned failure: rfidsql ... failed!
-
- Posts: 1
- Joined: Tue Jun 11, 2013 4:03 am
Re: Keep python script running?
this should be run as a bash script, not pythoncookn wrote:ok solved my autofs problem now on startup i hav:
File "/etc/init.d/rfidsql", Line 12
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
^
SyntaError: invalid syntax
[FAIL] startpar: service(s) returned failure: rfidsql ... failed!
Re: Keep python script running?
Hello,
simple solution is to start the program with an '&' at the end of the command line. This creates a daemon process, running detached from the session.
In unix, processes are started as childs of the console session, and closing the session means stopping the processes. Detached processes are not affected.
Example rfe.py:
will print a message to the console, but also to /var/log/messages.
Start in a console session by 'python rfe.py &' and in another session monitor the logger output 'tail -f /var/log/messages'.
Then after a while close the first console session, and the program continues to run: you will see ongoing messages.
To stop this daemon, us 'ps -A | grep python', and you get the process id of this process. Then 'sudo kill <processid>' will stop this. Be careful, mistyping the processid will kill nothing or maybe something useful.
Of course, the program will not be restarted after a shutdown. This is what the init.d-thing is about.
Gerhard
simple solution is to start the program with an '&' at the end of the command line. This creates a daemon process, running detached from the session.
In unix, processes are started as childs of the console session, and closing the session means stopping the processes. Detached processes are not affected.
Example rfe.py:
Code: Select all
import time
import os
while True:
print("hello again")
os.system("logger hello again")
time.sleep(3)
Start in a console session by 'python rfe.py &' and in another session monitor the logger output 'tail -f /var/log/messages'.
Then after a while close the first console session, and the program continues to run: you will see ongoing messages.
To stop this daemon, us 'ps -A | grep python', and you get the process id of this process. Then 'sudo kill <processid>' will stop this. Be careful, mistyping the processid will kill nothing or maybe something useful.
Of course, the program will not be restarted after a shutdown. This is what the init.d-thing is about.
Gerhard
Re: Keep python script running?
cronjob..
00 09-18 * * 1-5 /home/pi/somescript.py &
00 09-18 * * 1-5 /home/pi/somescript.py &