Starting a background process on Windows through an SSH connection that doesn't get stopped when SSH disconnects - windows

I have a java application that I'm trying to deploy to a Windows server via SSH (via Jenkins). I wrote a powershell script to start the process in the background using start-process and javaw:
$SSL_KEYSTORE_PASSWORD = $args[0]
Write-Output "Running war in the background"
Start-Process javaw -ArgumentList "-jar", `
"e:\app\app.war", `
$("-Dserver.ssl.key-store-password=" + $SSL_KEYSTORE_PASSWORD), `
"-Dserver.tomcat.basedir=e:\app" `
-RedirectStandardOutput "e:\app\output\stdout.txt" `
-RedirectStandardError "e:\app\output\stderr.txt"
Write-Output "Starting javaw instance"
When I run the script from the server, it works perfectly: the process runs in the background indefinitely and all the output goes to the right place. But, when I attempt to run it via SSH from jenkins, the java application will start up properly, but as soon as the powershell script ends, the process gets killed. Initially, I thought it was jenkins killing the process, but I did some debugging and figured out that it's getting killed as soon as it exits the powershell script, so I think it's related to SSH. This is the line in the jenkinsfile that's attempting to start up the process:
sh 'ssh -o StrictHostKeyChecking=no $ssh_target powershell e:\\app\\startup $SSL_KEYSTORE_PASSWORD'
I would like to start the process using a powershell script like this, because once I get this working, I'd like to add some other stuff into the powershell script to make sure it started up properly, and I don't want to clutter up the jenkinsfile even more than it already is.

Related

Can Start-Process start a process and let it run even when script ends in TFS?

I'm using Start-Process cmdlet as part of a Powershell script being executed by the new scriptable TFS build system.
My issue is that I'm starting some executables from my Powershell script and once the build step ends, it kills started processes.
I've also also tried to use ProcessStartInfo directly and Start-Job with no luck.
When I run that script alone it ends, but it leaves the started processes opened.
Is there any way to solve this?
You can use start-Job:
Start-Job -ScriptBlock { start C:\Windows\notepad.exe }
After exiting the PS, Notepad is still open
Check this post for more info:
if you start a script using Start-Process, it will survive the shell
termination, but if you started it from a console window then it stays
bound to that window and closing the window will terminate the process

remote execution(PowerShell) of cmd file doesn't complete

Current PS script:
Invoke-Command -ComputerName RemoteServer007.FQDN.com -ScriptBlock {
Set-Variable -Name WOWCONFIG -value "d:\ABCs\WOWzers" `
| Start-Process "d:\da-folder\Do-It-NOW-Pleez.cmd"
}
If I log on locally to the server(RemoteServer007.FQDN.com) and execute the cmd file, it runs through all of the lines(commands) within the cmd file.
When I execute it remotely, it gets about 30% of the way through the commands within the cmd file, the PS execution ends without error, but not all of the lines/commands in the cmd file had been executed.
This was discovered by simply configuring each line of the cmd file to output to txt files.
I even tried re-ranging the commands in the cmd file, thinking that perhaps there was a specific command that was causing it to exit, but that is not the case.
I'm wondering if there is some timeout or response that PowerShell is not getting? and just quitting almost immediately after starting?
Any ideas or help would be greatly appreciated.
There are a couple of things you can do here:
You may have a memory issue. Increasing the value of MaxMemoryPerShellMB might help
set-item WSMan:\$target\Shell\MaxMemoryPerShellMB -Value 0 -Force
You'd need to run this once on the remote machine before you execute your commands again.
You can also see possible error logs in the windows event viewer. There are categories for powershell and for Windows Remote Management which you should look at.
Finally, you can just run this process asynchronously, using the task scheduler for instance. I had a similar problem with windows in the past, and running the process from the task scheduler, outside the powershell session, fixed it. There's an example of how we did this in Cloudify here:
https://github.com/CloudifySource/cloudify/blob/master/esc/src/main/resources/clouds/ec2-win/upload/bootstrap-management.ps1#L220

Running powershell script from CMD does not load module

So i have fairly easy powershell script that contains following:
import-module activedirectory
Get-ADUser -Filter *
remove-module activedirectory
If i run it from powershell it runs OK, but when i try to call it from CMD nothing happens, it just opens powershell and thats it. I am using following command to run it:
powershell.exe -file "D:\test.ps1"
I noticed also following thing, 2 powershell.exe processes run after i execute this. If i exit from CMD from one powershell then i start seeing lists that this PS query should be returning. Is there a way to get this working since i am trying to run ps script as scheduled job. The crucial part here is import module when i run it over cmd which is not happening for some reason.
It's powershell 2.0 running on Windows 2008R2. I tried this script on win 2012r2, works fine from CMD... Looks like ps 2.0 limitation?
Could be a couple of things going on here. Since your windows opens and closes you wont get to see any errors that might be occurring. What is your ExecutionPolicy set to? Get-ExecutionPolicy
When I make scheduled tasks of my scripts I usually set up my action as such
Program/Script = %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe
Arguments = -ExecutionPolicy Unrestricted -NoProfile -File C:\data\script.ps1
Start In = %SystemRoot%\system32\WindowsPowerShell\v1.0
Also, I don't believe it matters in this case but be sure you have the script "Running with highest privilege" if required.

Running a windows batch command using chef blocks the chef provision

I'm using chef for windows and need to run a batch file that starts up the selenium-server java service (java -jar seleniumserver.jar) as a daemon. When I try to use a windows_batch resource, it causes chef to hang during it's provisioning.
The problem is that the selenium server stays running in whatever command line you start it in, but chef won't continue provisioning the machine until the command is finished. The thing is, the command never finishes, it's not supposed to.
Here's what I've tried so far:
Executing java -jar seleniumserver.jar & (with the windows_batch resource)
Using a template to create a batch file for the command, then using windows_batch to execute file.bat
Using windows_batch to execute the batchfile with an & (file.bat &)
I'm open to any ideas, even if it's kind of hacky. Thanks in advance!
If I understand the question correctly, you can start a separate process so the main batch file ends. For example:
start java -jar seleniumserver.jar
You can control several execution parameters through the different start options.
Ending command lines with & does not do the same as *nix.
async process on windows that doesn't block chef run
batch "run" do
code 'powershell -c "start-process notepad.exe"'
end
The following is an example of using start-process to start a command with arguments.
Try running the command by itself in the command prompt to familiarize yourself with its output.
net statistics server
Now run it using powershell and start-process in the command prompt and verify the correct output shows in c:\output.txt. Please pay careful attention to the use of single or double quotes. My experience says start-process won't work with double quotes for some reason.
powershell -c start-process net -ArgumentList 'statistics workstation' -RedirectStandardOutput c:\output.txt
Now put the following in a Chef execute resource and run it. Again, be aware of the single quotes, double quotes and escaped single quotes.
execute "run" do
command 'powershell -c "start-process net -ArgumentList \'statistics workstation\' -RedirectStandardOutput c:\chef-output.txt"'
end
You should find the correct output in c:\chef-output.txt.

kill remote job initiated by invoke-command

what is the easy way to kill remote job initiated by invoke-command with background processes?
I kick remote PS script to run legacy exe file and need a way to kill that process.
I was going to use stop-job first but as I am reading it works only for local processes.
Then I was going to use Remove-job -force command but if I understood right, it cannot kill running process until it completes (again my remote ps script will start another process to run exe file).
I am reading I can kill a process using wmi terminate command but I do not know how to get PID of remote process (I cannot use name because I can kill processes from other users)
what is the best way to achieve my goal?
my script looks like that:
invoke-command -computername server1,server2,server3..etc -script-block {my.exe} -asjob
loop to wait for all processes to complete and report progress
check for CTRL-C press and kill all remote instances on {my.exe}
ok checked this morning from work and this works fine from a calling script:
get-job | remove-job -force
I was confused by MSDN doc which says:
When you stop a background job, Windows PowerShell **completes** all tasks that are pending in that job queue and then ends the job
Check out this answer, you can add the start process command to your script block and return the pid or save it to a text file to be reference later.
PowerShell - get process ID of called application

Resources