我正在尝试编写一个bash脚本来做一些事情,启动一个进程,等待该进程说它准备就绪,然后在该进程继续运行时再做更多的事情.我遇到的问题是找到一种方法,在继续之前等待该过程准备就绪,并允许它继续运行.
在我的具体情况下,我正在尝试建立PPP连接.在运行下一个命令之前,我需要等到它已连接.如果PPP无法连接,我还想停止脚本.pppd打印到stdout.
在psuedo代码中我想要做的是:
[some stuff]
echo START
[set up the ppp connection]
pppd <options> /dev/ttyUSB0
while 1
if output of pppd contains "Script /etc/ppp/ipv6-up finished (pid ####), status = 0x0"
break
if output of pppd contains "Sending requests timed out"
exit 1
[more stuff, and pppd continues to run]
echo CONTINUING
Run Code Online (Sandbox Code Playgroud)
关于如何做到这一点的任何想法?
Ivi*_*vin 16
我必须做类似的事情,等待/ var/log/syslog中的一行出现.这对我有用:
FILE_TO_WATCH=/var/log/syslog
SEARCH_PATTERN='file system mounted'
tail -f -n0 ${FILE_TO_WATCH} | grep -qe ${SEARCH_PATTERN}
if [ $? == 1 ]; then
echo "Search terminated without finding the pattern"
fi
Run Code Online (Sandbox Code Playgroud)
它会将附加到监视文件的所有新行管道为grep,并指示grep在发现模式后立即安静地退出.以下if语句检测"wait"是否在没有找到模式的情况下终止.
我想出的最快解决方案是在后台使用 nohup 运行 pppd 并检查 nobup.out 文件中的 stdout。结果是这样的:
sudo nohup pppd [options] 2> /dev/null &
# check to see if it started correctly
PPP_RESULT="unknown"
while true; do
if [[ $PPP_RESULT != "unknown" ]]; then
break
fi
sleep 1
# read in the file containing the std out of the pppd command
# and look for the lines that tell us what happened
while read line; do
if [[ $line == Script\ /etc/ppp/ipv6-up\ finished* ]]; then
echo "pppd has been successfully started"
PPP_RESULT="success"
break
elif [[ $line == LCP:\ timeout\ sending\ Config-Requests ]]; then
echo "pppd was unable to connect"
PPP_RESULT="failed"
break
elif [[ $line == *is\ locked\ by\ pid* ]]; then
echo "pppd is already running and has locked the serial port."
PPP_RESULT="running"
break;
fi
done < <( sudo cat ./nohup.out )
done
Run Code Online (Sandbox Code Playgroud)
有一个名为“Expect”的工具几乎可以完全满足您的需求。更多信息: http: //en.wikipedia.org/wiki/Expect
您还可以查看“chat”的手册页,这是一个 pppd 功能,可以完成一些预期可以完成的操作。
| 归档时间: |
|
| 查看次数: |
21472 次 |
| 最近记录: |