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
3
Answers
Just run it in the background.
You could store the logs to system log.
You could learn about
screen
andtmux
virtual terminals, so that you can view the logs later. You could start atmux
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".
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:
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:Or, this is another "single shot" where you won’t get another prompt till you type
EOF
If atd is running you can use
at
to schedule commands to be executed at a particular time.Examples:
Jobs created with
at
are executed only once.