How to Kill the Tty in Unix

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.

How to kill a process in Unix that is often sleeping?

The shell subprocess invoked by the ( ... ) & construct, and all its children, will be in their own process group.

$ ps -e -o pid,pgid,ppid,tty,comm
PID PGID PPID TT COMMAND
...
2827 2827 2147 pts/1 bash
2832 2827 2827 pts/1 sleep
...

The entire process group can be killed in a single action by specifying a negative number as the process ID to kill. (To do this you must also specify a signal number.)

$ kill -15 -2827
[2]+ Terminated ( trap "" HUP; exec 2> /dev/null; ...

The PGID to kill is guaranteed to be equal to the PID of its process group leader, which in this case is the shell subprocess. So you can modify your code along the lines of

(
# Trap the HUP signal so it doesn't kill me
trap "" 1
# ...
) &

# the special shell variable $! contains the PID of the most recently
# started background process
SUBPROCESS="$!"

# and later when you want to shut it all down again
kill -15 "-$SUBPROCESS"

# ensuring the subprocess is killed with the script would also be a good idea
trap "kill -15 '-$SUBPROCESS'" 0 1 2 3 15

(Note: kill -NAME and trap "..." NAME are not portable shell; however, the meanings of signal numbers 1 through 15 are portable all the way back to V7. If total portability is not an overriding concern, don't write a shell script; the moment you are tempted to reach for an unportable feature, instead stop and rewrite the entire thing in Perl, which is not only a superior programming language, it's more likely to be available on a randomly chosen Unix box than Bash is. Your future self will thank you.)

(Note to pedants: sadly, no readily available version of POSIX.1 can be taken as the reference for what is and is not portable shell, because several major proprietary-Unix vendors froze their shell environments in 1995 plus or minus two years. For complete portability, as e.g. required for autoconf scripting, I'm not aware of a reliable test other than "does this work with Solaris /bin/sh?" (Just be glad you no longer have to dig up access to HP-UX, IRIX, and AIX as well.) However, I am under the impression that you can code to POSIX.1-2001, although not -2008, if you're only interested in portability to the open-source BSDs, full-scale desktop or server Linux, and OSX. I am also under the impression that Android, busybox, and various other embedded environments do not provide all of -2001.)

How to resume a suspended (tty input) process [unix/mac]?

To add to the Chaitanya Bapats answer,

you can use jobs command to list the suspended jobs. The output will be something like this if you have more than one suspended jobs.

[1]  - suspended  sleep 400
[2] + suspended sleep 300

You can bring the job to foreground by using fg. If you have more than one suspended jobs, then you can use fg %ID. ID is the value inside [] in the output of jobs command.

Also, if you want to resume the job without bringing it to the foreground, you can use kill -CONT command. Example usage:

kill -CONT JOBID

You can also use bg command to move job to background. bg will resume the last suspended job. Also, bg %ID will resume the job with [ID] in jobs command output

Find and kill a process in one line using bash and regex

In bash, you should be able to do:

kill $(ps aux | grep '[p]ython csp_build.py' | awk '{print $2}')

Details on its workings are as follows:

  • The ps gives you the list of all the processes.
  • The grep filters that based on your search string, [p] is a trick to stop you picking up the actual grep process itself.
  • The awk just gives you the second field of each line, which is the PID.
  • The $(x) construct means to execute x then take its output and put it on the command line. The output of that ps pipeline inside that construct above is the list of process IDs so you end up with a command like kill 1234 1122 7654.

Here's a transcript showing it in action:

pax> sleep 3600 &
[1] 2225
pax> sleep 3600 &
[2] 2226
pax> sleep 3600 &
[3] 2227
pax> sleep 3600 &
[4] 2228
pax> sleep 3600 &
[5] 2229
pax> kill $(ps aux | grep '[s]leep' | awk '{print $2}')
[5]+ Terminated sleep 3600
[1] Terminated sleep 3600
[2] Terminated sleep 3600
[3]- Terminated sleep 3600
[4]+ Terminated sleep 3600

and you can see it terminating all the sleepers.


Explaining the grep '[p]ython csp_build.py' bit in a bit more detail:

When you do sleep 3600 & followed by ps -ef | grep sleep, you tend to get two processes with sleep in it, the sleep 3600 and the grep sleep (because they both have sleep in them, that's not rocket science).

However, ps -ef | grep '[s]leep' won't create a process with sleep in it, it instead creates grep '[s]leep' and here's the tricky bit: the grep doesn't find it because it's looking for the regular expression "any character from the character class [s] (which is s) followed by leep.

In other words, it's looking for sleep but the grep process is grep '[s]leep' which doesn't have sleep in it.

When I was shown this (by someone here on SO), I immediately started using it because

  • it's one less process than adding | grep -v grep; and
  • it's elegant and sneaky, a rare combination :-)

What's the best way to send a signal to all members of a process group?

You don't say if the tree you want to kill is a single process group. (This is often the case if the tree is the result of forking from a server start or a shell command line.) You can discover process groups using GNU ps as follows:

 ps x -o  "%p %r %y %x %c "

If it is a process group you want to kill, just use the kill(1) command but instead of giving it a process number, give it the negation of the group number. For example to kill every process in group 5112, use kill -TERM -- -5112.

Kill detached screen session

"kill" will only kill one screen window. To "kill" the complete session, use quit.

Example

$ screen -X -S [session # you want to kill] quit

For dead sessions use:
$ screen -wipe

TTY says ? on ps -p command

No, it only means that the TTY, which the script was started from, was closed in the meantime.

If you want to know more about the status, look into the STAT column:

It shows the status of the process. S stands for sleeping: the process
is waiting for something to happen. Z stands for a zombied process. A
zombied processes is one whose parent has died, leaving the child
processes behind. This is not a good thing. D stands for a process
that has entered an uninterruptible sleep. Often, these processes
refuse to die even when passed a SIGKILL. You can read more about
SIGKILL later in the next section on kill . W stands for paging. A
dead process is marked with an X. A process marked T is traced, or
stopped. R means that the process is runable.

source: http://www.slackbook.org/html/process-control-ps.html



Related Topics



Leave a reply



Submit