![](/img/trans.png)
[英]Using bash script output with periodic output selectively to trigger another script
[英]Selectively switch bash script on and off from inside another bash script
我有一個運行bash腳本(例如ScriptA)的Raspberry Pi,該腳本調用在雲服務器上運行的Web函數,並且該函數將值返回給調用腳本。 雲服務器包含一個系統,在該系統中,使用按鈕為站點的訪問者提供了一些選項,例如Button1,Button2和Button3。 訪客選擇的選項通過Web功能發送到Pi上的調用bash腳本,分別作為Button1,Button2和Button3的Request1,Request2或Request3。 ScriptA繼續遞歸調用Web函數,以檢查Request值是否有任何更改。 該函數返回給Pi的值然后用於在Pi上運行另一個腳本,例如,Request1運行Script1,Request2運行Script2,Request3運行Script3。 現在,網頁是動態的,訪問者可以一個接一個地單擊不同的選項,並且在Pi上運行的腳本也需要相應地更改。
Button1 => Request1 | | Request1 => Script1
Button2 => Request2 | Cloud Server <=> Pi(ScriptA) | Request2 => Script2
Button3 => Request3 | | Request3 => Script3
我要做的是,我在ScriptA中使用了一個切換用例,可以在不同的腳本1、2和3之間進行切換。我想要的是,只要站點的訪客選擇了不同的選項並且Pi收到了新的請求,則先前選擇的選項的腳本將停止執行,而新請求的腳本將開始執行。
#!/bin/bash
# call.sh
while:
do
req=$(curl -d "param=$param" http://www.example.net/req.php)
case $req in
*req1*)
sudo sh /home/pi/stopscript2.sh
sudo sh /home/pi/stopscript3.sh
sudo sh /home/pi/startscript1.sh
;;
*req2*)
sudo sh /home/pi/stopscript1.sh
sudo sh /home/pi/stopscript3.sh
sudo sh /home/pi/startscript2.sh
;;
*req3*)
sudo sh /home/pi/stopscript1.sh
sudo sh /home/pi/stopscript2.sh
sudo sh /home/pi/startscript3.sh
;;
esac
done
但是,這段代碼的問題在於,pi第一次收到請求並開始執行針對傳入請求的特定腳本時,此后將不再對Web函數進行任何調用,並且腳本會繼續執行第一個請求,無論用戶是否選擇了新選項。
我如何使其按需工作? 我希望我能很好地提出我的問題。
非常歡迎幫助。 提前致謝。
假如說:
除了您的方法外,我還添加了以下內容:
日期,時間打印功能,清楚地顯示操作時間。 一旦功能被驗證,以后可以省略。
一個“ getpid”函數,可以重新計算先前運行的腳本的pid,然后傳遞給kill函數
為了演示這一點,我將跟蹤一個不斷增長的文件data.txt,因為3個背景回顯命令不斷將請求1,2,3放入其中。 這將用作我們的動態隨機輸入。 下面的示例。
樣品:
%_STATION@gaurav * /root/ga/shell> tail -f data.txt
Request2
Request1
Request1
Request1
Request3
Request2
Request1
腳本輸出:
EMPTY
[17-02-17 20:38:17|[>>>>NEW>>>REQUEST>> [Request3]>>>>]
[gaurav 82577 1 0 20:38 pts/3 00:00:00 sleep 11]
。 這是舊的運行過程,所以我現在將其殺死。 [OLDPID---->82577]
[gaurav 82611 1 0 20:38 pts/3 00:00:00 sleep 13]
日志摘錄:
17-02-17 20:38:16|EMPTY
17-02-17 20:38:16|[***NEWPROCESS***[82504]*****]
17-02-17 20:38:16|[>>>>NEW>>>REQUEST>> [Request1]>>>>]
17-02-17 20:38:16|gaurav 82504 1 0 20:38 pts/3 00:00:00 sleep 12
17-02-17 20:38:16|[OLDPID---->82504]
17-02-17 20:38:16|gaurav 82543 1 0 20:38 pts/3 00:00:00 sleep 11
17-02-17 20:38:16|[***NEWPROCESS***[82543]*****]
17-02-17 20:38:16|[>>>>NEW>>>REQUEST>> [Request1]>>>>]
17-02-17 20:38:16|gaurav 82543 1 0 20:38 pts/3 00:00:00 sleep 11
17-02-17 20:38:16|[OLDPID---->82543]
17-02-17 20:38:17|gaurav 82577 1 0 20:38 pts/3 00:00:00 sleep 11
17-02-17 20:38:17|[***NEWPROCESS***[82577]*****]
17-02-17 20:38:17|[>>>>NEW>>>REQUEST>> [Request3]>>>>]
17-02-17 20:38:17|gaurav 82577 1 0 20:38 pts/3 00:00:00 sleep 11
17-02-17 20:38:17|[OLDPID---->82577]
17-02-17 20:38:17|gaurav 82611 1 0 20:38 pts/3 00:00:00 sleep 13
17-02-17 20:38:17|[***NEWPROCESS***[82611]*****]
17-02-17 20:38:17|[>>>>NEW>>>REQUEST>> [Request2]>>>>]
17-02-17 20:38:17|gaurav 82611 1 0 20:38 pts/3 00:00:00 sleep 13
17-02-17 20:38:17|[OLDPID---->82611]
17-02-17 20:38:17|EMPTY
17-02-17 20:38:17|[***NEWPROCESS***[82664]*****]
17-02-17 20:38:17|[>>>>NEW>>>REQUEST>> [Request1]>>>>]
17-02-17 20:38:17|gaurav 82664 1 0 20:38 pts/3 00:00:00 sleep 12
17-02-17 20:38:17|[OLDPID---->82664]
17-02-17 20:38:17|gaurav 82699 1 0 20:38 pts/3 00:00:00 sleep 11
17-02-17 20:38:17|[***NEWPROCESS***[82699]*****]
17-02-17 20:38:17|[>>>>NEW>>>REQUEST>> [Request1]>>>>]
17-02-17 20:38:17|gaurav 82699 1 0 20:38 pts/3 00:00:00 sleep 11
17-02-17 20:38:17|[OLDPID---->82699]
17-02-17 20:38:18|gaurav 82733 1 0 20:38 pts/3 00:00:00 sleep 11
17-02-17 20:38:18|[***NEWPROCESS***[82733]*****]
17-02-17 20:38:18|[>>>>NEW>>>REQUEST>> [Request3]>>>>]
17-02-17 20:38:18|gaurav 82733 1 0 20:38 pts/3 00:00:00 sleep 11
17-02-17 20:38:18|[OLDPID---->82733]
17-02-17 20:38:18|gaurav 82767 1 0 20:38 pts/3 00:00:00 sleep 13
17-02-17 20:38:18|[***NEWPROCESS***[82767]*****]
17-02-17 20:38:18|[>>>>NEW>>>REQUEST>> [Request2]>>>>]
tail -f data.txt | while read line
tail -f data.txt | while read line
部分, while true; do
然后while true; do
curl命令。 劇本:
%_STATION@gaurav * /root/ga/shell> cat request_action.sh
#!/bin/bash
# configure your scirpts. Here I used Sleeps.
s1='sleep 11' ; s2='sleep 12' ; s3='sleep 13'
# DateTime and print function.
prnt()
{
str="$1"
dt=$(date +"%d-%m-%y "%T)
[[ -z "$str" ]] && str="EMPTY"
echo "$dt|$str"
}
# Function to generate PID of previous running script.
getpid()
{
pid=$(ps -ef|egrep "$s1|$s2|$s3"|grep -v grep|awk '{print $2}')
echo "$pid"
}
# To kill the existing running script/s.
stopnstart()
{
prnt "$(ps -ef|egrep "$s1|$s2|$s3"|grep -v grep)"
prnt "[OLDPID---->$(getpid)]"
$(kill -9 $(getpid) 1>/dev/null 2>&1) # Kill the old PID.
$($1 1>/dev/null 2>&1 &) # Now run the fresh script.
prnt "$(ps -ef|egrep "$s1|$s1|$s3"|grep -v grep)"
prnt "[***NEWPROCESS***[$(getpid)]*****]"
}
# Main loop
tail -f data.txt | while read line
do
prnt "[>>>>NEW>>>REQUEST>> [$line]>>>>]"
case $line in
Request1)
stopnstart "$s1"
;;
Request2)
stopnstart "$s2"
;;
Request3)
stopnstart "$s3"
;;
esac
done
%_STATION@gaurav * /root/ga/shell>
我希望我做對了。 謝謝。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.