nohup runs a process in the background. There is any equivalent for Windows?
4 Answers
You can use the start command to run a process in the background of the command window.
command1
command2
start command3
command4
command2 waits until command1 finish but command4 don't wait that command3 finishes.
and if you need to run independently of the user logged on, you need to start the process as a service and you can use anysrv.exe
- 300
- 6
- 11
-
As far as I understand `START` command doesn't do a detach, i.e. it it still a child of the process that created it. In your example it will mean that 'command3' will die whenever the mother process exits. Unix circumvents this by doing was is known as a 'fork-twice' but I do not know of an equivalent in Windows. – unixhacker2010 Nov 08 '13 at 11:33
-
at least on Windows 7 the command3 will NOT die, for example u can get into a cmd prompt "start notepad" then exit and notepad will still be running – Aragorn Nov 08 '13 at 19:11
-
Yep, you are right. However I was testing from within another language where all I can is `exec(
)`. I can clearly see in [Process Explorer](http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx) the parent-child relationship and whatever I start from within `exec()` doesn't survive. Correct that it seems to work if I start a `cmd.exe` from Start Menu where the started process seems to be orphaned 'correctly' when `cmd.exe` exits. Clearly I need to understand this better. :-( – unixhacker2010 Nov 16 '13 at 10:12
You might want to look at windows services. There are some tools you can download to host any process as a windows service. This causes the process to load in the background at windows startup, so provided it doesn't require user interaction you should be able to host it like this.
Windows Server 2003 Resource Kit
The tool you're after is called srvany.exe.
- 670
- 3
- 9
- 19
The only way, in Windows, that you can have a process started by a user continue running after logoff (i.e. what "nohup" does) is to start it either through a "scheduled task" or as a Windows service. When the user logs off all processes in their logon session will be killed.
If you're game to try the "Scheduled Tasks" method, you'll want to know how to create them programmatically. The Win32_ScheduledJob WMI class can do it. Documentation is provided in detail here: http://www.microsoft.com/technet/scriptcenter/guide/sas_man_rsxs.mspx?mfr=true Basically, you're looking at doing (shamelessly stolen from Microsoft):
Set objService = GetObject("winmgmts:\\.")
Set objNewJob = objService.Get("Win32_ScheduledJob")
errJobCreated = objNewJob.Create("Program-to-execute.exe", "ugly-formatted-time-string-per-Microsoft-docs",True ,1 OR 4 OR 16, , ,JobID)
If Err.Number = 0 Then
Wscript.Echo "New Job ID: " & JobID
Else
Wscript.Echo "An error occurred: " & errJobCreated
End If
To grant "joe user" the ability to create scheduled tasks, you'll have to modify the permission on the %SystemRoot%\Tasks folder. See here for some info on that front: http://technet.microsoft.com/en-us/library/cc785125(WS.10).aspx
- 141,071
- 19
- 191
- 328
-
What's the advantage of doing this vs using schtasks.exe or the Task Scheduler GUI? – Helvick Jul 12 '09 at 06:43
-
There's not much of a functional difference between this API and schtasks.exe. The advantage of the GUI is that you can do this programmatically. – Evan Anderson Jul 12 '09 at 07:17
This all depends on what your ultimate purpose is. You could run a scheduled task with the option to run only if the user is logged on set to DISABLED. You could possibly use psexec from a remote machine. Better yet might be to run the process as a service. Check out this Google search, this other Google search, this thread, and this other thread for some possible leads in your search for a solution. Ultimately it appears that there is no exact equivalent to nohup on a Windows machine.
- 32,320
- 9
- 80
- 116
-
My ultimate purpose is to hide the process from the user. Hide the windows that it spawns. – Jader Dias Jul 12 '09 at 01:48