简体   繁体   English

从另一个bash脚本内部选择性地打开和关闭bash脚本

[英]Selectively switch bash script on and off from inside another bash script

I have a Raspberry Pi running a bash script, eg, ScriptA, that calls a web function running on a cloud server and the function returns a value to the calling script. 我有一个运行bash脚本(例如ScriptA)的Raspberry Pi,该脚本调用在云服务器上运行的Web函数,并且该函数将值返回给调用脚本。 The cloud server contains a system where, visitors to the site are given a few options using buttons, eg, Button1, Button2 and Button3. 云服务器包含一个系统,在该系统中,使用按钮为站点的访问者提供了一些选项,例如Button1,Button2和Button3。 The option selected by the visitor is sent by the web function to the calling bash script on the Pi as Request1, Request2 or Request3 for Button1, Button and Button3 respectively. 访客选择的选项通过Web功能发送到Pi上的调用bash脚本,分别作为Button1,Button2和Button3的Request1,Request2或Request3。 The ScriptA keeps calling the web function recursively to check for any change in Request value. ScriptA继续递归调用Web函数,以检查Request值是否有任何更改。 The value returned by the function to the Pi is then used to run another script on the Pi, eg, Request1 runs Script1, Request2 runs Script2 and Request3 runs Script3. 该函数返回给Pi的值然后用于在Pi上运行另一个脚本,例如,Request1运行Script1,Request2运行Script2,Request3运行Script3。 Now, the webpage is dynamic and the visitor can click different options, one after the other, and the script running on the Pi also needs to change accordingly. 现在,网页是动态的,访问者可以一个接一个地单击不同的选项,并且在Pi上运行的脚本也需要相应地更改。


Button1 => Request1 |                              | Request1 => Script1
Button2 => Request2 | Cloud Server <=> Pi(ScriptA) | Request2 => Script2
Button3 => Request3 |                              | Request3 => Script3

What I have done is, I have used a switch case in the ScriptA to switch between the different scripts 1, 2 and 3. What I want is, as soon as the visitor to the site chooses a different option and the Pi receives the new request, the script for the previously selected option stops executing, and the script for the new request starts executing. 我要做的是,我在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

But, the problem with this piece of code is that, the first time the pi receives a request and starts executing the specific script for the incoming request, no more calls to the web function are made thereafter and the script continues executing the first request, regardless of whether the user has selected a new option or not. 但是,这段代码的问题在于,pi第一次收到请求并开始执行针对传入请求的特定脚本时,此后将不再对Web函数进行任何调用,并且脚本会继续执行第一个请求,无论用户是否选择了新选项。

How do I make it work as desired? 我如何使其按需工作? I hope I could frame my question well enough. 我希望我能很好地提出我的问题。

Help is much welcome. 非常欢迎帮助。 Thanks in advance. 提前致谢。

Assuming that: 假如说:

  1. we keep reading the new input and just execute the freshly gathered value, whether "Request1", "Reqest2" or "Request3" and get rid of the old one. 我们会继续读取新的输入,并只执行新收集的值,无论是“ Request1”,“ Reqest2”还是“ Request3”,并摆脱旧的值。
  2. You don't mind previous process to be killed rather than to be stopped. 您不介意之前的过程被杀死而不是被停止。

In addition to your approach, I have added the following: 除了您的方法外,我还添加了以下内容:

  1. A date,time print function, to demonstrate operation timing clearly. 日期,时间打印功能,清楚地显示操作时间。 This can be omitted later one once functionality has been verified. 一旦功能被验证,以后可以省略。

  2. A 'getpid' function, which freshly calculates the pid of the previous running script and passes on to the kill function 一个“ getpid”函数,可以重新计算先前运行的脚本的pid,然后传递给kill函数

  3. To demonstrate this, I will be tailing a file data.txt which is continuously growing as 3 backgroud echo commands are continuously putting Request 1,2,3 into it. 为了演示这一点,我将跟踪一个不断增长的文件data.txt,因为3个背景回显命令不断将请求1,2,3放入其中。 This will serve as our somewhat dynamic random input. 这将用作我们的动态随机输入。 A sample below. 下面的示例。

Sample: 样品:

%_STATION@gaurav * /root/ga/shell> tail -f data.txt
Request2
Request1
Request1
Request1
Request3
Request2
Request1

The script output: 脚本输出:

  1. The line with EMPTY represents when no PID was found. 带有EMPTY的行表示未找到PID的时间。 EMPTY
  2. This indicates a new request read from your file: [17-02-17 20:38:17|[>>>>NEW>>>REQUEST>> [Request3]>>>>] 这表示从您的文件中读取了一个新请求: [17-02-17 20:38:17|[>>>>NEW>>>REQUEST>> [Request3]>>>>]
  3. [gaurav 82577 1 0 20:38 pts/3 00:00:00 sleep 11] . [gaurav 82577 1 0 20:38 pts/3 00:00:00 sleep 11] This is the old running process, so I will kill it now. 这是旧的运行过程,所以我现在将其杀死。
  4. Its PID is captured: [OLDPID---->82577] 捕获其PID: [OLDPID---->82577]
  5. Once killed, a new one fired, and it is "Request3": [gaurav 82611 1 0 20:38 pts/3 00:00:00 sleep 13] 一旦被杀死,就会发射一个新的,它是“ Request3”: [gaurav 82611 1 0 20:38 pts/3 00:00:00 sleep 13]
  6. Now this keeps repeating. 现在,这一直在重复。 See the log below for more clarity. 请参阅下面的日志以更清楚。

A log excerpt: 日志摘录:

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]>>>>]
  1. As seen in the above log, the script is now able to act according to the incoming requests. 如上面的日志所示,脚本现在能够根据传入的请求进行操作。 In your case, you can replace the tail -f data.txt | while read line 根据您的情况,您可以替换tail -f data.txt | while read line tail -f data.txt | while read line line portion with while true; do tail -f data.txt | while read line部分, while true; do while true; do and then put your curl command. 然后while true; do curl命令。

The script: 剧本:

%_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>

I hope I got it right. 我希望我做对了。 Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM