用于生成守护进程的CGI Bash脚本

时间:2012-08-28 09:32:41

标签: html apache bash cgi daemon

我正在开展一个项目,将HDTV从个人计算机流式传输到支持HTTP直播流的设备(想想iOS设备和一些Android)。我有视频格式和流媒体方面。我现在试图实现的是一种远程更改频道的简便方法。

我当前的方法涉及通过SSH连接以终止旧流并开始新流。这有效,但并不漂亮。我想要妈妈或女朋友可以使用的东西。我决定构建一个HTML5应用程序,它将通过CGI脚本发出频道切换。我目前有一个父进程,其中一个表单调用子进程来决定流是否正在运行,然后是子进程来实际调整流。

当我从我的计算机上直播视频时,我需要子进程无限期地运行。 不幸的是,当我的父进程完成后,子进程中启动的后台进程终止。

我尝试过使用nohup,setsid和守护进程的简单&和。守护进程运行最干净但在父进程完成时仍然终止。即使有-r标志。我将我的代码放在下面,也许有人会知道如何实现这个或更好的方法来实现同样的事情?谢谢! (哦,我知道杀死vlc并不是一种杀死流的好方法,如果你有更好的方式我都是耳朵)

parent invoking child:
----------------------
./ChangeChannel.sh $channel     #passed from form submission


child (ChangeChannel.sh):
-------------------------
#!/bin/bash

directory=./Channels/
newchannel=$1

if [ $(pidof vlc) ]
    then
        sudo kill $(pidof vlc)
fi
daemon -r -v -d $directory$newchannel &


subchild example:
-----------------
vlc atsc://frequency=605029000 --intf=dummy --sout-transcode-audio-sync :live-cache=3000 --sout='#transcode{vcodec=h264,vb=150,fps=25,width=480,scale=1,venc=x264{aud,profile=baseline,level=30,keyint=15,bframes=0,ref=1},acodec=aac,ab=40,channels=2,samplerate=22050}:duplicate{dst=std{mux=ts,dst=-,access=livehttp{seglen=16,delsegs=true,numsegs=10,index=/var/www/stream/live.m3u8,index-url=content/live-######.ts},mux=ts{use-key-frames},dst=/var/www/stream/content/live-######.ts,ratecontrol=true}}'

如何让子孙终止?在Ubuntu 12.04上运行Apache

1 个答案:

答案 0 :(得分:1)

我明白了!

对于任何对如何感兴趣的人,我改变了我的策略,使用nohup,&,disown和> / dev / null 2>& 1。

老实说,仍然不太确定我是如何运作的...只是经过一些有根据的猜测而进行了大量的试验和错误。我的代码如下:

parent invocation:
------------------
nohup ./ChangeChannel.sh $channel & disown


child invocation:
-----------------
sudo nohup su user $directory$newchannel &> /dev/null 2>&1


subchild invocation:
--------------------
vlc atsc://frequency=605029000 --intf=dummy --sout-transcode-audio-sync :live-cache=3000 --sout='#transcode{vcodec=h264,vb=150,fps=25,width=480,scale=1,venc=x264{aud,profile=baseline,level=30,keyint=15,bframes=0,ref=1},acodec=aac,ab=40,channels=2,samplerate=22050}:duplicate{dst=std{mux=ts,dst=-,access=livehttp{seglen=16,delsegs=true,numsegs=10,index=/var/www/stream/live.m3u8,index-url=content/live-######.ts},mux=ts{use-key-frames},dst=/var/www/stream/content/live-######.ts,ratecontrol=true}}' & disown

ChangeChannel.sh使用sudo通过cgi执行su,以便以非root用户身份执行vlc。这看起来有点乱,但它确实有效。