Johnny010
Posts: 35
Joined: Thu Jul 25, 2013 2:58 pm

Continue running a program after SSH disconnect

Thu Oct 29, 2015 4:06 pm

Hi.

I am accessing my Pi via an SSH terminal.

I am running a python script I wrote and I want it to continue running once I quit/disconnect.

How do I go about this :|?

User avatar
DougieLawson
Posts: 35790
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Continue running a program after SSH disconnect

Thu Oct 29, 2015 4:08 pm

nohup /home/pi/program.to.keep.running &
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

Johnny010
Posts: 35
Joined: Thu Jul 25, 2013 2:58 pm

Re: Continue running a program after SSH disconnect

Thu Oct 29, 2015 4:12 pm

Code: Select all

sudo nohup python myscripy.py &
Is this correct?

User avatar
PeterO
Posts: 4941
Joined: Sun Jul 22, 2012 4:14 pm

Re: Continue running a program after SSH disconnect

Thu Oct 29, 2015 4:18 pm

The "screen" command may also provide some useful facilities for you.
https://www.gnu.org/software/screen/manual/screen.html
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
DougieLawson
Posts: 35790
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Continue running a program after SSH disconnect

Thu Oct 29, 2015 4:20 pm

Yes, that will set python running your script even when you disconnect.

I normally make my python scripts executable and include a shebang line as the first line of the program

chmod 755 ./myscript.py
First line has

Code: Select all

#!/usr/bin/env python3
nohup ./myscript.py &

If your program emits any output it will go in ./nohup.out

Peter's option of using screen is also something to look at. It's more sophisticated than nohup.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
rpdom
Posts: 14984
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Continue running a program after SSH disconnect

Thu Oct 29, 2015 7:04 pm

PeterO wrote:The "screen" command may also provide some useful facilities for you.
https://www.gnu.org/software/screen/manual/screen.html
PeterO
+1 for screen


Return to “General discussion”