If I want to run a Powershell script (v. 7.1.5), detached, as in I can close the cmd window, log off, come back, log in, and it still be running like in Linux, is that possible without using the task scheduler?
I have seen a lot of questions on SO about this, but nothing seems to work like & or nohup in Linux. I am on Windows Server 2016, if that matters. I have seen this (and many others on SO):
Powershell equivalent of bash ampersand (&) for forking/running background processes
If I want to run this,
foreach($n in (0..100)) {
write-host "Hello World $n"
start-sleep -seconds 2
}
I can see the job and what it produces with receive-job [job_number]
. If I close my session window, it dies. I thought if I used:
start-job -name BackgroundJob -FilePath C:\temp\test.ps1
it would stay and I also tried .\test.ps1 &
The script always dies when I close my cmd window.
- How do I keep everything running?
- I could do a Scheduled task to run the script every minute and not run it if it is already running, but could that bog down the server checking that much?
- I could also hide the window when starting, but this doesn't really seem to be the same thing (is it?)
- Is nohup and & in Linux fooling me by simply running something and hiding the window (what if I log out?