元刷新(仅刷新浏览器)

Meta refresh (refresh only browser)

当我像下面那样"meta refresh"时,有人能告诉我吗,那还会运行 bash program-run-tmp-directory3.sh &> stdout.out &吗?或者,只有浏览器会刷新以保持 apache 活动?

"pid"是后台程序运行的"process ID"。

如果下面这段代码也重新运行s 程序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

谢谢。

此时,您的问题似乎是如何在 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