使用ksh93我尝试等待后台进程run_cataloguer(),使用下面的脚本在单独的后台进程内完成send_mail():
#!/usr/bin/env ksh
function run_cataloguer
{
echo "In run_cataloguer()"
sleep 2
echo "leaving run_cataloguer()"
}
function send_mail
{
echo "In send_mail()"
#jobs
wait_for_cataloguer
sleep 1
echo "Leaving send_mail() "
}
function wait_for_cataloguer
{
echo "In wait_for_cataloguer() PID_CAT = $PID_CAT"
wait $PID_CAT
waitRet=$?
echo "waitRet = $waitRet"
}
run_cataloguer &
PID_CAT=$!
echo "PID_CAT = $PID_CAT"
send_mail &
wait # Wait for all
echo "Finished main"
可以看到以下输出:
PID_CAT = 1265
In run_cataloguer()
In send_mail()
In wait_for_cataloguer() PID_CAT = 1265
waitRet = 127 # THIS SHOULD be 0
Leaving send_mail()
leaving run_cataloguer()
Finished main
问题是 waitRet = 127 这意味着wait命令无法看到$ PID_CAT,因此它不会等待run_cataloguer()完成并且 "离开send_mail()" 以前打印过 "离开run_cataloguer()" 如果我在前台运行send_mail,那么waitRet = 0,这是正确的。
因此,您似乎无法在单独的后台进程中等待后台进程。 此外,如果我取消注释jobs命令,则不返回任何内容,这似乎确认了前一个语句。 如果有人有一个解决方案,除了使用标志文件,:)之外,我将非常感激。
答案 0 :(得分:0)
看起来无法做到这一点。我使用的解决方案来自Parvinder: wait child process but get error: 'pid is not a child of this shell'