Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am using a windows machine as a ssh server. I want to remotely run some tasks which takes several hours to finish. Is there any way to have persistent process even after remote ssh session is disconnected in Powershell?

share|improve this question
    
What shell are you using? I think Screen works in Cygwin. –  paradroid Sep 9 '13 at 22:39
    
I'm using powershell - I personally prefer not to use Cygwin. SSH server is Bitvise. –  joon Sep 9 '13 at 22:42

4 Answers 4

What you are looking for are PSSessions.

From the about_pssessions page on technet (http://technet.microsoft.com/en-us/library/hh847839.aspx)

However, unlike the session that starts automatically, you can control the PSSessions that you create. You can get, create, configure, and remove them, disconnect and reconnect to them, and run multiple commands in the same PSSession. The PSSession remains available until you delete it or it times out.

share|improve this answer
    
Thanks for the reply. I have not been able to New-PSSession without using administrator in my local machine, thought. –  joon Sep 12 '13 at 18:46
    
Okay now I can enter-pssessions withaout administrative privileges, but it seems the sessions are not persistent; when I log-out and log back int, I do not see anything with Get-PSSession. –  joon Sep 12 '13 at 19:47
1  
According to the docs on disconnected sessions, this is a new feature in Powershell 3.0. technet.microsoft.com/en-us/library/… –  Eris Sep 13 '13 at 22:50

Use the RunasJob parameter via a PSSession:

(from Help about_remote_jobs)

START A REMOTE JOB THAT RETURNS THE RESULTS TO THE LOCAL COMPUTER (ASJOB)

To start a background job on a remote computer that returns the command results to the local computer, use the AsJob parameter of a cmdlet such as the Invoke-Command cmdlet.

When you use the AsJob parameter, the job object is actually created on the local computer even though the job runs on the remote computer. When the job is completed, the results are returned to the local computer.

You can use the cmdlets that contain the Job noun (the Job cmdlets) to manage any job created by any cmdlet. Many of the cmdlets that have AsJob parameters do not use Windows PowerShell remoting, so
you can use them even on computers that are not configured for
remoting and that do not meet the requirements for remoting.

STEP 1: INVOKE-COMMAND -ASJOB

The following command uses the AsJob parameter of Invoke-Command to start a background job on the Server01 computer. The job runs a Get-Eventlog command that gets the events in the System log. You can use the JobName parameter to assign a display name to the job.

  invoke-command -computername Server01 -scriptblock {get-eventlog system} -asjob

The results of the command resemble the following sample output.

  SessionId   Name    State      HasMoreData     Location   Command
  ---------   ----    -----      -----------     --------   -------
  1           Job1    Running    True            Server01   get-eventlog system

When the AsJob parameter is used, Invoke-Command returns the same type of job object that Start-Job returns. You can save the job object in a variable, or you can use a Get-Job command to get the job.

Note that the value of the Location property shows that the job ran on the Server01 computer.

STEP 2: GET-JOB

To manage a job started by using the AsJob parameter of the Invoke-Command cmdlet, use the Job cmdlets. Because the job object that represents the remote job is on the local computer, you do not need to run remote commands to manage the job.

To determine whether the job is complete, use a Get-Job command. The following command gets all of the jobs that were started in the current session.

   get-job

Because the remote job was started in the current session, a local Get-Job command gets the job. The State property of the job object shows that the command was completed successfully.

  SessionId   Name   State      HasMoreData     Location   Command
  ---------   ----   -----      -----------     --------   -------
  1           Job1   Completed  True            Server01   get-eventlog system

STEP 3: RECEIVE-JOB

To get the results of the job, use the Receive-Job cmdlet. Because the job results are automatically returned to the computer where the job object resides, you can get the results with a local Receive-Job command.

The following command uses the Receive-Job cmdlet to get the results of the job. It uses the session ID to identify the job. This command saves the job results in the $results variable. You can also redirect the results to a file.

  $results = receive-job -id 1
share|improve this answer
    
The problem with this is that it seems when I exit the current local session, the jobs are not preserved. –  joon Dec 31 '13 at 5:50

Since the OP is using ssh to connect to Powershell from a non-Windows machine, he can't use Powershell sessions. He would be running it as though it were connected to the local machine. The Start-Job cmdlet should do the trick:

Start-Job -scriptblock {script or command you wish to run as job}

share|improve this answer
    
He can ssh in, and start a new pssession after he logs in. Start-Job doesn't let him resume an interactive session later, it just runs a command in the background. –  Eris Sep 11 '13 at 18:21
    
I tried this, but when I exit from the PowerShell session, the process stops as well. –  joon Dec 16 '13 at 1:28
up vote 1 down vote accepted

After some experimentation, now I have replaced ssh with PowerShell Remoting.

Especially, START A REMOTE JOB THAT KEEPS THE RESULTS ON THE REMOTE COMPUTER section of the about_Remote_Jobs has been really helpful.

Basically, my current workflow is

  1. Open a new PSSession
  2. Invoke a command (Invoke-Command) on the remote server as a job, with Start-Job

Then I can freely Disconnect from the session and later reconnect to the session and get the job outputs with Receive-Job.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.