1

first of all a short overview over my current goal:

I want to use a scheduler to execute a simple python program every second. This program reads some data and enter the results inside a database. Because the scheduled task will operate over several days on a raspberry pie the process should start in the background. Therefore I want to create a python file which can start, stop and get the current status from the background job. Furthermore it should be possible to exit and reenter the control file without stopping the background job.

Currently I tried apscheduler to execute the python file every second. The actual problem is, that I can't access the current python file, to control the status, from another external file. Overall I found no real solution how I can control a subprocess form an external file and after find the same subprocess again after restarting the controlling python file.

EDIT:

So overall as far I got it now I'm able to find the current process with his pid. With that im able to send send a terminatesignal to the current process. Inside my scheduled file I'm able to catch these signals and shut down the program on a normal way.

1 Answers1

1

To control (start, restart, stop, schedule) the background process use subprocess. Here is example of subrocess' popen with timeout.

To pass some data between the scheduler and the background job use one of IPC mechanisms, for example sockets.

Community
  • 1
  • 1
Sergey Belash
  • 1,433
  • 3
  • 16
  • 21
  • Okay to control the process I'm able to use subprocess but how can I find the running subprocess again after restarting the controller (mainprocess)? – Veit Bjarsch Sep 06 '16 at 11:29
  • No, subprocess created using python's subprocess would normally end after the controlling program ends. – AbdealiLoKo Sep 06 '16 at 11:52
  • @VeitBjarsch you may use linux tools like `ps` for that, see [example](http://stackoverflow.com/questions/2940858/kill-process-by-name). – Sergey Belash Sep 06 '16 at 12:08
  • The problem is that this will kill the process on a hard way and not exit ist normally. I found [this](http://stackoverflow.com/questions/16420092/how-to-make-python-script-run-as-service). Maybe it will help with my problem. – Veit Bjarsch Sep 06 '16 at 13:49