I have tried to search process id i-e 6762 stored in a variable say buffer
nohup tcpdump -ni eth0 -s0 2>&1 </dev/null &
[1] 6762
You have new mail in /var/mail/root
If it matches then I want to kill it.
I have tried the following code:
foreach line [split $buffer "\n"]{
if {[regexp {\[\d\]\s+(\d+)}$line junk pid]}
break
}
if {[info exists $pid]} {
puts "PID of nohup is $pid"
}
Following error i am getting while executing the above code
wrong # args: should be "foreach varList list ?varList list ...? command"
while executing
"foreach line [split $Buffer "\n"]{"
(procedure "Test_SNMP_Trap" line 21)
invoked from within
"Test_SNMP_Trap"
(file "./SNMP_TRY.tcl" line 46)
How can i search a process id and then correctly destroy it?
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
}
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.
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"
}