Keep Server Online
If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.
or
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.
| |
|
Topic: How to get cgi-bin to stop blocking on long processes? |
|
Author |
|
NoViS1
Joined: 14 Apr 2022 Posts: 2 Location: USA, San Diego
|
Posted: Thu 14 Apr '22 8:34 Post subject: How to get cgi-bin to stop blocking on long processes? |
|
|
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
Joined: 16 Aug 2020 Posts: 200 Location: UK
|
Posted: Thu 14 Apr '22 21:51 Post subject: |
|
|
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.
|
|
Back to top |
|
NoViS1
Joined: 14 Apr 2022 Posts: 2 Location: USA, San Diego
|
Posted: Fri 15 Apr '22 2:04 Post subject: disown -ah = no change |
|
|
Still works fine from the command line, still keeps chrome waiting till the script completes executing
Any other ideas?
Thanks,
Robert |
|
Back to top |
|
tangent
Joined: 16 Aug 2020 Posts: 200 Location: UK
|
Posted: Fri 15 Apr '22 20:50 Post subject: |
|
|
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: 7104 Location: Germany, Next to Hamburg
|
Posted: Tue 03 May '22 8:52 Post subject: |
|
|
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 |
|
|
|
|
|
|