developer tip

프로세스 ID를 캡처하고 존재하는 경우 종료하는 쉘 스크립트

optionbox 2020. 12. 14. 08:05
반응형

프로세스 ID를 캡처하고 존재하는 경우 종료하는 쉘 스크립트


이 질문에 이미 답변이 있습니다.

이 코드를 시도했지만 작동하지 않습니다.

#!/bin/sh

#Find the Process ID for syncapp running instance

PID=`ps -ef | grep syncapp 'awk {print $2}'`

if [[ -z "$PID" ]] then
Kill -9 PID
fi

awk 근처에 오류가 표시됩니다.

어떤 제안이라도 부탁드립니다.


실제로이를 수행하는 가장 쉬운 방법은 아래와 같이 kill 인수를 전달하는 것입니다.

ps -ef | grep your_process_name | grep -v grep | awk '{print $2}' | xargs kill

도움이 되었기를 바랍니다.


이것은 나를 위해 잘 작동합니다.

PID =`ps -eaf | grep syncapp | grep -v grep | awk '{print $ 2}'`
if [[ ""! = "$ PID"]]; 그때
  echo "$ PID 죽이기"
  죽이기 -9 $ PID
fi

pkill명령 사용합니다 .

NAME
       pgrep, pkill - look up or signal processes based on name and 
       other attributes

SYNOPSIS
       pgrep [options] pattern
       pkill [options] pattern

DESCRIPTION
       pgrep looks through the currently running processes and lists 
       the process IDs which match the selection criteria to stdout.
       All the criteria have to match.  For example,

              $ pgrep -u root sshd

       will only list the processes called sshd AND owned by root.
       On the other hand,

              $ pgrep -u root,daemon

       will list the processes owned by root OR daemon.

       pkill will send the specified signal (by default SIGTERM) 
       to each process instead of listing them on stdout.

코드가 인터프리터 (java, python, ...)를 통해 실행되는 경우 프로세스의 이름은 인터프리터의 이름입니다. --full 인수를 사용해야합니다. 이것은 명령 이름 및 인수와 일치합니다.


당신은 아마 쓰고 싶었을 것입니다

`ps -ef | grep syncapp | awk '{print $2}'`

but I will endorse @PaulR's answer - killall -9 syncapp is a much better alternative.


A lot of *NIX systems also have either or both pkill(1) and killall(1) which, allows you to kill processes by name. Using them, you can avoid the whole parsing ps problem.


Came across somewhere..thought it is simple and useful

You can use the command in crontab directly ,

* * * * * ps -lf | grep "user" |  perl -ane '($h,$m,$s) = split /:/,$F
+[13]; kill 9, $F[3] if ($h > 1);'

or, we can write it as shell script ,

#!/bin/sh
# longprockill.sh
ps -lf | grep "user" |  perl -ane '($h,$m,$s) = split /:/,$F[13]; kill
+ 9, $F[3] if ($h > 1);'

And call it crontab like so,

* * * * * longprockill.sh

#!/bin/sh

#Find the Process ID for syncapp running instance

PID=`ps -ef | grep syncapp 'awk {print $2}'`

if [[ -z "$PID" ]] then
--->    Kill -9 PID
fi

Not sure if this helps, but 'kill' is not spelled correctly. It's capitalized.

Try 'kill' instead.


This should kill all processes matching the grep that you are permitted to kill.

-9 means "Kill all processes you can kill".

kill -9 $(ps -ef | grep [s]yncapp | awk '{print $2}')

Try the following script:

#!/bin/bash
pgrep $1 2>&1 > /dev/null
if [ $? -eq 0 ]
then
{
    echo " "$1" PROCESS RUNNING "
    ps -ef | grep $1 | grep -v grep | awk '{print $2}'| xargs kill -9
}
else
{
    echo "  NO $1 PROCESS RUNNING"
};fi

Kill -9 PID

should be

kill -9 $PID

see the difference?


PID=`ps -ef | grep syncapp 'awk {print $2}'`

if [[ -z "$PID" ]] then
**Kill -9 $PID**
fi

참고URL : https://stackoverflow.com/questions/13910087/shell-script-to-capture-process-id-and-kill-it-if-exist

반응형