I have tried to search process id i-e 6762 stored in a variable say buffer
nohup tcpdump -ni eth0 -s0 2>&1
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
}
Some of the problems with the code snippet above:
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"
}
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.
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.