Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 548 Vote(s) - 3.54 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Start a detached background process in PowerShell

#1
I have a Java program which I would like to launch as a background process from a PowerShell script, similar to the way a daemon runs on Linux. The PowerShell script needs to do a couple of things:

1. Run the program as a separate and detached process in the background, meaning the parent window can be closed and the process keeps running.
2. Redirect the program's standard output and standard error to files.
3. Save the PID of the background process to a file so it can be terminated later by another script.

I have a shell script on Linux which starts the program like so:

$ java -jar MyProgram.jar >console.out 2>console.err &

I'm hoping to replicate the same behavior on Windows using a PowerShell script. I have tried using `Start-Process` with various combinations of options, as well as creating `System.Diagnostics.ProcessStartInfo` and `System.Diagnostics.Process` objects, but so far I am not having any luck. PowerShell starts the program as a background process, but the program abruptly terminates when the DOS window which started the PowerShell session is closed. I would like it to start in the background and be independent of the command window which started it.

The output redirection has also been troublesome, as it seems that the output and error streams can only be redirected in the process is being run in the same window (e.g., using `-NoNewWindow`).

Is this sort of thing possible in PowerShell?
Reply

#2
Consider using the task scheduler for this. Define a task and set it without any triggers. That will allow you to simply "Run" (manually trigger) the task.

You can set up and/or trigger scheduled tasks using the ScheduledTasks powershell module, or you can use the GUI.
Reply

#3
Use [jobs][1] for this:

Start-Job -ScriptBlock {
& java -jar MyProgram.jar >console.out 2>console.err
}

Another option would be [`Start-Process`][2]:

Start-Process java -ArgumentList '-jar', 'MyProgram.jar' `
-RedirectStandardOutput '.\console.out' -RedirectStandardError '.\console.err'

[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#4
This is an old post but since I have it working fine thought it might help to share. Its the call to 'java' instead of 'javaw' that is likely your issue. Ran it out myself using my JEdit java program through powershell to launch it.

#Requires -Version 3.0
$MyDriveRoot = (Get-Location).Drive.Root
$JEditDir = $($mydriveroot + "jEdit") ;# Should be C:\jEdit or wherever you want. JEdit is a sub-directory.
$jEdit = $($JEditDir + "\jedit.jar" )
$jEditSettings = $($JEditDir + "\settings")
$JEditLogs = $($JEditDir + "\logs")

Start-Process -FilePath javaw -ArgumentList ( '-jar',"$jEdit", '-settings="$JEditSettings"' ) -RedirectStandardOutput "$JEditLogs\console.out" -RedirectStandardError "$JEditLogs\console.err"

Which you can turn into a little function and then an alias to make it easy to launch in Powershell.

If ( ( Test-Path $jedit) ) {
Function Start-JEdit() {
Start-Process -FilePath javaw -ArgumentList ( '-jar',"$jEdit", '-settings="$($mydriveroot + "jEdit\settings")"' ) -RedirectStandardOutput "$JEditLogs\console.out" -RedirectStandardError "$JEditLogs\console.err"
}
New-Alias -Name jedit -Force Start-JEdit -Description "Start JEdit programmers text editor"
}
Reply

#5
Old question, but since I had the same goal, I used answer from [@use](

[To see links please register here]

) to acheive it.

So here is my code :)

```powershell
$NAME_TASK = "myTask"
$NAME_TASKPATH = "\myPath\"

if ($args[0] -eq "-task") {
# Code to be run "detached" here...
Unregister-ScheduledTask -TaskName $NAME_TASK -TaskPath $NAME_TASKPATH -Confirm:$False
Exit
}

$Task = (Get-ScheduledTask -TaskName $NAME_TASK -TaskPath $NAME_TASKPATH -ErrorAction 'SilentlyContinue')
if ($Task) {
Write-Host "ERR: Task already in progress"
Exit 1
}

$A = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-ExecutionPolicy bypass -NoProfile -Command ""$PSCommandPath -task $args"""
Register-ScheduledTask -TaskName $NAME_TASK -TaskPath $NAME_TASKPATH -Action $A | Start-ScheduledTask
````
Reply

#6
Try this with PowerShell:

```none
Start-Process cmd -Args /c,"java -jar MyProgram.jar" `
-WindowStyle Hidden -RSI console.out -RSE console.err
```
OR
```none
Start-Process cmd -Args /c,"java -jar MyProgram.jar >console.out 2>console.err" `
-WindowStyle Hidden
```

This will start a detached cmd window that is hidden, and will redirect the std streams accordingly.
Reply

#7
The solution is to combine ```Start-Process``` with ```nohup```:

[To see links please register here]


(Note: This is NOT for Windows.)
Reply

#8
To complete [@Ansgar's answer](

[To see links please register here]

), and provide a complete one-liner that you can run in your terminal without having to modify the format at all:

Here is the command you can launch if you only want to launch a process and detach it (just like if you were e.g. launching it from the Start Menu) and don't care about the outputs (`stdout` and `stderr`) =>

```pwsh
Start-Job -ScriptBlock { & command >$null 2>$null }
```

###### *PS: Text in orange needs to be replaced with your own values*
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through