Linux kill process and child process small trick

Source: Internet
Author: User
Tags linux

Our hive Web is to invoke the Polestar RESTful service (HTTPS://GITHUB.COM/LALAGUOZHE/POLESTAR-1) to execute specific hive or shark statements, which have been said hive by users these days The Kill button on the Web is invalid, although the query has been shown to stop, but the job is still running on the mapred job or the Spark worker node that was submitted to jobtracker. I looked and did have this problem.

Polestar the commands that are executed for each query are as follows

Sudo-u yukang.chen bash-c "echo \$\$ >/tmp/hive.pid;source/etc/profile;export krb5ccname=/tmp/krb5cc_500;hive-e ' s Elect count (1) from Hippolog ' "

First sudo to submit query users, the user bash-c process number output to a file, set the environment variable, and then a Java child Process execution statement

PS: Output process number to a file is to kill the job, here because the $ symbol is inside the double quotes, it is replaced by the current user's bash process number, and we need to get the process number of sudo to the specified user to execute the command, so the $ symbol plus the backslash to escape

Yukang.chen user terminal to see PS Xuf

USER       PID%cpu%mem    VSZ   RSS TTY      STAT START time   COMMAND  
1158     30497  0.0  0.0 106204  1556 PTS/25   S+   15:08   0:00 bash-c echo $$ >/tmp/hive.pid;source/etc/profile;export krb5ccname=/tmp/krb5cc_500;hive- E ' select COUNT (1) from Hippolog '  
1158     30504 29.5  2.2 9644528 178476 pts/25 sl+  15:08   0:28  \_/USR/LOCAL/JDK/BIN/JAVA-DPROC_JAR-XMX7000M-SERVER-XX:PARALLELGCTHREADS=8-XX:+USECONCMARKSWEEPGC- Dhadoop.log.dir=/data/logs

The ppid of the Java process is the BASH-C process number 30497 (also the/tmp/hive.pid value), which forms the parent-child process relationship

After the user presses the Kill button on the front end, the backend will sudo to the specified user, read the PID file first, then kill-9 the PID

The observation found that the parent process was killed, but the subprocess was alive, and the Ppid had changed to 1, the init (1) process

View Commands Ps-p 30504-l

F S   UID   PID  PPID  C PRI  NI ADDR SZ wchan  TTY time          CMD  
0 S  1158 30504     1   0-2411132 futex_ pts/25 00:00:28 java

This kill method leads to the kill of the parent process, but the Java process that actually executes hive and shark is still alive and does not exit

There are two ways to solve this problem:

1. Use kill---<PPID>

This way the execution of the statement does not change, the KILL command is changed, and a minus sign is used before the ppid, which will kill the entire process tree (including the subprocess) headed by Ppid.

2. Use the EXEC command

This approach requires more execution of statements, plus exec before the hive command

The EXEC command replaces the current process execution with the subprocess (Bash-c replaced with the Hive Java process), and subsequent commands are not executed after hive.

Execute the statement as follows:

Sudo-u yukang.chen bash-c "echo \$\$ >/tmp/hive.pid;source/etc/profile;export b5ccname=/tmp/krb5cc_500;exec hive-e ' Select COUNT (1) from Hippolog ' "
USER       PID%cpu%mem    VSZ   RSS TTY      STAT START time   COMMAND  
1158     32068 59.0  3.6 9577964 290476 pts/25 sl+  15:18   0:28/usr/local/jdk/bin/java-dproc_jar-xmx7000m-server-xx:parallelgcthreads=8- Xx:+useconcmarksweepgc-dhadoop.log.dir=/data/logs-dh

Because the child Java process replaces the parent bash-c process, you see only one Java process, so that if we kill, it's easy enough that we don't need a kill subprocess

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/Linux/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.