Question

I have a problem of understanding with the use of a background process.

I have to convert a logic of treatment which is on Linux towards Windows. I have a script which in its treatment, launche among others, another script in background, and pursues its treatment. The script which throws(launches) the treatment background does not have to care about the result of this one thus has to end even if the treatment in backgroud still execute.

What I experimented in Powershell is when my first script ends, it looks like the processing of the script thrown in background breaks off.

Here is an example of the tests I have made in my environment (PowerShell version 2.0)

Here's the simple code of my first script script_1.ps1:

start-job -filepath d:\script\script_2.ps1  
sleep 3  
get-item d:\log\log

And my second script script_2.ps1:

sleep 1  
"test" > d:\log\log-1.log  
sleep 1  
"test" > d:\log\log-2.log  
sleep 1  
"test" > d:\log\log-3.log  
sleep 1  
"test" > d:\log\log-4.log  
sleep 1  
"test" > d:\log\log-5.log

Here's what's happen when I submit it:

C:\Documents and Settings\user1>Powershell -command "D:\script\script_1.ps1"

Id Name   State   HasMoreData   Location   Command    
-- ----   -----   -----------   --------   -------  
1  Job1   Running True          localhost  sleep 1...

LastWriteTime : 2013-10-10 10:19:13
Length        : 14
Name          : log-1.log


LastWriteTime : 2013-10-10 10:19:14
Length        : 14
Name          : log-2.log

The result show me that when the first script terminate, (after 3 sec.) the second script stop also because we see that only the first 2 logs files were created.

I tought that it would be like on Linux (&) where the background job continue anyway. Is it the way it suppose to be with PowerShell or is somethings I do wrong?

No correct solution

OTHER TIPS

When you use the -Command switch to start PowerShell it runs the supplied command, and then exits the session. Jobs in PowerShell are specific to the session they're started in. You can see this for yourself if you want by starting two instances (sessions) of PowerShell. Create a job in one, e.g. start-Job {sleep 600}, and do get-Job, you'll see that a job is Running. If you type get-Job into the other PowerShell instance you'll see that there are no jobs.

The start-Job cmdlet is creating a job, but it doesn't wait for that job to complete, it simply starts it on its merry way. In your case what's happening is that start-Job is creating the job, you're waiting one second, then listing the contents of the log directory. Then you reach the end of the script and once that happens the session is terminated. This is why only some of your log files are created. The session is being ended because you've reached the end of the script, before the created job has had a chance to finish.

To use jobs in the manner you describe you need for the PowerShell session to remain started for the entirety of the job's lifetime. For that, you'll need the -NoExit parameter on your PowerShell command that runs the job. You may want to browse around powershell /? for other switches you might find useful. If you got extra creative with your script that starts the job you could eventually make a hidden, noninteractive session that automatically quit once the job was done. But baby steps.

On linux, the background job will keep on running only if launched with the nohup command, or the nohuponexit option is set in the shell (or the process is later disowned with the eponym command)

Hints on how to do the same with PS can be found in this thread of emails on freelist.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top