I have a python process running, called test.py
that I run from a bash script with input parameters.
$PWD/test.py -s $symbol -folder $folder -n $name -d $name
Once running ps -elf | grep test.py
, the process appears with the parameters.
I am trying, within that same bash script, to kill this process given the input arguments:
pkill -f 'test.py -s $symbol -folder $folder -n $name -d $name'
But this doesn't seem to work.
What is the correct syntax? Do I need to add parentheses ()
or brackets {}
somewhere?
As @Aserre mentioned in the comments, you'll need to use double quotes instead of single quotes in your
pkill
command so that the value of the shell variables is replaced in the string.Also, take into account that
pkill
sendsSIGTERM
by default. Therefore,test.py
should have a signal handler to shutdown when it receives SIGTERM. Iftest.py
doesn't have a signal handler, it will ignore the signal and continue to live. If that is the case, usepkill -9
, which will killptest.py
since signal 9 can't be ignored:Finally, I'm assuming that
test.py
runs as a daemon or somehow runs in the background. Otherwise, your bash script will wait until it exits before it continues. Iftest.py
is not a daemon, your bash script can run it in the background. Here is an example: