如何从 bash 脚本执行的 bash 脚本中 return PID
How to return the PID from a bash script executed from a bash script
下面的get_pid()函数是为了returndaemon_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 "" in
start)
start
;;
...
*)
echo "Usage: [=13=] {start|stop|restart|status}"
esac
*编辑
开始作为命令行参数传入。
我们使用 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.
下面的get_pid()函数是为了returndaemon_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 "" in
start)
start
;;
...
*)
echo "Usage: [=13=] {start|stop|restart|status}"
esac
*编辑
开始作为命令行参数传入。
我们使用 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.