nykon
nykon

Reputation: 634

pkill doesn't kill process

I have a process running called productivity

[root@productivity ~]# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1009514/sshd        
tcp6       0      0 :::8443                 :::*                    LISTEN      2803472/productivit 
tcp6       0      0 :::443                  :::*                    LISTEN      1017657/httpd       
tcp6       0      0 :::80                   :::*                    LISTEN      1017657/httpd       
tcp6       0      0 :::22                   :::*                    LISTEN      1009514/sshd        
udp        0      0 127.0.0.1:323           0.0.0.0:*                           1009281/chronyd     
udp6       0      0 ::1:323                 :::*                                1009281/chronyd 

I periodically restart the service to renew the let's encrypt certs

[root@productivity ~]# cat restart-productivity.sh 
#!/bin/sh
pkill "productivity"
bash -c "exec -a productivity java -jar productivity.jar -Dkeystore.location=./ssl/productivity.to.keystore . & disown &"

This worked like a charm for months. Now, even when I manually run pkill productivity the service does not get killed. I can only kill the service by calling kill PID. This does work but is useless for automation.

I'm running on CentOS 8 and I'm clueless why it stopped working. Can anyone help me out here?

UPDATE

[root@productivity ecommerce]# ps aux | grep productivity
root     2803472  1.3 10.3 2648228 204168 pts/1  Sl   20:24   0:21 productivity -jar productivity.jar -Dkeystore.location=./ssl/productivity.to.keystore .
root     2803848  0.0  0.0  11800  1152 pts/1    S+   20:49   0:00 grep --color=auto productivity

Upvotes: 1

Views: 4465

Answers (2)

Mario
Mario

Reputation: 1032

I use kill to send SIGTERM to most processes. I don't use pkill any more, instead I prefer pgrep combined with kill ...

pgrep -io "PROCESSNAME" | xargs kill 

The -i is to check for case insensitive and -o is to output first process ID only.

You can use this as an alias in your ~/.bashrc_aliases or direct in your ~/.bashrc. For example I use that to kill discord cause it will not halt on its own.

alias kill_discord='pgrep -io discord | xargs kill'

Just my 2cents.

Update

Rule of thumb for killing processes.

  1. SIGTERM is trying to shutdown a process the normal way.
  2. when SIGTERM doesn't work you can try SIGINT. This is an interrupt like CTRL-C.
  3. if neither of those signal work you can try SIGHUP. This signal is send to processes when closing a terminal. Daemons will reload their config-files.
  4. Last but not least there is SIGKILL. This will kill any process no matter what, and it should only be used when a process isn't responding any more.
kill {-2|-INT|-SIGINT|-s 2|-s INT|-s SIGINT|--signal 2|--signal INT|--signal SIGINT} [PID]
kill {-1|-HUP|-SIGHUP|-s 1|-s HUP|-s SIGHUP|--signal 1|--signal HUP|--signal SIGHUP} [PID]
kill {-9|-KILL|-SIGKILL|-s 9|-s KILL|-s SIGKILL|--signal 9|--signal KILL|--singal SIGKILL} [PID]

Upvotes: 3

nykon
nykon

Reputation: 634

The solution that worked for me in the end was

#!/bin/sh
PID=`pidof productivity`
kill $PID
bash -c "exec -a productivity java -jar productivity.jar . & disown &"

Upvotes: 0

Related Questions