Starting a Background Java Process on Windows

Hello All–

I’m trying to get a couple of jars up and running in the background on some Windows machines. My expected result is at the end of the play I have both jars running with logs for both, and they’re listening on ports 4444 and 24240 respectively. Here’s my tasks:

`

  • raw: Start-Process javaw -ArgumentList ‘-jar C:\Users\Administrator\mySweetJar.jar’
  • raw: Start-Process javaw -ArgumentList ‘-Dwebdriver.chrome.driver=C:\Windows\System32\chromedriver.exe -jar C:\selenium.jar -browserSessionReuse -browserTimeout 300 -timeout 300000 -maxSession 1 -log C:\Users\Administrator\node.log’
    `

What actually happens is that Ansible reports the commands running successfully:

`
TASK [Starting MySweetJar…] ****************************************************
task path: /Users/allen.fisher/projects/setup-windows-SUT/playbook.yml:61
<XX.XX.XX.XX> ESTABLISH WINRM CONNECTION FOR USER: administrator on PORT 5985 TO XX.XX.XX.XX
<XX.XX.XX.XX> EXEC Start-Process javaw -ArgumentList ‘-jar C:\Users\Administrator\MySweetJar.jar’
ok: [XX.XX.XX.XX] => {“changed”: false, “invocation”: {“module_args”: {“_raw_params”: “Start-Process javaw -ArgumentList ‘-jar C:\Users\Administrator\MySweetJar.jar’”}, “module_name”: “raw”}, “rc”: 0, “stderr”: “”, “stdout”: “”, “stdout_lines”: }

TASK [Starting Selenium…] ****************************************************
task path: /Users/allen.fisher/projects/setup-windows-SUT/playbook.yml:63
<XX.XX.XX.XX> ESTABLISH WINRM CONNECTION FOR USER: administrator on PORT 5985 TO XX.XX.XX.XX
<XX.XX.XX.XX> EXEC Start-Process javaw -ArgumentList ‘-Dwebdriver.chrome.driver=C:\Windows\System32\chromedriver.exe -jar C:\selenium.jar -browserSessionReuse -browserTimeout 300 -timeout 300000 -maxSession 1 -log C:\Users\Administrator\node.log’
ok: [XX.XX.XX.XX] => {“changed”: false, “invocation”: {“module_args”: {“_raw_params”: “Start-Process javaw -ArgumentList ‘-Dwebdriver.chrome.driver=C:\Windows\System32\chromedriver.exe -jar C:\selenium.jar -browserSessionReuse -browserTimeout 300 -timeout 300000 -maxSession 1 -log C:\Users\Administrator\node.log’”}, “module_name”: “raw”}, “rc”: 0, “stderr”: “”, “stdout”: “”, “stdout_lines”: }
`

But the ports are not open, and there are no logs so it’s almost like Ansible is killing the process before it even gets going.

The commands run as I would expect on the box directly. I’ve tried bundling them into a PowerShell script and using the script module to execute the commands, I’ve tried both java.exe and javaw.exe with no success. Does anyone have a way to accomplish what I’m after?

Thanks, Allen

You’re partially right: it’s not Ansible, but WinRM that’s killing the process.

WinRM runs everything under a Windows job object, and that job object gets terminated when the connection is closed (ie, when Ansible finishes that task), which causes all processes spawned under it to be terminated as well.

Ansible 2.2 is scheduled to ship with Windows async support, as well as the win_command/win_shell modules, which currently support a “breakaway” parameter to allow you to do exactly what you’re trying to do here (spawn a process that will run longer than the Ansible WinRM session by explicitly breaking away from WinRM’s job object).

If you need to do it sooner than that, you might want to look into the win_scheduled_task module, which allows you to run commands/scripts outside the constraints of WinRM.

-Matt

Thanks, Matt.

What I ended up doing was using NSSM and installing them both as services. That seems to be working well for at least one of them thus far.

Looking forward to async support in 2.2, though.

Allen