简体   繁体   中英

grepping the PID of a process - Unix

I'm trying to execute the following command:

ps aux | grep com.scheduler.app.workermain | kill -15 [pid]

How can I obtain the [pid] (or list of PID ) using ps aux | grep "expression" ps aux | grep "expression" and pipe that to kill ? There may be zero or many processes running the machine. This is part of an automated job, to ensure all the processes spun will be terminated. A sample line from the command line, when ps aux | grep com.scheduler.app.workermain ps aux | grep com.scheduler.app.workermain is executed is:

jenkins  12373  1.1  4.2 2905440 173628 ?      Sl   19:28   0:05 java -Xmx600m -Dlog4j.configurationFile=log4j2-trace.xml -Dpid=foobar -Dipaddr=127.0.0.1 -cp build/classes:build/dependency/* com.scheduler.app.workermain testing.properties

pkill is used for exactly this purpose. How about:

pkill -15 -f com.scheduler.app.workermain

Also if you just want to grep for a PID you can use pgrep :

pgrep -f com.scheduler.app.workermain

pkill man page

kill -15 $(ps aux | grep -i com.scheduler.app.workermain | awk -F' ' '{ print $2 }')

One of possible solutions is to use the pidof command:

kill $( pidof com.scheduler.app.workermain )

PS. You don't need to pass -15 (or -TERM) to the kill command, as SIGTERM is the default signal sent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM