logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in RSS X


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
Post new topic   Forum Index -> Coding & Scripting Corner View previous topic :: View next topic
Reply to topic   Topic: How to get cgi-bin to stop blocking on long processes?
Author
NoViS1



Joined: 14 Apr 2022
Posts: 2
Location: USA, San Diego

PostPosted: Thu 14 Apr '22 8:34    Post subject: How to get cgi-bin to stop blocking on long processes? Reply with quote

I have a series of scripts to open my garage door, one script I press while I'm in my house and waits 60 seconds before opening the garage door so I have time to lock the door and get to the car. While my bash scripts run instantly on the command line, when executed as cgi-bin scripts they are blocking until the sleep function finishes instead of forking off the child to sleep and returning back to the main control page. The code I'm having trouble long block is this:


<OpenGarageIn60SecondsHTML.sh>
Code:
#!/bin/bash

/root/Garage/Office_60s.py 2> /dev/null &
/root/Garage/patio_60s.py 2> /dev/null &
/root/Garage/keypad_arduino_chirp.py 2> /dev/null &
/root/Garage/bedroom_arduino_buzz.py 2> /dev/null &

echo "Content-type: text/html"
echo ""
echo "<HEAD>"
echo "<META HTTP-EQUIV=\"refresh\" CONTENT=\"1; url=http://192.168.12.224/html/index.php\">"
# echo "<META HTTP-EQUIV=\"refresh\" CONTENT=\"1; url=http://192.168.12.224/cgi-bin/OpenIn60InterstitialHTML.sh\">"
echo "</HEAD>"
echo ""
echo ""

/usr/lib/cgi-bin/OpenGarageIn60Seconds.sh 2> /dev/null &


So I'm looking for help to get my script to stop blocking on the long sleeping child process in the last line...
Back to top
tangent
Moderator


Joined: 16 Aug 2020
Posts: 346
Location: UK

PostPosted: Thu 14 Apr '22 21:51    Post subject: Reply with quote

The various processes you background in your script are still owned by that shell, so the shell won't exit normally until they do.

To get round this you need to detach them with the bash 'disown' builtin command.

Try adding this as the last command in your script.

Code:
disown -ah
Back to top
NoViS1



Joined: 14 Apr 2022
Posts: 2
Location: USA, San Diego

PostPosted: Fri 15 Apr '22 2:04    Post subject: disown -ah = no change Reply with quote

Still works fine from the command line, still keeps chrome waiting till the script completes executing Sad

Any other ideas?

Thanks,
Robert
Back to top
tangent
Moderator


Joined: 16 Aug 2020
Posts: 346
Location: UK

PostPosted: Fri 15 Apr '22 20:50    Post subject: Reply with quote

Your solution is interesting to say the least, since you're running various Python / shell scripts as well as calling a PHP script in the HTML response.

If it's not doing what you want, I'd be tempted to try a different strategy and decouple the various Python and shell script tasks from Apache CGI, by using a semaphore file of some sort.

So, consider developing a background script which has a loop with a timed delay (say 1 second), and each loop check for the presence of a specific semaphore file. If said file exists, delete it, and then run your various Python and shell scipts.

In the Apache CGI script, apart from creating your HTML response, simply create the semaphore file.

If this approach works, then you can tighten security (owners / permissions), and launch your background script from cron on a reboot.
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7370
Location: Germany, Next to Hamburg

PostPosted: Tue 03 May '22 8:52    Post subject: Reply with quote

It might be an option to simply create a text file with a certain value in it. Then a cronjob every minute looks for the file, does the job, and deletes that file. That way you don't have to wait.
Back to top
ritzmax72



Joined: 01 Nov 2022
Posts: 1
Location: India,Bangalore

PostPosted: Tue 01 Nov '22 15:05    Post subject: Re: How to get cgi-bin to stop blocking on long processes? Reply with quote

NoViS1 wrote:
I have a series of scripts to open my garage door, one script I press while I'm in my house and waits 60 seconds before opening the garage door so I have time to lock the door and get to the car. While my bash scripts run instantly on the command line, when executed as cgi-bin scripts they are blocking until the sleep function finishes instead of forking off the child to sleep and returning back to the main control page. The code I'm having trouble long block is this:


<OpenGarageIn60SecondsHTML.sh>
Code:
#!/bin/bash

/root/Garage/Office_60s.py 2> /dev/null &
/root/Garage/patio_60s.py 2> /dev/null &
/root/Garage/keypad_arduino_chirp.py 2> /dev/null &
/root/Garage/bedroom_arduino_buzz.py 2> /dev/null &

echo "Content-type: text/html"
echo ""
echo "<HEAD>"
echo "<META HTTP-EQUIV=\"refresh\" CONTENT=\"1; url=http://192.168.12.224/html/index.php\">"
# echo "<META HTTP-EQUIV=\"refresh\" CONTENT=\"1; url=http://192.168.12.224/cgi-bin/OpenIn60InterstitialHTML.sh\">"
echo "</HEAD>"
echo ""
echo ""

/usr/lib/cgi-bin/OpenGarageIn60Seconds.sh 2> /dev/null &


So I'm looking for help to get my script to stop blocking on the long sleeping child process in the last line...




The way cgi has worked out for me is that cgi scripts are run once per request. Plus, cgi script has to terminate because apache always wait for it. Think of apache code calling execv and doing waitpid on cgi script while grabbing its stdout file descriptor.

Even if you spawn a new process from cgi script the grant parent is apache itself which is waiting for all of its grandchildren to terminate. The worse part of this problem is how little information and guides are available on the internet.

Fastcgi might help you which are basically independent processes from apache. Apache don’t have to wait for them to terminate. You’d loop infinitely inside fasgcgi and whenever an api like Fastcgi_Accept() is called you get the user input, process it and don’t even have to explicitly send because fastcgi_accept on second call would deliver all your stdout results. The way it works is through unix sockets. Fastcgi programs and apache are run at starup so there is no link between them.

Still, this domain is dark. Very less or confusing information. How does big companies like google and amazon process cgis? Do they have their own framework? Then should not internet be filled with information about developing a cgi framework from scratch? If you try to make simple background processing of images how would you go about because processing of image would take lot of time.
Back to top


Reply to topic   Topic: How to get cgi-bin to stop blocking on long processes? View previous topic :: View next topic
Post new topic   Forum Index -> Coding & Scripting Corner