Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

exec and after...

Status
Not open for further replies.

fl0ra

MIS
Jul 27, 2004
93
FR
Hi guys,
I was wondering how to run shell scripts using the tcl exec function and wait for them to finish until I can start the next command...
Long story short:
Code:
foreach i $various_Shell_Scripts {
  [b]# the commands in each $i are rather complex and long and
  # I want to execute them in the background[/b]
  exec "$i &"
}

[b]# Once they are all launched I want to wait for all of them
# to complete and then execute one last shell
# but I do not know how to wait
# with [i]after[/i] maybe?[/b]
exec $another_Shell_Scripts

Cheers
 
Hmmm, it's hard to know when a script finishes when it's in the background. How about writing a dummy shell script that calls all your scripts and puts them in the background? Then you call that from tcl in the foreground and you would know when it's done. Probably not the best solution, but it might work.

Code:
#!/bin/sh
# runScripts.sh
./script1 &
./script2 &
./script3 &
#etc ...
Code:
#!/usr/bin/tclsh
exec "./runScripts.sh"
exec $another_Shell_Scripts
 
Hi

tCarls is right, but I think the dummy shell script is not needed.
Code:
[b]set[/b] command [i]""[/i]
[b]foreach[/b] i $various_Shell_Scripts {
  [b]set[/b] command [i]"$command $i &"[/i]
}
[b]exec[/b] sh -c $command

[b]exec[/b] $another_Shell_Scripts

Feherke.
 
Cheers dudes
after hours of research I found that using Tclx was the best option...
I fork my everytime I need to launch a shell script à la C, in the mean time I count how many fork call there is and I loop with a wait inside for the children to complete.
Easiest and fastest way. I have to point out to those who might read this that it does not work on windows (unless perhaps with cygwin) but on linux it does the job and it does it very nicely
 
will give a code sample as soon as I get back to work (where it remains -lol-)
 
here we go:
Code:
[i]#!/usr/bin/tclsh[/i]
[b]package require[/b] Tclx

[b]foreach[/b] i $various_Shell_Scripts {
    [b]set[/b] child [[b]fork[/b]]
    [b]if[/b] {$child == 0} {
        [b]cd[/b] [[b]lindex[/b] $i 0]
        [b]set[/b] runcmd [[b]list exec[/b] $i]
        [b]puts[/b] "Running:\ncd [lindex $i 0]\n$runcmd\n\n"
        [b]if[/b] {[[b]catch[/b] $runcmd]!=0} {
            [b]error[/b] "Failed to run command $runcmd"
            [b]exit[/b] 1
        }
        [b]exit[/b] 0
    } [b]else[/b] {
        [b]lappend[/b] cpid $child
    }
}

[i]# Waiting for the children to finish[/i]
[b]for[/b] {[b]set[/b] i 0} {$i<[[b]llength[/b] $cpid]} {[b]incr[/b] i} {
    [b]puts[/b] "Waiting for process #$i to complete"
    [b]wait[/b]
    [b]puts[/b] "Done waiting for #$i"
}

[b]set[/b] cmd "$another_Shell_Script"
[b]set[/b] runcmd [[b]list exec[/b] $cmd]
[b]if[/b] {[[b]catch[/b] $runcmd]!=0} {
    [b]error[/b] "Failed to run command $runcmd"
    [b]exit[/b] 1
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top