Search code examples
bashraspberry-pijobs

Run a job in a raspberry pi


I have written some simple python scripts and I would like to run them on my raspberry pi even when I am not logged in. So far, I can log in to the machine via ssh and run the script without a problem. As soon as I have to disconnect the ssh session, I notice that the script breaks or stops. Thus, I was wondering if there is a way to keep the script running after the end of the ssh connection.

Here is the system I am using: Raspberry pi 3B+ with ubuntu 22.04 LTS, and here is how I run my script:

ssh [email protected]
cd myapp/
python3 runapp.py

Solution

  • You can use nohup to stop hangup signals affecting your process. Here are three different ways of doing it.


    This is a "single shot" command, I mean you type it all in one go:

    ssh SOMEHOST "cd SOMEWHERE && nohup python3 SOMESCRIPT &"
    

    Or here, you log in, change directory and get a new prompt in the remote host, run some commands and then, at some point, exit the ssh session:

    ssh SOMEHOST
    cd SOMEWHERE
    nohup python SOMESCRIPT &
    exit
    

    Or, this is another "single shot" where you won't get another prompt till you type EOF

    ssh SOMEHOST <<EOF
    cd SOMEWHERE
    nohup python SOMESCRIPT &
    EOF