简体   繁体   中英

Starting and stopping snmpd subprocess with global variable

I am starting snmpd with subprocess, storing a reference to this as a global variable, then want to kill this process later on.

I start the process like so:

snmp_proc = subprocess.Popen(['snmpd', '-p', pid_file,'-Lf', log_file],
                              stdout=open(os.devnull,'w'),stderr=subprocess.STDOUT)

This starts correctly but seems to also create some sort of zombie process? ps ax output gives:

1716 ?        Z      0:00 [snmpd] <defunct>
1718 ?        S      0:00 snmpd -p /var/run/snmpd.pid -Lf /var/log/snmpd

Now when I try to kill the process later only the defunct zombie process is killed, with the other process remaining. Any idea what I'm doing wrong? Here is the code to stop snmpd:

def stop_snmp(): 
    global snmp_proc
    if not snmp_proc:
        return 
    snmp_proc.terminate()
    snmp_proc.wait()
    snmp_proc = None

<defunct> means that the child process is dead but you haven't read its exit status (using snmp_proc.wait() ).

My guess: snmpd starts a daemon ( double fork, etc ) and returns immediately ie, to avoid zombies, use subprocess.check_call() instead of subprocess.Popen() here.

To stop the started daemon service, there should be a special command. If there is none then read the pid from pid_file and use it to send a signal ( os.kill() ) to the daemon in order to kill it.

I just solved this by passing -f to disable forking

Yes, disabling the daemonizing behavior is another option. snmpd runs the service in the same process (started by Popen() ) in this case and therefore it shouldn't become a zombie unless there is a fatal error eg, if it has failed to start.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM