How To Run Long-running Scripts on a Raspberry Pi

Raspberry Pi
(Image credit: Shutterstock)
4

When I first got started with a Raspberry Pi, I would often use it to tackle some long running scripts - like the continuous monitoring of a video feed, or a small script that gathered data from the web. The problem I faced was how to stay connected to the Pi long enough so that the script wouldn’t terminate.

You could connect a keyboard, monitor, and keep the pi running that way - but I prefer to work with my Raspberry Pi over SSH, so I can have all the comforts of my regular computer.

Latest Videos FromTom's Hardware
Ryder Damen
Freelance Writer

Ryder Damer is a Freelance Writer for Tom's Hardware US covering Raspberry Pi projects and tutorials.

  • gazzat5
    If you're using tmux and you only have one session, it's far easier to reconnect with tmux attach-session

    But by far the easiest method of running a script without it being terminated when you disconnect from SSH is using nohup
    Reply
  • 1_rick
    Unless it isn't on the Pi for some reason, if you have a long-running script that you don't necessarily need to connect back to you can always nohup it.
    Reply
  • Weaver1985
    Great tutorial! Tmux is a great tool for running one-off, long running scripts. I'm also a big fan of supervisor.
    Reply
  • rocketron999
    I tried the tutorial but I get an error message when I enter

    pi@raspberrypi:~ $ sudo systemctl start myscript && sudo systemctl enable myscript


    Failed to start myscript.service: Unit myscript.service has a bad unit file setting.
    See system logs and 'systemctl status myscript.service' for details.

    I named my script my_script.py and copied and pasted from the website so there should be no spelling or syntax errors
    Reply