How to terminate a remote KDB+ session via script?
Asked Answered
kdb
B

5

5

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.

Boehmite answered 4/2, 2013 at 16:19 Comment(0)
W
8

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)\\
Wonky answered 5/2, 2013 at 3:8 Comment(2)
I had already tried an async call, no errors are thrown but it also doesn't shut down the server. Bear in mind the code should be run from a script and not in interactive mode.Boehmite
That's strange; it seems to work just fine for me (on Windows). Using protected eval with a synchronous call will work, but also has the side effect of hanging your client while waiting for the remote process to shut down. This might be bad if you have a lot of processes to shut down.Wonky
B
3

I managed to sort this out by using Protected Evaluation:

In test.q file:

h: hopen `::5000
@[h; "exit 0"; {}]
\\
Boehmite answered 6/2, 2013 at 22:27 Comment(0)
S
0

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

Spandau answered 19/4, 2019 at 9:11 Comment(0)
J
0

You have option explicitly close session and discard handle:

h: hopen `:hostname:port  <BR>
h <BR>
h:hclose <BR>
h<BR>
Jaan answered 12/6, 2019 at 14:14 Comment(0)
N
0

I would use a function like this:

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

you don't need protective evaluation for this

Norward answered 9/7 at 22:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.