Suppose that I want my server to sleep on any HTTP request to the path /sleep (i.e. http://hostname/sleep), but also send a complete response (HTTP 200) before sleeping.
Using nginx with FastCGI, I have configured the path in the nginx configuration:
location /sleep {
fastcgi_pass unix:/path/to/fcgiwrap.socket;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /path/to/script.sh;
}
This invokes the following script (/path/to/script.sh):
#!/bin/sh
# -*- coding: utf-8 -*-
cat << EOF
Content-Type: text/html
<html><head><title>Sleep</title><meta charset="UTF-8"></head>
<body><p>Sleep: $(date)</p></body></html>
EOF
sleep 1 && sudo systemctl suspend < /dev/null > /dev/null 2>&1 &
Even though this will sleep the system, FastCGI will block until the child process completes, meaning that the web request will not complete before the system sleeps. So, how do I detach that final command from oversight by FastCGI so that the response completes, yet the command continues in the background, in this case ultimately to sleep the system?
sleep 5in the script, but kill the request after 2 seconds).