realtek
Posts: 34
Joined: Wed Jan 09, 2013 3:16 pm

Keep python script running?

Thu Mar 21, 2013 3:04 pm

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

realtek
Posts: 34
Joined: Wed Jan 09, 2013 3:16 pm

Re: Keep python script running?

Thu Mar 21, 2013 3:08 pm

hmm, it looks like it actually quits rather than runs! and leaves the GPIO pins on which I set to HIGH.

sej7278
Posts: 249
Joined: Tue Jan 22, 2013 6:43 pm

Re: Keep python script running?

Thu Mar 21, 2013 3:45 pm

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

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

realtek
Posts: 34
Joined: Wed Jan 09, 2013 3:16 pm

Re: Keep python script running?

Thu Mar 21, 2013 3:54 pm

thanks, ah yes thanks :)

realtek
Posts: 34
Joined: Wed Jan 09, 2013 3:16 pm

Re: Keep python script running?

Thu Mar 21, 2013 4:15 pm

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?

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

sej7278
Posts: 249
Joined: Tue Jan 22, 2013 6:43 pm

Re: Keep python script running?

Thu Mar 21, 2013 8:56 pm

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!

realtek
Posts: 34
Joined: Wed Jan 09, 2013 3:16 pm

Re: Keep python script running?

Fri Mar 22, 2013 10:30 am

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

User avatar
Leestons
Posts: 53
Joined: Sun Jul 22, 2012 2:19 pm

Re: Keep python script running?

Sat Mar 23, 2013 1:18 pm

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.

sej7278
Posts: 249
Joined: Tue Jan 22, 2013 6:43 pm

Re: Keep python script running?

Sat Mar 23, 2013 5:27 pm

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.
not really a workable solution, an init script is the way to go, saves you having to login first too.

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2876
Joined: Sat Mar 03, 2012 11:57 am
Location: UK

Re: Keep python script running?

Sat Mar 23, 2013 7:21 pm

sej7278 wrote:not really a workable solution, an init script is the way to go, saves you having to login first too.
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 LCD
Alex Eames RasPi.TV, RasP.iO

sej7278
Posts: 249
Joined: Tue Jan 22, 2013 6:43 pm

Re: Keep python script running?

Sat Mar 23, 2013 8:11 pm

alexeames wrote:
sej7278 wrote:not really a workable solution, an init script is the way to go, saves you having to login first too.
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 LCD
well ok its not the "proper" way to do it then, same as nohup, its a hack.

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2876
Joined: Sat Mar 03, 2012 11:57 am
Location: UK

Re: Keep python script running?

Sat Mar 23, 2013 9:02 pm

sej7278 wrote:
alexeames wrote:
sej7278 wrote:not really a workable solution, an init script is the way to go, saves you having to login first too.
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 LCD
well ok its not the "proper" way to do it then, same as nohup, its a hack.
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.
Alex Eames RasPi.TV, RasP.iO

cookn
Posts: 2
Joined: Sat Jan 12, 2013 3:52 pm

Re: Keep python script running?

Fri May 17, 2013 10:47 pm

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!

protoShiro
Posts: 1
Joined: Tue Jun 11, 2013 4:03 am

Re: Keep python script running?

Tue Jun 11, 2013 4:06 am

cookn 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!
this should be run as a bash script, not python

ghp
Posts: 3114
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany

Re: Keep python script running?

Sat Jun 15, 2013 8:44 am

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:

Code: Select all

import time
import os

while True:
    print("hello again")
    os.system("logger hello again")
    time.sleep(3)
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

Maddo
Posts: 15
Joined: Mon May 06, 2013 4:39 am

Re: Keep python script running?

Tue Jun 25, 2013 6:38 am

cronjob..

00 09-18 * * 1-5 /home/pi/somescript.py &

Return to “Python”