0

I want to use pgrep command to terminate the running process in centos terminal. I tried:

pgrep fedora-proxy-k8s-postgresql | xargs -r kill -9

and I could not found the process because it's a bash script running in background:

➜  pydolphin git:(tencent-cloud) ps aux|grep k8s-pos
root     14131  0.0  0.0 113288  1084 ?        SN   01:35   0:00 bash ./fedora-proxy-k8s-postgresql.sh

What should I do to find and terminate the process using pgrep? Or I have to use ps command?

1
  • I think that the problem is that fedora-proxy-k8s-postgresql is more than 16 characters long, which is the limit for a name of a process. Check the actual name of the process (not the command line) by running ps -o comm -p <pid> on the pid of a running instance of the script. I assume you'll see something such as fedora-proxy-k8s which is the actual process name.
    – aviro
    Dec 27, 2021 at 7:53

1 Answer 1

1

Try:

pgrep -af 'fedora-proxy-k8s-postgresql'

The pattern is matched against the full command line when -f is set.

And if you need to kill the process:

pkill -9 -f 'fedora-proxy-k8s-postgresql'

As mentioned in the comment by @aviro, pkill should be used carefully, in case some other processes running commands line like vi fedora-proxy-k8s-postgresql.sh exist, they also match this pkill pattern and will be killed. So you had better to ensure the process you want to kill is the only one whose command line matching the pattern, or write a more accurate pattern like -f 'bash ./fedora-proxy-k8s-postgresql.sh', before you execute pkill.

For more details refer to the manpage of pgrep.

1
  • 1
    Of course if you have other processes running vi or less on the script, for instance, they would also be killed, so you should think of an improvement, or mention this caveat in your answer.
    – aviro
    Dec 27, 2021 at 7:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .