How to terminate an application based on file existence check in Tcl on Linux environment

StackOverflow https://stackoverflow.com/questions/22532611

  •  18-06-2023
  •  | 
  •  

Question

I run T-Plan robot which connects to my windows machine and executes some script. On successful execution of script,I export the generated xml file via pscp to my linux machine.

T-paln robot acts as a 3rd party freeware to pass some command via cmd on windows machine. This takes place by running a simple batch file on t-plan robot.However,the script which sends out command to windows disconnects itself based on some explicitly declaring timeout seconds.

I want to write a tcl code which launches this application on linux machine and once the command has generated a successful outcome as xml file and is received on linux machine,it should check whether the xml file exists on the specified directory and terminates the application right at that moment.I want this because the next code section would parse this received xml report and perform other actions.

I think there should be some class in tcl which kills the process/service on any environment ,here I need to perform that in linux.

Sincerely waiting for reply .Thanks in advance

Was it helpful?

Solution

To kill a process on the same Linux machine, provided you've got permission to do so (i.e., you're running as the same user), you do either:

package require Tclx
kill $processId

Or:

exec kill $processId

(The former doesn't require an external command — it does the syscall directly — but the second doesn't need the Tclx pacakge.)

Both of these require the process ID.

To test if a file exists, use file exists like this:

if {[file exists $theFilename]} {
    puts "Woohoo! $theFilename is there!"
}

To kill something on a remote machine, you need to send a command to run to that machine. Perhaps like:

exec ssh $remoteMachine kill $remotePID

Getting the $remotePID can be “interesting” and may require some considerable thought in your whole system design. If calling from Windows to Linux, replace ssh with plink. If going the other way, you're talking about doing:

exec ssh $remoteMachine taskkill /PID $remotePID

This can get very complicated, and I'm not sure if the approach you're taking right now is the right one.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top