0

Can someone please tell me when I do "meta refresh" like below,will that also run bash program-run-tmp-directory3.sh &> stdout.out & again? OR, only the browser will be refreshed keeping the apache alive?

"pid" is the "process ID" of the program run in the background.

If this code below also reruns the program bash program-run-tmp-directory3.sh &> stdout.out &. Please let me know how can I avoid it?

#!/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

Thanks.

agc
  • 7,973
  • 2
  • 29
  • 50
DanielSebas
  • 135
  • 6
  • Please put more efforts in formatting code in your question. You need four spaces in front of code lines, not a dozen of them – Basile Starynkevitch Oct 01 '17 at 07:26
  • I assume you have configured your server to execute this script whenever it receives a request for a particular URL. In that case, yes, the script will be re-run (including the embedded call to `bash`) every time a request for that URL is made. Your meta refresh asks the browser to re-fetch the page every 10 seconds. If the browser obliges, then you will be asking `bash` to execute `program-run-tmp-directory3.sh` every 10 seconds. – Ron Burk Oct 04 '17 at 04:05

1 Answers1

0

At this point, it seems like your question is how to detect within a bash script that another script is running and avoid respawning it. A quick and dirty method that is often good enough is to grep the output of ps for the command-line of your script. This is slightly complicated by the fact that, depending on the options you use for ps, it may also display the grep process command-line, which obviously also includes the script's command-line as part of the grep pattern. One of many ways to fix this is here. All this explanation is longer than the actual script.

One more note. Just want to make sure you understand what the bash construct $! means:

($!) 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). 

So, that's only going to refer to things that the current execution of your CGI script knows about. When your browser decides to refresh again, that sends another HTTP GET to your server, which once again spawns your CGI script, at which point $! only refers to the job most recently spawned by that instance of your script.

If I intuit what you're trying to do, you might want something like this (untested):

#!/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
Ron Burk
  • 6,058
  • 1
  • 18
  • 20