4

I am using my RPi to measure the temperature of my fermenting homebrew and store it in a MySQL database via a python program.

Two days ago, I SSH'd into the RPi from my labtop and executed the python command. If I close the SSH program or if I put my computer to sleep, will the python program stop collecting data?

How can I guarentee it won't close?

13

I like to use a program called screen

It starts a new separate terminal "screen" from where you go do all the same stuff as in a terminal but if you loose connection that "screen" continues to run on its own thread uninterrupted. You can come back and reconnect to it later.

To install it simply do ...

apt-get install screen

to use it simply type screen. You will be teleported into the new screen :) Run the program that needs to run forever.

While in a screen ...

  • Detach from screen ctrl + a + d
  • Kill screen ctrl + a + k

Outside the screen ...

  • Show all running screens and their names screen -ls
  • Reconnect to a screen screen -r <session id/name>
  • Start a screen with human name screen -S <session name>

The contents of the screen are buffered so when you reconnect see the output and you can use ctrl + pageup but sometimes it does not work and you need enable some other stuff. It is good to write a log file whether you are running it as thread & or screen.

Screen can be used for allot of other things too. Read the documents.

Capitals matter. I run many programs like this on the Pi and it has been a fantastic tool since the invention of Unix :)

| improve this answer | |
  • To scroll up, you can also hit CTRL+A, release and hit escape, and then you can start scrolling. – Matthew Moisen Sep 14 '16 at 2:17
6

Program will stop after some time. Thought the time it take will fluctuate a lot. To overcome this use nohup

nohup script.py&

But even better might be to start the script on startup. That way it will resume collecting, after e.g. a power outage.

Easiest way for this would be to start your script from rc.local. Just run sudo pico /etc/rc.local and add the line /home/pi/script.py& before exit 0

| improve this answer | |
  • Thanks Gerben. Running nohup python thermometer1.py tells me nohup: ignoring input and appending output to 'nohup.out'. I see via ls that a nohup.out file has been created, and a cat nohup.out shows the output from my print command in the python script. Is there anyway I can get it to print to the console so I don't have to cat when I am interested in seeing the temperature ? – Matthew Moisen Jun 22 '13 at 21:42
  • Do you know how can I stop this python script from running once I return? I did a ps -A and couldn't find any reference to my thermometer1.py that I could kill. Thank you. -- I just noticed that ps -A | grep python returns the python -- should I kill that? – Matthew Moisen Jun 22 '13 at 21:45
  • 1
    1.) Printing to the console is impossible with nohup. first because if you close the ssh connection that console is gone. Secondly; if you reconnect using SSH you get a new console, so you would never see the output. If you really need the console, instead of using cat, you could look into screen. – Gerben Jun 23 '13 at 10:43
  • How do you terminate, list or restart scripts running like this? Please update your answer. – Piotr Kula Jun 23 '13 at 13:04
  • @ppumkin I do a ps -e | grep python and then a `kill' on the pid to terminate the script running through nohup. – Matthew Moisen Jul 12 '13 at 17:47
0

You can use the program daemon to make the program run as a daemon. The advantage is, that you can also read the output in the system logs. And you can start and stop the daemon, if you put it in a rc script. I wrote something like this for the mpdas scrobbler program. You can use my script and replace the mpdas with your program.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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