Strange behaviour with CGI file

2019-09-06 07:38发布

问题:

I've a small project allmost finished. In it, I use some .cgi files in which I make communication between a microcontroler and a webserver. The problem is the following:

If I use this code the project works fine:

#!/bin/bash

./moveRight

echo "Status: 204 No Content"
echo "Content-type: text/html"
echo ""

but when I use tjis code, nothing happens:

#!/bin/bash

mkdir /tmp/stream
raspistill --nopreview -w 640 -h 480 -q 5 -o /tmp/stream/pic.jpg -tl 100 -t 9999999 -th      0:0:0 &
chmod 777 /dev/ttyAMA0
LD_LIBRARY_PATH=/usr/local/lib mjpg_streamer -i "input_file.so -f /tmp/stream -n pic.jpg" -o "output_http.so -p 8080 -w /var/www" &


echo "Status: 204 No Content"
echo "Content-type: text/plain"
echo ""

What I want is to execute those bash commands when the .cgi is called, do you think what can be my problem? Or any workaround for this issue?

EDIT: ./moveRight is a compiled c program which moves a motor. I have two different .cgi. The first one moves the motor correctly but the second one is supposed to execute some shell commands but when it's called, nothing happens.

Thank you!!

回答1:

Try exec 2> /tmp/my.log as first command in your script. It redirects stderr to a file, so you can view it later, and then continues with executing the script with that redirection installed.

Place echo "before mkdir" 1>&2 etc. between the commands. This way you will see in the log how far you get in your script. Probably you just have a command in the script which does not terminate. Then your web browser will hang until a timeout occurs.



回答2:

Try to print the HTTP header first and execute your command after:

#!/bin/bash

echo "Status: 204 No Content"
echo "Content-type: text/plain"
echo ""

mkdir /tmp/stream
raspistill --nopreview -w 640 -h 480 -q 5 -o /tmp/stream/pic.jpg -tl 100 -t 9999999 -th      0:0:0 &
chmod 777 /dev/ttyAMA0
LD_LIBRARY_PATH=/usr/local/lib mjpg_streamer -i "input_file.so -f /tmp/stream -n pic.jpg" -o "output_http.so -p 8080 -w /var/www" &

Indeed, if the shell commands are printing something on stdout, you will have a status 500 because your HTTP header is not correct.