+ -
当前位置:首页 → 问答吧 → 我想要这样的脚本——

我想要这样的脚本——

时间:2011-04-11

来源:互联网

在Expect脚本中,我想把系统当前进程中的corosync进程个数查询出来,并且赋值给一个变量corosync_count,请问该如何使用Tcl实现。
当corosync进程存在时,就使用kill -9杀掉这些进程。

# 检测当前系统进程中是否存在corosync的进程,并且把进程数赋值给corosync_count
set corosync_count [ exec ps -ef | grep 'corosync' | grep -v 'grep' | wc -l ]

# 如果进程数大于0,即存在corosync进程
if { $corosync_count > 0 } {

### 返回corosync的pid列
set corosync_pid [ exec ps -ef | grep 'corosync' | grep -v 'grep' | awk "{print \$2}" ]

### 遍历corosync_pid列表,杀死进程
foreach i $corosync_pid {
exec kill -9 $i
}



执行返回为
child process exited abnormally
while executing
"exec ps -ef | grep 'corosync' | grep -v 'grep' | awk "{print \$2}" "

作者: chengmoYS   发布时间: 2011-04-11

corosync进程运行的时候,在shell中查询如下

[root@localhost Desktop]# ps -ef | grep 'corosync' | grep -v 'grep' | awk '{print $2}'
2280

在expect中查询如下

expect1.1> set cp [ exec ps -ef | grep 'corosync' | grep -v 'grep' | awk "{print \$2}" ]
6934

为啥不一样呢?

作者: chengmoYS   发布时间: 2011-04-11