How to search a digit i.e process id in tcl and kill the process id

前端 未结 4 817
隐瞒了意图╮
隐瞒了意图╮ 2021-01-27 09:48

I have tried to search process id i-e 6762 stored in a variable say buffer

nohup tcpdump -ni  eth0 -s0  2>&1 

        
相关标签:
4条回答
  • 2021-01-27 10:32

    Almost right. You need a space to separate the first and second arguments. Also I would change the first \d to \d+, as there's always the possibility that you could have more than 9 background jobs.

    if {[regexp {\[\d+\]\s+(\d+)} $line junk pid]}
    

    Also [info exists ...] acts on a variable, not a value:

    [info exists pid]
    

    Edit: Add example of final code snippet

    There is a missing space in the foreach line. There needs to be a space before the {. And the body of the if statement was not attached.

    The parser in Tcl doesn't work in the same manner as some other languages. Line continuations and spaces are important.

    So the final code will look like:

    foreach line [split $buffer "\n"] {
        if { [regexp {\[\d+\]\s+(\d+)} $line junk pid] } \
           break
    }
    
    if { [info exists pid] } {
        puts "PID of nohup is $pid"
    }
    

    The if statement could also be (better):

    if { [regexp {\[\d+\]\s+(\d+)} $line junk pid] } {
       break
    }
    
    0 讨论(0)
  • 2021-01-27 10:32

    Some of the problems with the code snippet above:

    • missing line continuation characters
    • missing closing quotes
    • info exists should be on pid and not $pid

    Try the snippet below and see if it helps:

    foreach line [split $buffer "\n"] \
    {
        if {[regexp {\[\d\]\s+(\d+)} $line junk pid]} \
        {
            break
        }
    }
    
    if {[info exists pid]} \
    {
        puts "PID of nohup is $pid"
    }
    
    0 讨论(0)
  • 2021-01-27 10:37

    If you want to try to kill it,

    try {
        exec sh -c "kill -0 $pid && kill $pid"
    } on error e {
        puts "could not kill $pid: $e"
    }
    

    The kill -0 $pid is just a test to see if such a pid is running.

    0 讨论(0)
  • 2021-01-27 10:48

    It is important to put spaces and braces in in Tcl because each word to a command needs to be properly separated from all the others and end-of-line signals end-of-command unless you quote or escape it.

    Thus, your code:

    foreach line [split $buffer "\n"]{
        if {[regexp {\[\d\]\s+(\d+)}$line junk pid]}
           break
    }
    

    That has a problem in that there's no space between ] and { on the first line, a problem in that there's no space between } and $ on the second line, and a problem that there's nothing to make the third line associated with the second. Let's write it to be conventional Tcl:

    foreach line [split $buffer "\n"] {
        if {[regexp {\[\d\]\s+(\d+)} $line junk pid]} {
            break
        }
    }
    

    I've changed almost nothing; just added some spaces and some braces.

    0 讨论(0)
提交回复
热议问题