我想知道期望脚本中父进程的父进程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 ~]$ 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"
proc ppid {} {
catch {set line [exec who -u | grep pts]}
return [lindex $line 5]
}