Home > Back-end >  Run a job in a raspberry pi
Run a job in a raspberry pi

Time:06-02

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

CodePudding user response:

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

CodePudding user response:

if there is a way to keep the script running after the end of the ssh connection.

Just run it in the background.

python3 runapp.py &

You could store the logs to system log.

python3 runapp.py | logger &

You could learn about screen and tmux virtual terminals, so that you can view the logs later. You could start a tmux session, run the command inside and detach the session.

You could setup a systemd service file with the program, and run is "as a service".

  • Related