簡體   English   中英

shell啟動/停止python腳本

[英]shell start / stop for python script

我有一個簡單的python腳本,我需要啟動和停止,我需要使用start.sh和stop.sh腳本來完成它。

我有start.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腳本。 認為這是尋找pid的合適方式,但我不會對它下注太多。 start.sh成功啟動它。 當我運行stop.sh時,我再也找不到"ps aux | grep 'myscript.py'"但控制台輸出:

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

這是因為ps aux |grep SOMETHING也找到了grep SOMETHING過程,因為SOMETHING匹配。 執行完grep后,無法找到它。

添加一行: ps aux | grep -v grep | grep YOURSCRIPT ps aux | grep -v grep | grep YOURSCRIPT

其中-v表示排除。 更多關於man grep

init-type腳本對此很有用。 這與我使用的非常相似。 將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

ps aux | grep“/path/to/my/script.py”

將返回script.py實例的pid以及grep的這個實例。 這可能就是為什么你沒有這樣的過程:當你開始殺死grep時,它已經死了。

“正確”的方法可能是讓您的腳本將其pid寫入/ var / run中的文件,並在您終止腳本時清除它。 如果不能選擇更改腳本,請查看start-stop-daemon

如果你想繼續使用grep like方法,請查看proctools 它們內置於大多數GNU / Linux機器上,並且可以在包括OS X在內的BSD上使用:

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

我目前沒有打開unix盒子,所以我無法測試這個,但是想到這個想法應該相當簡單。

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

stop.sh:

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

試試吧。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM