DX Unified Infrastructure Management

Expand all | Collapse all

Lua script Kill process, restart services

  • 1.  Lua script Kill process, restart services

    Posted Mar 31, 2014 01:48 PM

    Hi

     

    I  have minimal scripting knowledge and have a request to stop and start two services and kill the process of one of the services if it fails to stop gracefully.This script will be launched from an AO profile on a custom alarm I have created.

    Is there a simple way to do this via a LUA script ?

     

    Many thanks

    Dave 



  • 2.  Re: Lua script Kill process, restart services

    Posted Mar 31, 2014 02:25 PM

    Hi,

     

    Assume your alarm message is the pid of the process to be killed:

     

    msg = alarm.get("")
    
    arguments = pds.create()
    pds.putInt(arguments, "pid", msg.message)
    nimbus.request("/domain/hub/robot/processes", "kill_process", arguments)

     -jon



  • 3.  Re: Lua script Kill process, restart services

    Posted Mar 31, 2014 02:40 PM

    Hi Jon,

     

    Thanks for the info, the alarm message is a SQL_response message which checks if a table has updated in a given time period, if not then I require an AO profile to run a script to do the following.

     

    Stop Windows service on server A

    Kill process on server B

     

    Start Windows service on server A

    Start Windows service on server B

     

    I have NTservices and Processes probe deplyed to both.

     

    Kind Regards

    Dave 



  • 4.  Re: Lua script Kill process, restart services

    Posted Mar 31, 2014 03:25 PM

    Hi,

     

    Something like this should work. Didn't test it, but hope it helps anyway:

     

    service1_pds = pds.create()
    pds.putString(service1_pds, "name", "service_name")
    
    service2_pds = pds.create()
    pds.putString(service2_pds, "name", "service_name")
    
    procs, rc = nimbus.request("/Domain/Hub/robot/processes", "list_processes")
    process_pds = pds.create()
    
    nimbus.request("/Domain/Hub/robotA/ntservices", "stop_service", service1_pds)
    
    for _,process in pairs (procs) do
       if string.match(process.executable, "C:\\path\\to\\process.exe") then
          pds.putInt(process_pds, "pid", process.process_id)
          nimbus.request("/Domain/Hub/robotB/processes", "kill_process", process_pds)
       end
    end
    
    sleep(10)
    
    nimbus.request("/Domain/Hub/robotA/ntservices", "start_service", service1_pds)
    nimbus.request("/Domain/Hub/robotB/ntservices", "start_service", service2_pds)
    

     



  • 5.  Re: Lua script Kill process, restart services

    Posted Mar 31, 2014 05:37 PM

    Hi Jon

     

    I have the services restart of the script working but can't get the kill_process to work, here is my edited version as a test on the print spooler. 

     

     

    procs, rc = nimbus.request("/Domain/NMSTunnelServer1/rt-w28s-006/processes", "list_processes")

    process_pds = pds.create()

     

    for _,process in pairs (procs) do

       if string.match(process.executable, "C:\Windows\System32\spoolsv.exe") then

          pds.putInt(process_pds, "pid", process.process_id)

          nimbus.request("/Domain/NMSTunnelServer1/rt-w28s-006/processes", "kill_process", process_pds)

       end

    end

     

    Kind regards

    Dave



  • 6.  Re: Lua script Kill process, restart services

    Posted Mar 31, 2014 06:25 PM
    You need double \ in the executable path in string match. Or use ' instead of "

    -jon


  • 7.  Re: Lua script Kill process, restart services

    Posted Mar 31, 2014 07:15 PM

    Thanks Jon

     

    Works a treat, thanks for your help.

     

    Kind Regards

    Dave



  • 8.  Re: Lua script Kill process, restart services

    Posted Apr 01, 2014 12:37 PM

    Hi

     

    Still having an issue with this script killing the process , I have tried with a differant process on the same server and this works fine the only differance I can see is that this particular process does not run as local system but as administrator.

     

    Is there a way to pass this information through via the script? 



  • 9.  Re: Lua script Kill process, restart services

    Posted Apr 01, 2014 02:11 PM

    forgot to post the script here it is. 

     

    procs, rc = nimbus.request("/Domain/CATraffic/ca-data-svr/processes", "list_processes")

    process_pds = pds.create()

     

    for _,process in pairs (procs) do

       if string.match(process.executable, "C:\\Program Files\\mxData Limited\\CA ANPR Database Server\\binary\\utmc-scheduler.exe") then       pds.putInt(process_pds, "pid", process.process_id)

          nimbus.request("/Domain/CATraffic/ca-data-svr/processes", "kill_process", process_pds)

       end

    end



  • 10.  Re: Lua script Kill process, restart services

    Posted Apr 01, 2014 02:40 PM

    Hi,

     

    You might want to modify it to do some error checking:

     

    rc, err = nimbus.request("/Domain/CATraffic/ca-data-svr/processes", "kill_process", process_pds)

    print (rc)

    print (err)

     

    It might nto have  the "err" which is error message in text form, not all functions return it. Can't remember now if this does, you'll have to try it out.

     

    You might also want to put something like print("test") inside the if for string.match to make sure that it matches the process. You can check the executable names from processes probe callback "list_processes". You can do the callback in IM by pressing ctrl+p when probe is chosen.

     

    -jon



  • 11.  Re: Lua script Kill process, restart services

    Posted Apr 01, 2014 05:11 PM

    Thanks Jon

     

    Finaly got the answer now it didn't like the - in utmc-scheduler.exe

     

    this works

     

    ifstring.match (process.executable, "C:\\Program Files\\mxData Limited\\CA ANPR Database Server\\binary\\utmc%-scheduler.exe")

    then



  • 12.  Re: Lua script Kill process, restart services

    Posted Apr 01, 2014 05:48 PM

    Good catch. In case you did not figure out why you had the issue with the - character...

     

    The 2nd argument to the string.match() function is a pattern, not a literal string. In Lua patterns, the - character is a quantifier and indicates to match 0 or more occurrences of the preceding character, using the shortest match possible. It is the non-greedy version of the * character quantifier. I always struggle to remember that the - character is special because most Lua pattern syntax matches regular expression syntax, but the - character is one exception.

     

    A very good quick reference for Lua 5.1 (and one that I use frequently when coding in Lua) is available here:

     

    http://lua-users.org/wiki/LuaShortReference