Linux下后台执行的程序出现stopped的情况
写了一个服务器程序camSvr,在前台执行没有问题。使用命令./camSvr & 放到后台执行,程序执行一会儿,终端便打印:[1]+ stopped ./camSvr
使用ps -ef命令查看,程序还在,但没有响应。使用top命令查看,状态为T,即stopped。
查找原因,说是程序员中有从终端读取的代码。在《Unix 环境高级编程》第9.8节作业控制中讲到,“如果后台程序试图读取终端,这并不是一个错误,但是终端驱动程序将检测这种情况,并向后台作业发送一个特定信号SIGTTIN,该信号会停止此后台程序,并向用户发送通知”。
我代码里有调用两个脚本,一个通过top命令获取cpu占用率,一个通过free命令获取内存占用率。这两个脚本是必须的,不能去掉。
最后使用重定向解决。./camSvr < /dev/null &
而且调用top过程中,经常打印top: failed tty get 错误。
查找原因,说的是通过其他程序或者脚本在非交互模式下调用top就会出现。
解决方式是加入-b 选项。即 top -b。
-b : Batch mode operation
Starts top in ’Batch mode’, which could be useful for sending output from top to other programs or to a file.
In this mode, top will not accept input and runs until the iterations limit you’ve set with the ’-n’ command-line option or until killed.
参考:http://blog.youkuaiyun.com/a2796749/article/details/53192940