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

  1. instsrv myProgram “c:\Program Files\Windows Resource Kits\Tools\srvany.exe”

  2. install_service.reg

    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"
  3. sc config myProgram depend= Tcpip

  4. net start myProgram or sc start myProgram

java

    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

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 &

你可能感兴趣的:(Run a program as windows service or background job)