LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 2230|回复: 17

请问如何判断一个进程是自动退出的还是人为终止的?

[复制链接]
发表于 2008-2-29 22:22:43 | 显示全部楼层 |阅读模式
我在写一个用mplayer连续播放文件的shell,
问题是如何让shell区分
1, mplayer播放到文件结尾自动退出
2, 用户按q或esc退出程序
两种情况(都属于正常退出)?

请各位高手给点建议
thx!
发表于 2008-3-1 01:15:39 | 显示全部楼层
exitstatus:
q/esc键thunder@thunder:~/shellprogram$ mplayer kan*.mp3
MPlayer 1.0rc2-4.1.2 (C) 2000-2007 MPlayer Team
CPU: AMD Sempron(tm) Processor 2500+ (Family: 15, Model: 44, Stepping: 2)
CPUflags:  MMX: 1 MMX2: 1 3DNow: 1 3DNow2: 1 SSE: 1 SSE2: 1
Compiled with runtime CPU detection.
mplayer: could not connect to socket
mplayer: No such file or directory
Failed to open LIRC support. You will not be able to use your remote control.

Playing kanye west-stronger.mp3.
Audio file file format detected.
==========================================================================
Forced audio codec: mad
Opening audio decoder: [libmad] libmad mpeg audio decoder
AUDIO: 44100 Hz, 2 ch, s16le, 192.0 kbit/13.61% (ratio: 24000->176400)
Selected audio codec: [mad] afm: libmad (libMAD MPEG layer 1-2-3)
==========================================================================
AO: [alsa] 48000Hz 2ch s16le (2 bytes per sample)
Video: no video
Starting playback...
A:   3.2 (03.1) of 269.0 (04:29.0)  2.8%
Exiting... (Quit)
thunder@thunder:~/shellprogram$ echo $?
0
thunder@thunder:~/shellprogram$
回复 支持 反对

使用道具 举报

发表于 2008-3-1 01:24:35 | 显示全部楼层
下面是正常播放的
thunder@thunder:~/shellprogram$ mplayer kan*.mp3
Starting playback...
A: 269.0 (04:29.0) of 269.0 (04:29.0)  2.8%
[color="Red"]Exiting... (End of file)
thunder@thunder:~/shellprogram$ echo $?
0
上次退出的结果对shell来说都是0,可见你所说的两种情况都是正常退出,要想区分这两种情况,我认为, 可以用文件正常播放完的时间和mplayer运行的时间入手,来判断是否正常播放完毕。
回复 支持 反对

使用道具 举报

发表于 2008-3-1 09:45:01 | 显示全部楼层
还可以用信号捕获的方法来处理。
trap "my_signal" 2 3
然后脚本里面利用showkey调用可以捕获具体的某个按键被按下。自己研究下吧。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-3-1 23:52:57 | 显示全部楼层
多谢kingthunder提示!

文件播放时间要根据文件大小模糊判断吗,要是快进过就有问题了。:)
第二种方发我好好研究下,现在还不太明白。

我还有个思路,能不能把这些东西
Starting playback...
A: 269.0 (04:29.0) of 269.0 (04:29.0) 2.8%
Exiting... (End of file)
单独输出在什么地方,然后判断是否有 (End of file)
不过好像不能用重定向,在想想看吧...
回复 支持 反对

使用道具 举报

发表于 2008-3-2 10:56:30 | 显示全部楼层
ok,多谢你提醒我哈。可以重定向的。把标准输出重定向到一个文件中就ok了
thunder@thunder:~/shellprogram$ mplayer aaa.mp3 [color="Red"]1>mplayer.log
mplayer: could not connect to socket
mplayer: No such file or directory
Failed to open LIRC support. You will not be able to use your remote control.[color="Red"]-----》按q键
thunder@thunder:~/shellprogram$[color="Red"] cat mplayer.log
MPlayer 1.0rc2-4.1.2 (C) 2000-2007 MPlayer Team
CPU: AMD Sempron(tm) Processor 2500+ (Family: 15, Model: 44, Stepping: 2)
CPUflags:  MMX: 1 MMX2: 1 3DNow: 1 3DNow2: 1 SSE: 1 SSE2: 1
Compiled with runtime CPU detection.

Playing aaa.mp3.
Audio file file format detected.
==========================================================================
Forced audio codec: mad
Opening audio decoder: [libmad] libmad mpeg audio decoder
AUDIO: 44100 Hz, 2 ch, s16le, 192.0 kbit/13.61% (ratio: 24000->176400)
Selected audio codec: [mad] afm: libmad (libMAD MPEG layer 1-2-3)
==========================================================================
AO: [alsa] 48000Hz 2ch s16le (2 bytes per sample)
Video: no video
Starting playback...
A:  14.7 (14.7) of 269.0 (04:29.0)  2.9%
Exiting... (Quit)[color="Red"]-----》想要的结果,ok可以用其他方法来提取了!
回复 支持 反对

使用道具 举报

发表于 2008-3-2 10:58:35 | 显示全部楼层
thunder@thunder:~/shellprogram$ tail -1 mplayer.log
Exiting... (Quit)
回复 支持 反对

使用道具 举报

发表于 2008-3-2 11:13:21 | 显示全部楼层
ok,总的来看就是mplayer aaa.mp3 | tail -1 | awk '{print $2}'
thunder@thunder:~/shellprogram$ mplayer aaa.mp3 | tail -1 | awk '{print $2}'
mplayer: could not connect to socket
mplayer: No such file or directory
Failed to open LIRC support. You will not be able to use your remote control.
(Quit)[color="Red"]-----》想要的结果,ok已经可以使用了!!
回复 支持 反对

使用道具 举报

发表于 2008-3-2 11:15:36 | 显示全部楼层
如果你要把中间结果保存的话,就定向到一个文件中去吧,用awk,sed等来分析文件就ok了。如果不用中间结果,只是分析呢,那就直接用管道(进程间通信)就好了
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-3-2 12:25:09 | 显示全部楼层
太感谢kingthunder兄了

以前重定向过mplayer输出的wav文件,所以一直以为mplayer重定向的是输出的音视频流(话又说回来了,如果要重定向mplayer的A/V流该怎么办? :-))

呵呵,shell大功告成了,可以像终极解码一样自动播放连续电影文件了,庆祝一下

前天更新了几个包,导致混蛋锐捷出故障,上网又成麻烦事了....

THANKS AGAIN!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表