在后台运行CGI Shell脚本 [英] Run a cgi shell script in background
问题描述
我有一个像这样的cgi shell脚本
I've a cgi shell script like this
#!/bin/sh
# show the page
echo Content-type: text/html
echo
echo "<html><b>Hello world!</b></html>"
# the task I want to do in background
sleep 100
echo $(date) >> log
我想在最后一个 echo $ c之后显示页面$ c>,而不是在脚本执行结束时。我试过将代码放在另一个文件中并像
./ background.sh&
这样执行。
I want to page to show after the last echo
, not at the end of the execution of the script. I've tried putting the code in another file and execute it like this ./background.sh &
. It works in console but not in the browser.
我使用lighttpd
I use lighttpd
推荐答案
使用 exec>关闭标准输出
;&-,然后再执行长时间运行的任务或后台任务。也许您还必须关闭stderr。
Close stdout with exec >&-
before you execute your long-running or background task. Maybe you'll also have to close stderr. That would be
exec >&-
exec 2>&-
(似乎无法同时关闭两者)
(it seems you can't close both in one line)
这将中断管道,并使您的Web服务器将已经输出的数据(例如通过 echo
)发送到浏览器。
This will break the pipe and allow your web server to send the data already outputted (such as via echo
) to the browser.
这篇关于在后台运行CGI Shell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!