2013-07-16 2 views
5

나는 시작과 멈춤이 필요한 간단한 파이썬 스크립트를 가지고 있으며, start.sh와 stop.sh 스크립트를 사용하여 그것을 할 필요가있다.파이썬 스크립트를위한 쉘 시작/중지

#!/bin/sh 

script='/path/to/my/script.py' 
echo 'starting $script with nohup' 

nohup /usr/bin/python $script & 

및 stop.sh

#!/bin/sh 

PID=$(ps aux | grep "/path/to/my/script.py" | awk '{print $2}') 
echo "killing $PID" 
kill -15 $PID 

나는 stop.sh 스크립트를 주로 걱정 :

나는 start.sh 있습니다. 내가 그 pid를 찾을 수있는 적절한 방법이라고 생각하지만 나는 그것에 많이 내기하지 않을 것입니다. start.sh가 성공적으로 시작합니다. 내가 stop.sh을 실행할 때, 나는 더 이상 "ps aux | grep 'myscript.py'"하지만 콘솔 출력하여 프로세스를 찾을 수 있습니다 작동 AND "그런 과정"과 종류의 오류를 준다처럼

killing 25052 
25058 
./stop.sh: 5: kill: No such process 

그렇게 보인다.

이게 실제로 오류가 있습니까? 나는 이것을 정상적으로 접근하고 있는가? 내가주의해야 할 다른 것들이 있습니까? -

편집는 실제로 이런 일에 끝났다 : start.sh

#!/bin/bash 
ENVT=$1 
COMPONENTS=$2 


TARGETS=("/home/user/project/modules/script1.py" "/home/user/project/modules/script2.py") 
for target in "${TARGETS[@]}" 
do 
     PID=$(ps aux | grep -v grep | grep $target | awk '{print $2}') 
     echo $PID 
     if [[ -z "$PID" ]] 
     then 
       echo "starting $target with nohup for env't: $ENVT" 
       nohup python $target $ENVT $COMPONENTS & 
     fi 
done 

stop.sh

#!/bin/bash 
ENVT=$1 

TARGETS=("/home/user/project/modules/script1.py" "/home/user/project/modules/script2.py") 
for target in "${TARGETS[@]}" 
do 
    pkill -f $target 
    echo "killing process $target" 
done 
+1

'$!'를 사용하여 이전 명령의 PID를 얻을 수 있습니다. 그러면 이것을'stop.sh' 파일에서 사용할 수 있습니다. 당신은 또한 여러 번 시작하는 것을 다루기 위해 그 안에 아무것도 없습니다. – will

답변

4

ps aux |grep SOMETHING도 뭔가 때문에 grep SOMETHING 과정을 발견하기 때문이다 성냥. 실행 후 grep이 완료되어 찾을 수 없습니다. -v 수단이 제외 ps aux | grep -v grep | grep YOURSCRIPT

:

는 줄을 추가합니다. man grep에 더 있습니다.

1

ps aux | grep "/path/to/my/script.py"

은 script.py 인스턴스와이 grep 인스턴스에 대한 pid를 모두 반환합니다. 그게 아마도 당신이 그런 과정을 겪지 않는 이유 일거야. 당신이 grep을 죽일 때쯤에는 이미 죽었어.

1

"correct" 접근법은 스크립트가/var/run에있는 파일에 pid를 쓰고 스크립트를 죽일 때이를 지우는 것입니다. 스크립트를 변경할 수 없다면 start-stop-daemon을보십시오.

grep과 같은 방식으로 계속하려면 proctools을보십시오. 그들은 대부분의 GNU/Linux 시스템의 구축 및 OS X를 포함한 BSD에서 쉽게 사용할 수있어 :

pkill -f /path/to/my/script.py 
0

나는 순간에 유닉스 상자를 가지고 있지 않기 때문에 나는 그것을이를 테스트 할 수는 없지만 아이디어를 얻으려면 상당히 간단해야합니다.

start.sh :

if [ -e ./temp ] 
then 
    pid=`cat temp` 
    echo "Process already exists; $pid" 
else 
    script='/path/to/my/script.py' 
    echo 'starting $script with nohup' 
    nohup /usr/bin/python $script & 
    echo $! > temp 
fi 

중지합니다.sh :

if [ -e ./temp ] 
then 
    pid=`cat temp` 
    echo "killing $pid" 
    kill -15 $PID 
    rm temp 
else 
    echo "Process not started" 
fi 

+0

@Brad 파일이 존재하는지 확인합니다. [here] (http://tldp.org/LDP/abs/html/fto.html)를 참조하십시오. – will

2

init 유형의 스크립트가 유용합니다. 이것은 내가 사용하는 것과 매우 비슷합니다. pid를 파일에 저장하고 실행 중인지 확인하려면/proc 파일 시스템을 살펴보십시오.

#!/bin/bash 

script_home=/path/to/my 
script_name="$script_home/script.py" 
pid_file="$script_home/script.pid" 

# returns a boolean and optionally the pid 
running() { 
    local status=false 
    if [[ -f $pid_file ]]; then 
     # check to see it corresponds to the running script 
     local pid=$(< "$pid_file") 
     local cmdline=/proc/$pid/cmdline 
     # you may need to adjust the regexp in the grep command 
     if [[ -f $cmdline ]] && grep -q "$script_name" $cmdline; then 
      status="true $pid" 
     fi 
    fi 
    echo $status 
} 

start() { 
    echo "starting $script_name" 
    nohup "$script_name" & 
    echo $! > "$pid_file" 
} 

stop() { 
    # `kill -0 pid` returns successfully if the pid is running, but does not 
    # actually kill it. 
    kill -0 $1 && kill $1 
    rm "$pid_file" 
    echo "stopped" 
} 

read running pid < <(running) 

case $1 in 
    start) 
     if $running; then 
      echo "$script_name is already running with PID $pid" 
     else 
      start 
     fi 
     ;; 
    stop) 
     stop $pid 
     ;; 
    restart) 
     stop $pid 
     start 
     ;; 
    status) 
     if $running; then 
      echo "$script_name is running with PID $pid" 
     else 
      echo "$script_name is not running" 
     fi 
     ;; 
    *) echo "usage: $0 <start|stop|restart|status>" 
     exit 
     ;; 
esac 
관련 문제