2

I've got a batch file that I want to run in the background whenever the system (Windows 2007) is turned on. The batch file monitors the task list for a given program, and when it sees that it's shut down, prompts the user to de-licence it.

I'm currently trying to do this without converting the batch file into either an executable or Windows service file.

I've found more online references than I can count which tell me that I should use "start /b file.bat" to run the batch file in the background, but this doesn't work for me, it just starts up the batch file in the same cmd line window that I'm using.

Can anyone suggest either what's going wrong, or even better; a nice simple way for me to get the batch file to run ion start up (I cannot use a GUI as I have to roll this out to several computers remotely)

Thanks

WRJ
  • 617
  • 3
  • 9
  • 19
  • the largest Windows number ever released is 98, 2000 and 2003. There's no Windows 2007 – phuclv Jan 30 '18 at 03:09
  • Possible duplicate of [Execute Batch File without Command line visible](https://stackoverflow.com/questions/23557720/execute-batch-file-without-command-line-visible) – phuclv Jan 30 '18 at 03:10

2 Answers2

3

You could make a shortcut to your batch file and place the shortcut in your Startup Programs directory:

C:\Users\\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

Since you have to roll this out to several computers remotely, you should be able to copy the batch file to the startup programs directory over the network assuming the remote machines have WinRM enabled and your account has adequate permissions.

If you want this batch file to run in the background at start up, you could reference your batch file from a VBScript (instead of using the batch file's short cut) and set the VBscript to run in invisible mode:

Set WshShell = CreateObject("WScript.Shell") 
WshShell.Run chr(34) & "C:\path\to\your\batchfile.bat" & Chr(34), 0
Set WshShell = Nothing

Just give this vbscript file the .vbs extension.

Adam
  • 151
  • 1
  • 5
  • Hi Adam, thanks but I've tried this; the problem is that it will run the batch file in the foreground. – WRJ Jan 29 '18 at 16:52
  • You could run your batch file from a [VBscript in invisible mode](http://www.winhelponline.com/blog/run-bat-files-invisibly-without-displaying-command-prompt/) and place the vbscript in the start up programs directory. I've updated my answer to include this method. – Adam Jan 29 '18 at 17:04
  • @WRJ, if this helped or solved your issue, would you mind voting up the answer? Thanks! – Adam Jan 29 '18 at 20:49
0

If the program you are concerned about is a GUI program (ie non console) just wait for it to exit. Batch waits for GUI programs to exit (but not when started interactively).

notepad
echo My notepad exited

Start /b says to start program in same window. See Start /?. Also Start is usually the wrong command to be using. It starts programs in abnormal ways. See end of http://stackoverflow.com/questions/41030190/command-to-run-a-bat-file/41049135#41049135 for how to start programs.

This is a VBS file.

This monitors for notepad exiting, pops up a messagebox, and restarts notepad.

Set WshShell = WScript.CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2") 
Set objEvents = objWMIService.ExecNotificationQuery("SELECT * FROM Win32_ProcessStopTrace")

Do
    Set objReceivedEvent = objEvents.NextEvent
    msgbox objReceivedEvent.ProcessName
    If lcase(objReceivedEvent.ProcessName) = lcase("Notepad.exe") then 
        Msgbox "Process exited with exit code " & objReceivedEvent.ExitStatus
        WshShell.Run "c:\Windows\notepad.exe", 1, false
    End If
Loop

This should use less battery power and CPU cycles. Batch files are read line by line so make VERY poor background tasks.

If the program you are concerned about is a GUI program (ie non console) just wait for it to exit. Batch waits for GUI programs to exit (but not when started interactively).

notepad
echo My notepad exited
ACatInLove
  • 175
  • 3