Python Forum
Service that waits for another program to terminate
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Service that waits for another program to terminate
#1
Hello everyone,

my goal is to write a small service, which waits for another process to terminate and then shuts down the RPi.

Just like this:

The user clicks a button in the program, which closes it. Then my RPi shall shutdown.
(here the User actually wants to shutdown the VM by clicking "Shut down" in the Startmenu in Windows,
this terminates two processes on the RPi, but does not shutdown the RPi <-- that´s where my Service shall help)

Therefore i need something that waits for the process to terminate.

Which possibilities do i have here?

The solution i got uses the pid of the running program and checks every 60sec whether the path /proc/'pid' still exists.
If not ---> shutdown
If yes ---> repeat

A disadvantage of this method is that the worst case is a shutdown 60sec after the program has been closed.
Another is that it might be computational intensive if i repeat this every 3sec. Or is it not?

However...this is my stuff so far:

import subprocess
from subprocess import check_output
import os
import time

def get_pid(name):
    try:
        return int(check_output(["pidof","-s",name]))   	#returns PID of vmware-view
    except subprocess.CalledProcessError, e:
        process_id = -1										#"-1" never exists!

while True:
    time.sleep(60)
    process_id = get_pid("vmware-remotemks")
    if (type(process_id) != int):
        process_id = get_pid("vmware-remotemks-container")
    process_folder = "/proc/"+str(process_id)   			#process_id becomes filepath
    if (os.path.exists(process_folder) == False):
        os.system('shutdown -h now')    					#reboot RPi
So my actual questions are:
-Is this method i use here alright?
-If yes: is there something that can be improved?
-If not: what methods can you suggest?

The RPi will be running around 8hours/day and only work as thin-client
(so vmware-view is the only program users actually use...they won´t even realize it is linux)
The only moment my program should not be working is before the user logs in.

hhh
Reply
#2
See the psutil library.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  The game should now run and terminate once the board is full, but still can’t identif rango 0 1,449 Jul-22-2021, 03:24 AM
Last Post: rango
  Is there a way to not terminate a running thread in python Contra_Boy 3 1,970 May-05-2020, 09:38 PM
Last Post: SheeppOSU
  Create Service like window service jesssajan 3 2,979 Mar-06-2020, 01:09 PM
Last Post: ibreeden
  Terminate a process when hotkey is pressed 66Gramms 0 2,238 Dec-24-2019, 06:41 PM
Last Post: 66Gramms
  How to terminate the caller fuction through callee? 100k 2 2,142 Nov-27-2019, 06:49 PM
Last Post: jefsummers
  How to terminate a list of inputs with a set value? SOS Manning 2 2,179 Sep-19-2019, 01:54 AM
Last Post: scidam
  how to terminate all the processes in C py2exe 0 2,066 Aug-02-2018, 04:31 AM
Last Post: py2exe
  lambda-Amazon EC2:Remove instance termination protection if enabled and terminate ins dragan979 0 3,346 Jun-13-2018, 09:48 AM
Last Post: dragan979
  Using Terminating Signal to Terminate Long Threads crcali 1 2,578 Apr-06-2018, 01:26 AM
Last Post: woooee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020