nunaxe
nunaxe

Reputation: 1420

How to terminate a remote KDB+ session via script?

I need to kill a remote KDB+ session. This can be done in several ways but I'd prefer to use IPC handlers.

I start a KDB+ session:

$ q -p 5000
KDB+ 3.0 2012.11.13 Copyright (C) 1993-2012 Kx Systems

Then I start another KDB session and I manage to kill the server successfully:

$ q
KDB+ 3.0 2012.11.13 Copyright (C) 1993-2012 Kx Systems

q)h: hopen `::5000
q)h(exit;0)
'close
q)\\

But, if I create a script (test.q) with the instructions above, it fails:

$ cat test.q 
h: hopen `::5000
h(exit;0)
\\

$ q test.q 
KDB+ 3.0 2012.11.13 Copyright (C) 1993-2012 Kx Systems

k){0N!x y}
'close
@
"q"
"h(exit;0)"
q))

Any ideas? I really appreciate.

Upvotes: 5

Views: 4134

Answers (5)

Alex R.
Alex R.

Reputation: 721

I would use a function like this:

{x:hopen x; neg[x]@(exit;0)} `::5555

you don't need protective evaluation for this

Upvotes: 0

Alex Zamsky
Alex Zamsky

Reputation: 21

You have option explicitly close session and discard handle:

h: hopen `:hostname:port  <BR>
h <BR>
h:hclose <BR>
h<BR>

Upvotes: 0

Sujoy
Sujoy

Reputation: 21

You might want to try async. Also if needed you can try deferred async (neg h) ({exit 0};`)[]

Upvotes: 0

nunaxe
nunaxe

Reputation: 1420

I managed to sort this out by using Protected Evaluation:

In test.q file:

h: hopen `::5000
@[h; "exit 0"; {}]
\\

Upvotes: 3

moepud
moepud

Reputation: 461

You are making a synchronous request to the remote server which means that you are expecting a response. The problem is that your request causes the remote server to shut down and close the connection immediately, resulting in an error and causing q to go into debug mode.

If you just want to send an exit to the remote server without causing an error, you can send the request asynchronously by using a negative value for the connection handle (notice the lack of the 'close error):

q)h: hopen `::5000
q)(neg h) (exit;0)
q)\\

Upvotes: 8

Related Questions