Tuesday, September 15, 2009

How-to: Running q/kdb+ as service on Windows

When simply running q on windows from command line you will end  with many q instances running consuming space on you taskbar and so on. If you spend some time and setup all your q dev/prod instances like services on Windows this will save you huge amount of time. BTW if you don't have q right now you can download it for free from KX Systems site:http://kx.com/Developers/FREE-DOWNLOAD.php

There are many tools for Windows to wrap any executable as service, but I will use free tool from Microsoft - SRVANY.
SrvAny is a tool found in the Windows NT Resource Kit that is used for running Windows NT applications as services. You can download it from Microsoft site:ftp://ftp.microsoft.com/bussys/winnt/winnt-public/reskit/nt40/i386/srvany_x86.exe .

To create a Windows service, perform the following steps:

1.Copy srvany.exe to c:\q

2. At command prompt(cmd.exe), type
instsrv.exe q5010 c:\q\srvany.exe
This will create service named q5010. We will use it to run q on port 5010(tickerplant)

3.Prepare Windows registry file to setup parameters for q5010 service:

Windows Registry Editor Version 5.00

"AppParameters"="-p 5010 -q"
4. Now you can use q5010 service as usual windows service:

To start: sc start q5010 or net start q5010
To stop: sc stop q5010 or net stop q5010

If you want setup several instances repeat steps 2-3 for different service names and ports. If you run tickerplant, realtime and historical database you will have 3 services at minimum, q5010, q5011, q5012. To start in appropriate order you can setup dependencies between q5011 and q5010

sc.exe q5011 depend= q5010

Note that sign '=' must be immediately after word depend and space before service name.

In this way you can setup on windows complete kdb setup that starts and stops in right way.

Additional features:

Output redirect:
Also if you need to do redirect of output you must modify application using following reg file.
Windows Registry Editor Version 5.00

"AppParameters"="/c c:\\q\\w32\\q.exe -p 5010 -q >c:\\q\\logs\\q.5010.log"
Environment setup:

1.Start regedit and go to following subkey:
2. Add Value with name AppEnvironment with type REG_MULTI_SZ
3. In the Multi-String Editor, enter environment variables using the syntax =.
Do not forget to add a new line after the last line, or the value will be ignored.

Related articles:
How To Create a User-Defined Service:http://support.microsoft.com/kb/137890
Troubleshooting SrvAny Using Cmd.exe:http://support.microsoft.com/kb/152460
Passing Environment Variables to Applications Started by SRVANY:http://support.microsoft.com/kb/197178

No comments: