元刷新(仅刷新浏览器)

时间:2017-10-01 07:24:51

标签: bash shell cgi meta

有人可以在我做的时候告诉我" meta refresh"如下所示,那还会再次运行bash program-run-tmp-directory3.sh &> stdout.out &吗?或者,只有浏览器会刷新,apache还活着吗?

" PID "是"进程ID"该程序在后台运行。

如果以下代码也重新运行程序bash program-run-tmp-directory3.sh &> stdout.out &。请让我知道如何避免它?

#!/bin/sh
echo "Content-type: text/html"
echo ""
bash program-run-tmp-directory3.sh &> stdout.out &
pid=$!

if [[ `ps -p $pid | wc -l` -gt 1 ]]
then
    output="Program is running. Running time depends on the number of alternatively spliced proteins the submitted gene has. Results will be displayed here."
    echo "<html>"
    echo "<head>"
    echo "<meta http-equiv=\"refresh\" content=\"10\"/>"
    echo "</head>"
    echo "<body>"
    echo "<table width=\"750\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">"
    echo "<tr><td><img src=\"../../images/calc.gif\" align=\"absmiddle\"> <strong> $output </strong></td></tr>"
    echo "</table>"
    echo "</body>"
    echo "</html>"
fi

感谢。

1 个答案:

答案 0 :(得分:0)

此时,您的问题似乎是如何在bash脚本中检测到另一个脚本正在运行并避免重新生成它。一个通常足够好的快速而简单的方法是grep脚本命令行的ps输出。这有点复杂,因为根据您用于ps的选项,它还可能显示grep进程命令行,显然还包括脚本的命令行作为grep模式的一部分。解决这个问题的方法之一是here。所有这些解释都比实际脚本长。

还有一点需要注意。只是想确保您了解bash构建$!的含义:

($!) Expands to the process ID of the job most recently placed into
the background, whether executed as an asynchronous command or using
the bg builtin (see Job Control Builtins). 

所以,这只会引用你的CGI脚本的当前执行所知道的事情。当您的浏览器决定再次刷新时,会向您的服务器发送另一个HTTP GET,这会再次产生您的CGI脚本,此时$!仅引用最近由该实例你的剧本。

如果我想知道你正在尝试做什么,你可能会想要这样的事情(未经测试):

#!/bin/sh
echo "Content-type: text/html"
echo ""
# if other script not already running
if ! ps aux | grep "[b]ash.*program-run-tmp-directory3.sh"
    then
    bash program-run-tmp-directory3.sh &> stdout.out &
    # I'm superstitious; let's give it a moment to start
    sleep 1
    fi

if ps aux | grep "[b]ash.*program-run-tmp-directory3.sh"
then
    output="Program is running. Running time depends on the number of alternatively spliced proteins the submitted gene has. Results will be displayed here."
    echo "<html>"
    echo "<head>"
    echo "<meta http-equiv=\"refresh\" content=\"10\"/>"
    echo "</head>"
    echo "<body>"
    echo "<table width=\"750\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">"
    echo "<tr><td><img src=\"../../images/calc.gif\" align=\"absmiddle\"> <strong> $output </strong></td></tr>"
    echo "</table>"
    echo "</body>"
    echo "</html>"
else
    : # ... some useful error output composed as HTML
fi