This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

killing an exec process

Old posts that have not been replied to for several years.
Locked
a
arschbombe

killing an exec process

Post by arschbombe »

hi all,
i have a eggdrop tcl script which calls an external c++ program with the help of the exec command. the problem is that the prog is not too stable and sometimes needs to be killed and restarted. but i think that the procedure which invoked the process returns only after the c++ program returns and therefore i cant call another procedure in the same script that could kill the process, because it waits until the proc that called the c++ prog returns.

so is it possible to load 2 scripts on the same bot, one that executes the program and the other one with the ability to kill the process? or do i need 2 bots one for executing and one for killing? or is it much more simple if you know eggdrop and tcl better than me? ;P
any help is appreciated, thx :)

-focco
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

The exec command is just one way of doing things. You can also run applications using the "open" command, and using a pipe as the first character.

This way, you can create a loop, that will continue until you feel the program has completed, or you wish to kill it.

EG

Code: Select all

set fp [open "|/path/to/my/cpp/application" r]
set i 0
while {($i < 1000) && (![eof $fp])} {
  incr $i
}
if {[eof $fp]} {
  close $fp
  # Application finished normaly
} else {
  # Need to send a kill signal, then close the file pointer.
}
a
arschbombe

Post by arschbombe »

this looks like a good solution, im trying to implement it.
thanks a lot for your help :)
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

My bad, along with the "incr" counter, you will also need to use the "gets" command.

Without it, EOF will never be read, thus will never be reported.

A simple

Code: Select all

set null [gets $fp]
Will do.
a
arschbombe

Post by arschbombe »

hehe, thx for doublechecking :D

but on second thought, this solution is not ideal for the problem because the running time of the prog varies a lot, it depends heavily on the input params.
i really need a !trigger which kills the process instantly, whenever wanted. so i guess i dont get around the 2 scripts or even 2 bots since no procedure can be called as a long as the procedure that called the process didnt return, right?
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

Again, using the open command, this can be done.

As it had proved, no blocking takes place (otherwise the while loop wouldn't even work).

This would be a three part script.

1: Launcher: This will start the program, store the file ponter in a global variable and start a timer.

2: A checker: Will run on a timer, and check the process every so often, as to automaticaly clear it up, rather than only have it done manualy.

3: Manual kill: To manualy kill the program.

IE.

Code: Select all

set ourargs(on) 0
set ourargs(fp) 0
bind pub - "start" pub:start
proc pub:start {nick uh hand chan arg} {
  global ourargs
  if {$ourargs(on)} {
    #Allready running, return
    return
  }
  set ourargs(fp) [open "|/path/to/prog"]
  set ourargs(on) 1
  utimer 5 [list ourcheck]
  #list not needed in the case, but it's a habit, and a good one for safety
}

proc ourcheck {} {
  global ourargs
  if {$ourargs(on)} {
    if {[eof $ourargs(fp)]} {
      close $ourargs(fp)
      set ourargs(on) 0
    }
    utimer 5 ourcheck
  }
}

bind pub - "stop" pub:stop
proc pub:stop {nick uh hand chan arg} {
  global ourargs
  if {!$ourargs(on)} {
    #No running, don't bother
    return
  }
  close $ourargs(fp)
  set ourargs(on) 0
}
a
arschbombe

Post by arschbombe »

wow, that script does really what i want. thanks a lot for your time. since i am a newbie with tcl i needed some time to understand and extend the script but it works now. thx :)
Locked