I'm using pkill to send USR2 signal to suricata by python subprocess as below:
#!/usr/bin/python3
script_command='pkill -SIGUSR2 -f /usr/bin/suricata'
exit_status = subprocess.call(script_command, env=self.config_environment, shell=True)
the result is: exit_status = -12
When I executed on terminal:
pkill -SIGUSR2 -f /usr/bin/suricata
echo $?
the result is: 0
As I understand the document said at https://docs.python.org/3/library/subprocess.html#subprocess.CompletedProcess.returncode
Does Python detect return code of pkill process or suricata process is 12?
How can I bypass this mechanism and force subprocess.call return 0 when pkill send USR2 to suricata successfully and negative exit code corresponding to pkill process's feedback?
The documentation you've linked says
This implies that the process run by
subprocess.call()was killed by SIGUSR2 (sinceSIGUSR2is 12).Since you're using
shell=True, there will be a process with the command linesh -c '... suricata', which will be matched bypkill -f(since-fmeans The pattern is normally only matched against the process name. When -f is set, the full command line is used.).You may want to do
to avoid the additional shell process.
Better yet, you could use e.g.
psutilto find the Suricata process and kill it.EDIT
From the comments:
If you have a pidfile at hand, you don't need
pkillfor anything...