1

I have a couple of python programs, such as one which checks if the Collatz Conjecture is applicable for a given number or not, and writes output to a file (it runs on a Raspberry Pi).

Although I know that it hasn't been disproved for up to like a quadrillion or something, I just want to run it for programming practice. Although I can set it to start up when the RPi boots, and start it through a ssh session, the main reason I’m using a Pi is so that it can go upto numbers like 1 billion, while not consuming much power even if performance isn’t practical.

When I start it through an SSH Session by typing python col.py, it stops running if I terminate the session. How can I start the script using SSH so that it doesn't stop when I terminate the session (continues till it ends or RPi shuts down)?

1
1

There’s a few ways to do this actually .

The first question I’d ask myself is ”Do I need to see/interact with this task later?”

If yes, I’d use screen or tmux—This is an example of it with htopctrl a-d detaches a screen session and screen -r reattaches it. A detached screen session works even after you have closed the terminal but I was too lazy to do that here.

enter image description here

Else I’d use nohup python col.py & to just run the task in the background.

4
  • I'm going to use nohup as it'll let me redirect terminal output to a file and works great after session is terminated. ty – poiasd Jan 31 '15 at 5:43
  • “else I'd use nohup python col.py to just run the task in the background” nohup just stands for “no hangup”. That—in and of itself—would not set the process as a background process. See my answer for full details on the nohup/& method. – Giacomo1968 Jan 31 '15 at 5:55
  • ... oops. Yeah, needed the & there didn't I? I was significantly more focused on using screen ;p – Journeyman Geek Jan 31 '15 at 5:56
  • @JourneymanGeek I don’t mind screen but find it’s practical use is very idiosyncratic. 9 times out of 10, nohup/& works well and does the job on most any system. – Giacomo1968 Jan 31 '15 at 6:04
5

Using screen is fine, but nohup with & should work as well.

So in your case just run the command like this:

nohup python col.py &

Just so you understand how each item works:

  • nohup: That stands for “no hangup” which means that even if your terminal session is disconnected, the process that is connected to that nohup command will keep on runnig.
  • &: That ampersand in this context tells the shell to run the command that precedes it as a background process.

If you were to just run this:

nohup python col.py

The python col.py would run, but not as a background process; it would be a foreground process locking you into the terminal. And if you ran this:

python col.py &

The python col.py would run in the background, but the second you log out the python col.py command would terminate.

Doing the nohup and & combo is the simplest, most commonly used method of running unattended tasks as an independent background process.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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