2

I'm trying to redirect the contents of command output to a file in a CGI program. The issue for me is that the output file is not getting created.

The CGI Code snippet is as follows:

#!/bin/bash

echo "Content-type: text/html"
echo ""

echo "<html>"
echo "<body>"

`mysql -u root -predhat -H  -D mysql -e "select host,db from  db;" > output.txt`

 echo "</body>"
 echo "</html>" 

The CGI script is getting called from an HTML program. Also,I can use INTO OUTFILE with a mysql command, but I want that output redirection should work for any command.

Ownerships are as follows:

  #ls -l /var/www/cgi-bin/example.sh
  -rwxr-xr-x 1 root root 839 Apr 11 21:34 /var/www/cgi-bin/example.sh

# ps -ef | grep httpd root 2442 1 0 19:30 ? 00:00:00 /usr/sbin/httpd apache 2464 2442 0 19:30 ? 00:00:00 /usr/sbin/httpd apache 2465 2442 0 19:30 ? 00:00:00 /usr/sbin/httpd apache 2466 2442 0 19:30 ? 00:00:00 /usr/sbin/httpd apache 2467 2442 0 19:30 ? 00:00:00 /usr/sbin/httpd apache 2468 2442 0 19:30 ? 00:00:00 /usr/sbin/httpd

How can I resolve the issue?

1
  • What does your apache error log say?
    – Jenny D
    Apr 11, 2014 at 10:12

1 Answer 1

1

First off is this script setup so that the user that is running your web server (Apache I'm guessing) can execute it? You can check by looking at the permissions of the script:

$ ls -l somescript.cgi
-rwxrwxr-x. 1 apache apache 1199 Jan 17 20:47 somescript.cgi

You'll want the permissions such that anyone can run this or if the script is owned by the user apache then at least just the owner can execute it.

If that's the case then I would next focus on the location of the output from the mysql command. Apache likely cannot write to the location where mysql is attempting to write the file output. I'd make that an actual physical location like this:

mysql -u root -predhat -H  -D mysql -e "select host,db from  db;" \
    > /tmp/output.txt

Also the backticks are unnecessary, so you can take them out.

2
  • Changing the output location to /tmp directory resolves the issue .
    – Zama Ques
    Apr 11, 2014 at 10:59
  • @ZamaQues - great. Glad that resolved it.
    – slm
    Apr 11, 2014 at 11:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .