Unable to run a bash script from apache2 server using python3?
<p>I have two Ubuntu PCs set up and connected as shown, they are able to ping each other.</p>

<pre><code>PC1&lt;========================&gt;PC2
</code></pre>

<p>PC2 has apache2 running on it. I have set up a python script as shown below to be run when PC1 makes a request to server using PC2's IP.</p>

<p>The Python script works fine and generates a simple web page as indicated in it, but the moment I uncomment the line </p>

<pre><code>subprocess.call("./test.sh", shell=True)
</code></pre>

<p>of code to run a bash script, two things happen:</p>

<ol>
<li><p>When I run <code>./index.py</code> locally on PC2, it works and also executes the bash script file <code>./test.sh</code> as indicated in it and hence also executes the <code>top</code> command mentioned in the <code>./test.sh</code></p>

<p>but...</p></li>
<li><p>When I request this through PC1, the apache2 crashes and gives the following in browser:</p>

<blockquote>
  <p>Internal Server Error</p>
  
  <p>The server encountered an internal error or misconfiguration and was unable to complete your request.</p>
  
  <p>Please contact the server administrator at webmaster@localhost to inform them of the time this error occurred, and the actions you performed just before this error.</p>
  
  <p>More information about this error may be available in the server log.</p>
  
  <p>Apache/2.4.7 (Ubuntu) Server at localhost Port 80</p>
</blockquote></li>
</ol>

<p>The error log file has following detail:</p>

<pre><code>[Wed Feb 10 21:40:51.866832 2016] [cgi:error] [pid 9747] [client   

127.0.0.1:34506] malformed header from script 'index.py': Bad header:   
THIS COMES FROM TEST
[Wed Feb 10 21:40:51.867920 2016] [cgi:error] [pid 9747] [client  
127.0.0.1:34506] AH01215: TERM environment variable not set.
</code></pre>

<p>The Python script <code>./index.py</code> is as follows:</p>

<pre><code>#!/usr/bin/python

import subprocess
subprocess.call("./test.sh", shell=True)

# Turn on debug mode.
import cgitb
cgitb.enable()

# Print necessary headers.
print("Content-Type: text/html\n")




print('&lt;html&gt;')
print('&lt;head&gt;')
print('&lt;title&gt;IoT Based Electric SubStation Monitoring&lt;/title&gt;')
print('&lt;/head&gt;')
print('&lt;body&gt;')
print('&lt;/br&gt;')
print('&lt;marquee&gt;&lt;b&gt;&lt;h1&gt;IoT Based Electric SubStation Monitoring&lt;/h1&gt; 
&lt;/b&gt;&lt;/marquee&gt;')

print(' &lt;button type="button"&gt;Click here to STOP&lt;/button&gt; ')
print('&lt;/body&gt;')
print('&lt;/html&gt;')
</code></pre>

<p>The bash script <code>./test.sh</code> is as follows:</p>

<pre><code>#!/bin/bash
#echo THIS COMES FROM TEST
firefox
#echo STOPPPPPPPPPPPPPPPPPPPP
</code></pre>