I have a process that listens on a TCP port (?0003). From time to time it crashes - badly. It stops working, but continues hogging the port for some time, so I can't even restart it. I'm looking to automate this.
What I do right now is:
netstat -ntlp |grep -P "\*\:\d0003"
To see what the PID is and then:
kill -9 <pid>
Does anyone have a script (or EXE for that matter) that would link the two steps together, ie. parse the PID from the first command and pass it to the second?
You may do
kill -9
instead of you think the -9 is required. I redirect the netstat stderr because it omits a message when run as non-root which is unimportant for this purpose. I include tcp in the regex to filter out tcp6.You also can do by that way :
there you go, one line for you
I take it that the
python
command is just the first term in the full command, right? If you have the name of the script being run you can look at something likepgrep -f
If you are sure of uniqueness, you can also use the related
pkill -f
What about
killall <command>
are there more than one of these processes?If that don't work try:
That should do the trick.
Maybe the fuser command with the -k and -n options may help. Say "man fuser" for more info.