I've already read: thread287-1327062, but it doesn't seem to answer all my questions.
For some reason the parent doesn't pick up correctly when the children terminate.
I've created the following expect scripts: tclmulti.exp & helloworld.expect, both residing in the same dir.
-------------------------------------------------
#!/usr/bin/expect
#
# tclmulti.exp
#
set cpid {}
set commandList {"./helloworld.expect 10" "./helloworld.expect 5"}
foreach command $commandList {
set child [fork]
if {$child == 0} {
puts "I'm a child: my command is: $command"
catch {exec /bin/sh -c $command} result
puts "$result"
exit 1;
} else {
lappend cpid $child
puts "Child has pid: $child"
puts "My children are: $cpid"
}
}
-------------------------------------------------
#!/bin/sh
# the next line restarts using tclsh \
exec expect "$0" "$@"
#
# helloworld.exp
#
# Check for the right argument
if {$argc > 0 } {
set text [lindex $argv 0]
} else {
puts stderr "$argv0 --- usage: $argv0 <hostname>"
exit 1
}
sleep $text
puts $text
------------------------------------------------
for {set i 0} {$i<[llength $cpid]} {incr i} {
puts "Waiting for process #$i to complete"
puts [wait]
puts "Done waiting for #$i"
}
------------------------------------------------
It then shows me the result of the Children after the parent tells you the child has closed.
**Result**
user@local:~/$ ./tclmulti.exp
I'm a child: my command is: ./helloworld.expect 10
Child has pid: 5539
My children are: 5539
I'm a child: my command is: ./helloworld.expect 5
Child has pid: 5541
My children are: 5539 5541
Waiting for process #0 to complete
5537 exp0 -1 10 POSIX ECHILD no children
Done waiting for #0
Waiting for process #1 to complete
5537 exp0 0 4
Done waiting for #1
user@local:~/$ 5
10
user@local:~/$
For some reason the parent doesn't pick up correctly when the children terminate.
I've created the following expect scripts: tclmulti.exp & helloworld.expect, both residing in the same dir.
-------------------------------------------------
#!/usr/bin/expect
#
# tclmulti.exp
#
set cpid {}
set commandList {"./helloworld.expect 10" "./helloworld.expect 5"}
foreach command $commandList {
set child [fork]
if {$child == 0} {
puts "I'm a child: my command is: $command"
catch {exec /bin/sh -c $command} result
puts "$result"
exit 1;
} else {
lappend cpid $child
puts "Child has pid: $child"
puts "My children are: $cpid"
}
}
-------------------------------------------------
#!/bin/sh
# the next line restarts using tclsh \
exec expect "$0" "$@"
#
# helloworld.exp
#
# Check for the right argument
if {$argc > 0 } {
set text [lindex $argv 0]
} else {
puts stderr "$argv0 --- usage: $argv0 <hostname>"
exit 1
}
sleep $text
puts $text
------------------------------------------------
for {set i 0} {$i<[llength $cpid]} {incr i} {
puts "Waiting for process #$i to complete"
puts [wait]
puts "Done waiting for #$i"
}
------------------------------------------------
It then shows me the result of the Children after the parent tells you the child has closed.
**Result**
user@local:~/$ ./tclmulti.exp
I'm a child: my command is: ./helloworld.expect 10
Child has pid: 5539
My children are: 5539
I'm a child: my command is: ./helloworld.expect 5
Child has pid: 5541
My children are: 5539 5541
Waiting for process #0 to complete
5537 exp0 -1 10 POSIX ECHILD no children
Done waiting for #0
Waiting for process #1 to complete
5537 exp0 0 4
Done waiting for #1
user@local:~/$ 5
10
user@local:~/$