windows - Determine if process is running -
in tcl script running process can rerun after test completes. however, there 40 second wait period allow enough time program start up. if program running not want have 40 second waiting period. there anyway search , read tasklist determine if program running? thinking pid changes everytime run program because it's being placed in different area of memory.
i have batch file written process need pass result tcl. there way can accomplished?
if don't have twapi, here alternative: use tasklist.exe
:
package require csv proc getpids {imagename} { set notaskmessage "info: no tasks running match specified criteria." set output [exec tasklist.exe /fi "imagename eq $imagename" /fo csv /nh] set pidlist {} if {$output != $notaskmessage} { foreach line [split $output \n] { set tokens [::csv::split $line] lappend pidlist [lindex $tokens 1] } } return $pidlist } # try out set imagename chrome.exe set pids [getpids $imagename] if {$pids == ""} { puts "process $imagename not running" } else { puts "pids $imagename: $pids" }
discussion
- the "image name" must include extension (e.g. chrome.exe)
- the
/fi
flag specifies filter, in case, want filter image name - the
/fo
flag specifies output format: table (default), list, , csv. choose csv because easiest parse. - the
/nh
tells tasklist.exe omit header (aka no header). - if
getpids
returns empty list, process not running. getpids
might return more 1 pids, code must able handle case.
Comments
Post a Comment