Note that there are some explanatory texts on larger screens.

plurals
  1. POBash script hangs when called from php or python
    primarykey
    data
    text
    <p>I'm a novice at bash and I wrote a bash script that reads a large syslog file from end to start, looks for the entries that belong to a given minute (the minute before the last) and then counts how many ocurrences each of the given patterns has in that minute. It doesn't work if run from PHP/Python, but it works if I call it directly like this:</p> <p><code>sh /path/to/logparser.sh /path/to/big.log '2013-09-23T08:38' '2013-09-23T08:37' 'MySQL has gone away' 'Unhandled Error timed out'</code></p> <p>Below is the code of <code>logparser.sh</code>:</p> <pre><code>logfile=$1 echo $logfile shift minute=$1 echo $minute shift minute_before=$1 echo $minute_before shift command="tac $logfile | sed -n -e '/$minute/p' -e '/$minute_before/q'" echo $command if [ -f $logfile ]; then buffer=$(eval $command) echo "buffer complete" exit 1 fi </code></pre> <p>Side notes:</p> <ul> <li>I've used <code>buffer=$(eval $command)</code> because <code>buffer=$(tac $logfile | sed -n -e '/$minute/p' -e '/$minute_before/q')</code> hangs even in the command line</li> <li>I've used <code>-e '/$minute_before/q'</code> in sed because I couldn't get `-e '/$minute/!q' to work</li> </ul> <p>But when I run it from PHP via <code>pasthru()</code> or from Python via <code>subprocess.Popen().communicate()</code> it hangs. If I check the processes with <code>ps -ef r</code> I see that <code>tac</code> is still running.</p> <p>Here's the PHP code that calls the bash script:</p> <pre><code>$env = $argv[1]; $service_name = $argv[2]; $logfile = $argv[3]; $minute = $argv[4]; $minute_before = $argv[5]; $command = 'sh '.dirname(__FILE__).'/logparser.sh '; $n = count($argv); for($i=3; $i&lt;$n; $i++){ $command .= ' ' . escapeshellarg($argv[$i]); } $command .= "\n"; echo "\npassthru {$command}"; passthru($command, $out); var_dump($out); exit(); </code></pre> <p>Here's the Python code that calls the bash script:</p> <pre><code>env = sys.argv[1] service_name = sys.argv[2] logfile = sys.argv[3] minute = sys.argv[4] minute_before = sys.argv[5] args = ['sh', '%s/logparser.sh' % os.getcwd()] for i in range(3, len(sys.argv)): args.append(sys.argv[i]) print args output = subprocess.Popen(args, stdout=subprocess.PIPE).communicate() print output </code></pre> <p>Other stuff that I've rules out:</p> <ul> <li>exec permissions: both PHP and Python can run and get the results of linux commands like <code>whoami</code></li> <li>file permissions: both PHP and Python run perfectly for a simpler bash script, e.g. a file containing the <code>date</code> liunx command; the log file has read permissions for all</li> <li>bad params: I've compared in the bash script the params send by PHP/Python and the params sent via command line, they're identical</li> </ul> <p>How could I make it work called from PHP/Python?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload