Posts: 1
Joined: Sat Jan 13, 2018 7:31 pm

interacting with background process

Sat Jan 13, 2018 7:46 pm


I've hit a dead end in my knowledge (and in my stubbornness)

I've written a logging script in python (reads settings from a txt file, then reads data from sensors and write them, along with a timestamp, in a logfile, put in an FTP server, hosted by the RPI.

Now that it's beginning to be ready to run properly I want to run it as a background process, to be able to do other things in the raspberry.

Thats fine, but I can't find a good way to exit the process if I somehow need to do some service to the device, or make changes to the script without cutting the power to the rpi.

I've managed to detective my way to it's pid and "kill" it, but that was (at least the way I did it) quite tricky, and not something I want to do every time. Besides, I'm afraid it will mess with the files or communication if it stops in the middle of the program. (open files, half finished communication problems, etc.)

I would like to be able to send some sort of signal so the script knows that the next time it circles back to the top of the while loop it will exit.
The only way i can think of it is to manipulate a file that it reads in the while-expression (don't even know if it can open file, read file, close file inside an expression...)

I would like a cleaner solution, and I would say I'm not the first who wants to communicate with a background process..

Thankful for all the help I can get :)

User avatar
Posts: 4141
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: interacting with background process

Sat Jan 13, 2018 10:57 pm

Python 2 or Python 3 ?

You can add a "signalhandler" to your code. The signalhandler will run when the OS send certain signal to your process. This way your code can clean up before exiting.

Also if you define your process as a "service" in "systemd" you can define how the system starts ( and restarts ) and stops your program:
https://www.freedesktop.org/software/sy ... rvice.html

Posts: 312
Joined: Thu Aug 14, 2014 12:49 am

Re: interacting with background process

Sat Jan 13, 2018 11:20 pm

What you want to do is actually how thousands (millions?) of programs work.
This is called a daemon or a service, a program which always run in background with no user interaction.

As already suggested you could use signal handlers (SIGTERM) - something like this will do https://stackoverflow.com/questions/184 ... gracefully
(hint: google: python daemon sigterm)

Thinks to look for: implement proper exception handling, implement proper logging so you know what the script is doing or what went wrong when something goes wrong because you won't be able see your scripts output in console when run in bg, write a systemd unit (.service) to control your script.

There are other ways too (dbus, socket, built-in web ui, etc) but those are more involved and require more time and knowledge.

Return to “Python”

Who is online

Users browsing this forum: No registered users and 15 guests