linux script to kill java process

后端 未结 5 1007
情歌与酒
情歌与酒 2021-01-30 01:04

I want linux script to kill java program running on console.

Following is the process running as jar.

[rapp@s1-dlap0 ~]$ ps -ef |grep java
rapp    9473           


        
相关标签:
5条回答
  • 2021-01-30 01:33

    You can simply use pkill -f like this:

    pkill -f 'java -jar'
    

    EDIT: To kill a particular java process running your specific jar use this regex based pkill command:

    pkill -f 'java.*lnwskInterface'
    
    0 讨论(0)
  • 2021-01-30 01:36

    pkill -f for whatever reason does not work for me. Whatever that does, it seems very finicky about actually grepping through what ps aux shows me clearly is there.

    After an afternoon of swearing I went for putting the following in my start script:

    (ps aux | grep -v -e 'grep ' | grep MainApp | tr -s " " | cut -d " " -f 2 | xargs kill -9 ) || true

    0 讨论(0)
  • 2021-01-30 01:39

    if there are multiple java processes and you wish to kill them with one command try the below command

    kill -9 $(ps -ef | pgrep -f "java")
    

    replace "java" with any process string identifier , to kill anything else.

    0 讨论(0)
  • 2021-01-30 01:40

    Use jps to list running java processes. The command returns the process id along with the main class. You can use kill command to kill the process with the returned id or use following one liner script.

    kill $(jps | grep <MainClass> | awk '{print $1}')
    

    MainClass is a class in your running java program which contains the main method.

    0 讨论(0)
  • 2021-01-30 01:43

    If you just want to kill any/all java processes, then all you need is;

    killall java
    

    If, however, you want to kill the wskInterface process in particular, then you're most of the way there, you just need to strip out the process id;

    PID=`ps -ef | grep wskInterface | awk '{ print $2 }'`
    kill -9 $PID
    

    Should do it, there is probably an easier way though...

    0 讨论(0)
提交回复
热议问题