Implement ‘nohup’ like functionality in Windows PowerShell

Implement ‘nohup’ like functionality in Windows PowerShell post thumbnail image

In Linux operating system there is a very useful utility named ‘nohup‘ which can let the command running in background even if the user logs out of the terminal/command line. The utility is very useful if there is a command which takes too much time and need to run in background irrespective of the session timeout or session disconnection due to network failure etc.

In windows, currently there is not such functionality as a single command but with some search and finding, I could articulate a solution in PowerShell which is very close to nohup utility in Linux.

For running a command in a session which isn’t connected to any of sessions and doesn’t log out automatically first we have to set the PowerShell setting for session timeout value to be maximum value. For setting PowerShell session timeout value to maximum we can run following command:

Set-Item WSMan:\localhost\shell\IdleTimeout 2147483647

After setting the maximum timeout value which is almost 28 days. One can run the following command in PowerShell to run your executable script by replacing it at <<CMD>> you may skip -credential block if authentication isn’t needed and job can run as anonymous user, else provide the domain name and username at <<domain>> and <<username>> :

Invoke-Command -SessionOption @{IdleTimeout=21474836479999} -Computer localhost -InDisconnectedSession -Credential <<domain>>\<<username>> {
    Start-Job -ScriptBlock { <<CMD>> }
}

When providing the <<domain>> and <<username>> it will ask for password which need to be provided in order to authenticate the session.

One should remember that this command don’t create nohup.out like output file so one need to embed writing to log file in the <<CMD>> section using output redirection directives after command line.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post