5

I want to build a water dispanser using a raspberry pi. The pi is attached to a touch screen. Further, I made a python script to control everything.

From time to time, I want to adjust the script to make some changes or improvements. Currently I use putty to connect to the pi to change the code from my computer. When I want to re-start the script so it has the newest changes, I attach a keyboard to the pi to re-start it through the console.

Is there a way so that I can start the python script from my Computer through putty on my pi so that it continues to run even when I close the putty connection and the gui is visible on the touch screen?

I can run the script through putty from my Desktop when I use export DISPLAY=:0 but the script closes as soon as I close putty.

2
  • 2
    nohup /home/pi/processnamegoeshere &
    – Dougie
    Apr 23, 2020 at 20:32
  • related if not dupe: raspberrypi.stackexchange.com/q/29641/19949 both tmux and screen are of course more "bloated" than nohup but may offer functionality that is helpful for this particular usecase.
    – Ghanima
    Apr 23, 2020 at 21:55

3 Answers 3

4

Use the "nohup" command to start the script and it will continue running after you disconnect. For example, if your script is my_scripy.py you would start it with:

nohup python3 my_script.py &

If you do "man nohup" it will show you some additional options if needed.

5
  • Would exec do a better job here? Could you explain why one is better than the other for the OP's question?
    – Seamus
    Apr 23, 2020 at 14:11
  • I've rarely used exec but as I understand it, any process running under exec will terminate if the shell is disconnected.
    – jwh20
    Apr 23, 2020 at 14:17
  • exec executes something as a background process so you can continue using the same terminal for other jobs. nohup continues execution even if the terminal is closed. Apr 23, 2020 at 18:37
  • Would running python3 myscript and then disown process_id give the same result?
    – Arthur
    Apr 24, 2020 at 6:07
  • Yeah - I was hoping the Q might prompt you to elaborate on your answer a wee bit. Not that there's anything wrong with a short answer.
    – Seamus
    Apr 24, 2020 at 21:05
1

I have tried nohup, but I use tmux for this purpose. Tmux is a terminal multiplexer. For your purposes, you can think of it as an always-active-console, which runs on the Pi. A user may take control of tmux via Putty, or through a keyboard connected directly to the Pi. The console remains active even after the user releases control.

You should first install it on the Raspberry Pi. Exactly how you install tmux depends on the operating system running on your Pi, but I am guessing this command will work: sudo apt install tmux (Run it on the Raspberry Pi).

Now all you need to do is start your Python script that controls everything from within tmux. Steps:

  1. Login to the Pi through Putty (or through a keyboard).
  2. Run tmux on Raspberry Pi's command line. You should get a new shell.
  3. Run your script.
  4. "Detach" from tmux by pressing CTRL+B and then D.
  5. Logout from Putty (by exit or CTRL+D etc.)

If you want to stop/restart the script, you can reattach to the tmux session by logging into the Pi and running tmux a (a for attach). You can start/reattach from Putty, or from the Pi. You can even attach to the same session from two channels simultaneously.

An alternative to tmux is screen. They both have many more features, but the stuff above is all you need.

P.S.: You can even set a crontab @reboot entry which starts your script in tmux automatically after each boot.

0

As previous contributors have said, if you start the process using nohup you will be able to disconnect and it should still run:

nohup <program name> &

If you have already started the program through PuTTY and have now want to be able to disconnect, you can use the disown command:

  1. ctrl-Z to suspend the process
  2. bg # run the process in the background
  3. disown -h <job number>

More information is available on this stackoverflow contribution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.