下面的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作为命令行参数传入。
答案 0 :(得分:2)
我们使用 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.