Tcl 如何在expect脚本中获取父进程id?
我想知道expect脚本中父进程的父进程id,我该怎么做 我曾试图使用,但出现了这个错误Tcl 如何在expect脚本中获取父进程id?,tcl,expect,Tcl,Expect,我想知道expect脚本中父进程的父进程id,我该怎么做 我曾试图使用,但出现了这个错误 [sesiv@itseelm-lx4151 ~]$ invalid command name "id" while executing "id process parent " invoked from within "set parent_process_id [ id process parent ]" 也试着去做 puts "parent is ---$env(PPID)**"
[sesiv@itseelm-lx4151 ~]$ invalid command name "id"
while executing
"id process parent "
invoked from within
"set parent_process_id [ id process parent ]"
也试着去做
puts "parent is ---$env(PPID)**"
但它给了我这个
[sesiv@itseelm-lx4151 ~]$ can't read "env(PPID)": no such variable
while executing
"puts "parent is ---$env(PPID)**""
id
命令是Tclx包的一部分,您需要包括它:
package require Tclx
更新
由于您的系统没有Tclx包,并且根据您的提示,我猜您正在运行一个类似Unix的操作系统,因此我将提供一个使用ps
命令的解决方案。此解决方案在Windows下不起作用
# Returns the parent PID for a process ID (PID)
# If the PID is invalid, return 0
proc getParentPid {{myPid ""}} {
if {$myPid == ""} { set myPid [pid] }
set ps_output [exec ps -o pid,ppid $myPid]
# To parse ps output, note that the output looks like this:
# PID PPID
# 4584 613
# index 0="PID", 1="PPID", 2=myPid, 3=parent pid
set checkPid [lindex $ps_output 2]
set parentPid [lindex $ps_output 3]
if {$checkPid == $myPid} {
return $parentPid
} else {
return 0
}
}
# Test it out
set myPid [pid]
set parentPid [getParentPid]
puts "My PID: $myPid"
puts "Parent PID: $parentPid"
id
命令是Tclx包的一部分,您需要包括它:
package require Tclx
更新
由于您的系统没有Tclx包,并且根据您的提示,我猜您正在运行一个类似Unix的操作系统,因此我将提供一个使用ps
命令的解决方案。此解决方案在Windows下不起作用
# Returns the parent PID for a process ID (PID)
# If the PID is invalid, return 0
proc getParentPid {{myPid ""}} {
if {$myPid == ""} { set myPid [pid] }
set ps_output [exec ps -o pid,ppid $myPid]
# To parse ps output, note that the output looks like this:
# PID PPID
# 4584 613
# index 0="PID", 1="PPID", 2=myPid, 3=parent pid
set checkPid [lindex $ps_output 2]
set parentPid [lindex $ps_output 3]
if {$checkPid == $myPid} {
return $parentPid
} else {
return 0
}
}
# Test it out
set myPid [pid]
set parentPid [getParentPid]
puts "My PID: $myPid"
puts "Parent PID: $parentPid"
$PPID
环境变量未导出到子进程。$PPID
环境变量未导出到子进程。是否有其他方法,我似乎没有这个包,因为我使用了它并得到了错误没有其他方法,看来我没有这个软件包,因为我用了它,并得到了错误没有这样的软件包