Java阻止进程是否由runtime.exec创建?

时间:2014-07-10 12:14:21

标签: java runtime.exec

从Java代码我按照以下方式调用我的脚本文件,

Process  process =Runtime.getRuntime().exec("sh /usr/local/garner/garnerd start");
int status = process.waitFor();
下面给出了

garnerd脚本代码(后面又调用了garner.sh):

function start() 
{
    sh /usr/local/garner/garner.sh > /usr/local/garner/log/garner.log &
    echo "Garner is started"
}

case "$1" in
    start)
        start
        ;;
    *)
        echo "Usage: garnerd {start|stop|restart|status|reconfig}"
        exit 1
esac
exit $retval     

Garner shell脚本(garner.sh)来源是:

/usr/local/garner/garnerd status
if [ $? -eq 0 ]; then
    echo "`date` $0 :Garner is allready running"
    exit 0
fi
touch /dev/blank
cd /usr/local/garner
uname -a | grep -i cygwin
if [ $? -eq 0 ]
then
    export CYGWIN="$CYGWIN error_start=dumper -d %1 %2"
    /usr/local/garner/garner.exe -n -c /usr/local/garner/conf/garner.conf -p /usr/local/garner/garner.pid -l /usr/local/garner/log/garner.log -L 4   &
else
    /usr/local/garner/garner  -c /usr/local/garner/conf/garner.conf -p /usr/local/garner/garner.pid -l /usr/local/garner/log/garner.log -L 4   &
fi
cd -

当我调用./garnerd启动时,它会创建pid文件。之后如果我看到此文件的内容,它会显示加工的进程ID。

[root@localhost garner]# cat garner.pid
9282

但是当我通过以下命令检查进程ID的详细信息时,它会显示使用信号3的“SigBlk:0000000000000004”

[root@localhost garner]# cat /proc/9282/status
Name:   garner
State:  S (sleeping)
SleepAVG:       78%
Tgid:   9282
Pid:    9282
PPid:   9281
TracerPid:      0
Uid:    0       0       0       0
Gid:    0       0       0       0
FDSize: 64
Groups: 0 1 2 3 4 6 10
VmPeak:    58888 kB
VmSize:    58884 kB
VmLck:         0 kB
VmHWM:      7124 kB
VmRSS:      7124 kB
VmData:    17192 kB
VmStk:        88 kB
VmExe:        84 kB
VmLib:      4480 kB
VmPTE:       156 kB
StaBrk: 05af0000 kB
Brk:    060ec000 kB
StaStk: 7fff0329d950 kB
Threads:        2
SigQ:   0/47721
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: 0000000000000004
SigIgn: 0000000000001002
SigCgt: 0400000180006005
CapInh: 0000000000000000
CapPrm: 00000000fffffeff
CapEff: 00000000fffffeff
Cpus_allowed:   00000000,00000000,00000000,00000000,00000000,00000000,00000000,00ffffff
Mems_allowed:   00000000,00000003

如果我从linux机器手动运行命令(./ garnerd start),它会显示“SigBlk:0000000000000000”。 这意味着Java阻止进程?如果是,那么为什么以及在哪种情况下?

1 个答案:

答案 0 :(得分:5)

来自java.lang.Process的{​​{3}}:

  

因为某些本机平台仅提供有限的缓冲区大小   标准输入和输出流,无法及时写入输入   流或读取子进程的输出流可能会导致   要阻止的子进程,甚至是死锁。

API doc详细解释了这个问题并提出了解决方案。