簡體   English   中英

如何從bash腳本執行的bash腳本中返回PID

[英]How to return the PID from a bash script executed from a bash script

下面的get_pid()函數旨在返回daemon_itinerary.sh的PID。

以下腳本與daemon_itinerary.sh不在同一個工作目錄中。

#!/bin/bash

PID=""

get_pid() {
    PID='pidof daemon_itinerary.sh'
}

start() {
    echo "Restarting test_daemon"
    get_pid
    if [[ -z $PID ]]; then
        echo "starting test_daemon .."
        sh /var/www/bin/daemon_itinerary.sh &
        get_pid
        echo "done. PID=$PID"
    else
        echo "test_deamon is alrady running, PID=$PID"
    fi
}

case "$1" in
start)
    start
;;
...
*)
    echo "Usage: $0 {start|stop|restart|status}"
esac

*編輯

Start作為命令行參數傳遞。

我們使用pgrep獲取如下所示的進程的pid

PID=$(pgrep -f "daemon_itinerary.sh" | xargs)

# xargs - is given because pgrep will return both process id as well as parent pid
# also it will help us to get all pids if multiple instances are running.
# pgrep option to get session id or parent id alone, here its from manual
# -P, --parent ppid,...
#     Only match processes whose parent process ID is listed.
# -s, --session sid,...
#     Only match processes whose process session ID is listed. Session ID 0 is translated into pgrep's or pkill's own session ID.

暫無
暫無

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

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