Running scripts independently on Raspberry Pi

10,944

Solution 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.

Solution 2

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.

Share:
10,944

Related videos on Youtube

poiasd
Author by

poiasd

Updated on September 18, 2022

Comments

  • poiasd
    poiasd over 1 year

    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)?

  • poiasd
    poiasd about 9 years
    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
  • Giacomo1968
    Giacomo1968 about 9 years
    “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.
  • Thalys
    Thalys about 9 years
    ... oops. Yeah, needed the & there didn't I? I was significantly more focused on using screen ;p
  • Giacomo1968
    Giacomo1968 about 9 years
    @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.