Run a program as windows service or background job

nssm

Download nssm from http://nssm.cc/download and follow its usage to install custom program as service.

instsrv

  • instsrv myProgram “c:\Program Files\Windows Resource Kits\Tools\srvany.exe”
  • install_service.reg
    1
    2
    3
    4
    5
    Windows Registry Editor Version 5.00
    [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\myProgram\Parameters]
    "AppDirectory"="d:\\bin\\myProgram"
    "Application"="D:\\bin\\myProgram\\myProgram.exe"
    "AppParameters"="-d -f d:\\MyWorkDir\\myProgram.ini"
  • sc config myProgram depend= Tcpip
  • net start myProgram or sc start myProgram

java

1
2
3
4
5
6
7
public class Run{
public static void main(String[] args) throws InterruptedException, IOException {
Process pr = Runtime.getRuntime().exec("D:\\bin\\myProgram\\myProgram.exe -d -f d:\\MyWorkDir\\myProgram.ini");
pr.waitFor();
}
}
javaw -cp . Run

wscript

run.vbs

1
2
3
Set WshShell = WScript.CreateObject("WScript.Shell")
obj = WshShell.Run("plink.exe -load remotes_server -N", 0)
set WshShell = Nothing

wscript run.vbs

at

if the session is closed such as remote desktop(mstsc), above program will be shutdown. But schedule job has no this problem at 17:50 wscript run.vbs then delete this scheduled job

run nohup in cygwin

nohup wscript run.vbs 2>&1 &