Question

I created a CGI in C++. This program calls a shell script script.sh this way

system("script.sh")

I don't know why but when I use the CGI the web page displays all the output of the script. What can I do to avoid that? I inserted an echo off but it doesn't work and I still see all the output. Is there a way to disable that? I am using LINUX Red Hat 6.2 and uses Mongoose as web server.

Was it helpful?

Solution

Typically, it's good to use something like system("script.sh >> log.txt 2>&1") to capture all normal and error output and redirect it into log.txt. Otherwise this output is directed to STDOUT and STDERR, and STDOUT is the output of your CGI to the HTTP stream.

Having this in place, it may also improve your script to have an input that enables or disables this logging, something like &logging=1 amongst the CGI parameters and then you would have something like the following to control the output:

if (logging==1) {
  system("script.sh >> log.txt 2>&1");
}
else {
  system("script.sh >> /dev/null 2>&1");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top