Starpacks: Why and how to copy your own executable and launching the copied instance instead
Put the following sequence near the very top of your main starpack script:
set myself [info nameofexecutable] set mytemp [file join $::env(temp) [file tail $myself]] if {[string compare -nocase $myself $mytemp]} { set mytempP $::env(temp) if {[catch {exec -- [auto_execok xcopy] [file nativename $myself] [file nativename $mytempP] /Y} rc]} { tk_messageBox -type ok -title Fehler:\ -message "Copy failed:\n\n$rc\n" -icon error exit 253 } if {[catch {eval exec -- $mytemp $argv &} rc]} { tk_messageBox -type ok -title Fehler:\ -message "Exec failed:\n\n$rc\n" -icon error exit 252 } exit 0 }
See http://permalink.gmane.org/gmane.comp.lang.tcl.starkit/2537
I'm currently testing the above linked solution, meanwhile, here's the standardized version of the above snipped:
################################################################################ # # Modul : autoclone.tcl # Stand : 13.11.2005 # Zweck : Kopieren des eigenen Programmes an eine temporäre Position und # Neustart von dort # Autor : M.Hoffmann # Hinweise : # # `Autoclone`-Verfahren zur Vermeidung blockierter .EXEs (Updates!): Programm # kopiert sich selbst in Temp-Position und lädt von dort. Leider kann ein # Starkit sich weder mittels 'file copy', noch mittels 'open/fcopy/get/put' # selbst kopieren, da dann die gesamte VFS-Struktur ausgepackt wird. # Es muss also ein externer Copy angestossen werden! (Leider geht die Methode, # cmd.exe im Hintergrund zu starten und einen copy-befehl dorthin zu pipen, # nicht, da Benutzer teilweise kein Recht haben, cmd.exe zu starten). Daher # wird der externe Befehl XCOPY verwendet! # ACHTUNG: Ist das Paket eingebunden, wird das Verfahren automatisch über # das Paket initscript verwendet, es sei denn, der LETZTE Kommandozeilen- # Parameter lautet: --noautoclone # # ACHTUNG: Das Verfahren kann momentan nicht für Consol-Anwendungen (tclsh) # eingesetzt werden, nur für Tk-Programme. Evtl. kann dies mit START # umgangen werden. # # Historie : # 13.11.05 v1.0: als Paket aus RECEIVE übernommen (Schalter inkompatibel!) # ################################################################################ package provide autoclone 1.0 ################################################################################ # Prozedur zur Vermeidung globaler Variablen! # proc autoClone {} { if {[string equal -nocase [lindex $::argv end] "--noautoclone"]} { # Paket ist zwar eingebunden, aber wegen --noautoclone das # Verfahren nicht verwenden; also Schalter entfernen, um das # spätere Kommandozeilenparsing nicht zu stören. set ::argv [lreplace $::argv end end] set ::argc [llength $::argv] } else { set myself [file normalize [info nameofexecutable]] set mytempP $::env(temp); # Schwachpunkt: Umgebungsvar TEMP muss existieren! set mytemp [file normalize [file join $mytempP [file tail $myself]]] # Abfangen: Aufruf bereits aus TempVerzeichnis if {[string compare $myself $mytemp]} { # Voraussetzung: Aufruf von XCOPY muss möglich sein! catch {exec -- [auto_execok xcopy] [file nativename $myself] [file nativename $mytempP] /Y} # Falls Kopieren scheiterte, aber Ziel schon da war, ebenfalls fortfahren if {[file exists $mytemp]} { set ::argv [linsert $::argv 0 $mytemp] # Versuch mit START, bislang erfolglos (EXEC ist eigenwillig!): # set ::argv [linsert $::argv 0 [auto_execok cmd] /c start \"[file nativename $mytemp]\"] lappend ::argv --noautoclone # durch Angabe von --noautoclone hier Rekursion verhindern! if {[catch {eval exec -- $::argv &} rc]} { # Kopie ist aus irgendwelchen Gründen nicht aufrufbar; # Fehler ignorieren und aktuelle Instanz weiter verwenden! } else { exit 0; # sofort BEENDEN, da mit Kopie weitergearbeitet wird! } } } } } # ACHTUNG: Sofortige Ausführung durch PACKAGE REQUIRE! autoClone;
It should be mentioned that the code for now only works with starpacks that use tclkit...
Oh yeaah! Someone destroyed the page I'm just editing.... I have to retype everyting...: The reason for problems with tclkitsh vs. tclkit under windows are the specialities of the windows console mode. If calling a console mode prog via exec xyz & this called prog uses our console window handles or so, so we can't exit after execing.... the programs seems to hang.