Kill Command in Linux

How to kill a process running on particular port in Linux?

Use the command

 sudo netstat -plten |grep java

used grep java as tomcat uses java as their processes.

It will show the list of processes with port number and process id

tcp6       0      0 :::8080                 :::*                    LISTEN      
1000 30070621 16085/java

the number before /java is a process id. Now use kill command to kill the process

kill -9 16085

-9 implies the process will be killed forcefully.

Kill command does the job but gives error

As @Bodo pointed out, there was more than one PIDs and only one of them got killed while the other showed error as it wasn't started by the same user.

I changed the grep command to filter the process started by the same user and then killed it, so it worked.

Update:

I used this command to successfully run my code and terminate the app as needed.

ps -u | grep service.py | grep -v grep | awk '{print $2}' | xargs kill -9

How to kill a process by its pid in linux

Instead of this:

proid= pidof $proceso

You probably meant this:

proid=$(pidof $proceso)

Even so,
the program might not get killed.
By default, kill PID sends the TERM signal to the specified process,
giving it a chance to shut down in an orderly manner,
for example clean up resources it's using.
The strongest signal to send a process to kill without graceful cleanup is KILL, using kill -KILL PID or kill -9 PID.


I believe it's some kind of problem with the bash language (which I just started learning).

The original line you posted, proid= pidof $proceso should raise an error,
and Bash would print an error message about it.
Debugging problems starts by reading and understanding the error messages the software is trying to tell you.

Kill Command in linux

Try

#!/bin/sh
/usr/local/bin/wrun 'uptime;ps -elf|grep httpd|wc -l;free -m;mpstat' &
pid=$!
sleep 5
kill $pid 2>/dev/null && echo "Killed command on time out"

UPDATE:

A working example (no special commands)

#!/bin/sh
set +x
ping -i 1 google.de &
pid=$!
echo $pid
sleep 5
echo $pid
kill $pid 2>/dev/null && echo "Killed command on time out"

Why does the kill command work differently in bash and zsh

kill is a shell builtin for both zsh and bash, with different implementations and options on each. The zsh builtin does support the POSIX -l option for listing signals, but not the GNU -L extension.

You can always use /bin/kill to run the freestanding program version if you desire. On OSes with a GNU runtime, that'll also support -L.



Related Topics



Leave a reply



Submit