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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

execute unix command with conditions

Status
Not open for further replies.

rrot

Programmer
Feb 25, 2003
6
DE
Hello, everybody.

i need to run a unix command (let's call it progxy) after
pushing a button only if there is no other instance of
this program already running.

under bash-shell i would use:

ps -C progxy || progxy

i tried:

button .can01.but01 -command {exec ps -C progxy||progxy &}.....

button .can01.but01 -command {[exec ps -C progxy]||[exec progxy &]}.....

etc.

but it seems, that the exec command tries to execute the
return codes of the ps-command.

i get an

Error: invalid command name "345"

from the interpreter.

Does anybody know how the correct syntax is, or how to set the brackets ?

Thanks, rrot

 
I dont think that tcl is going to understand your code as it stands currently. You want to check if the command is already running before you execute it. Thats going to require an if test. I think that the "345" you're getting as an error is actually the processID thats being returned from [exec ps -C progxy]. here are a couple things you can try.

button .can01.but01 -command {
if {![exec ps -C progxy]} {
exec progxy &
}
}

OR

button .can01.but01 -command { check_progxy }

proc check_progxy {} {
if {![exec ps -C progxy]} {
exec progxy &
}
}
 
Thank you smugindividual.

i tried out your code, but i got the same error as
with a similiar code i tried by myself before:

Error: PID TTY TIME CMD
child process exited abnormally

it looks as if this code tries to execute the output
of the ps command and of course can't do that.

so, is there anybody out there who has a solution ?

Thanks, rrot
 
Sure, but it's a hack.
Code:
proc checkForProcess {cmd} {

       set data [eval exec $cmd]
       foreach part [split $data \n] {
               if {![regexp "grep" $part]} {
                     return $part
               }
        }
}


 if {![string length [checkForProcess "ps -aux | grep progxy"]]} {
                 puts "No such process available"
                 puts "Starting progxy"
                 catch {exec progxy}
             }
No such process available
Starting progxy
 
Thank you all for your support,

but i think i found a small and good solution
- and all by myself ;)

instead of the ps command i used the skill command.

with "skill -n -c progxy" it returns only the process id
and only if the process is running.

so i made the following:

proc startprogxy {} {
set progxyresult [exec skill -n -c progxy]
if {$progxyresult==""} then {exec progxy &}
}

button .can01.but01 -command {startprogxy} -........

Until next time, rrot
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top