Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run batch file in the background

Tags:

batch-file

I have a batch file, this batch file will not start automatically, it will only run when i double click on it.
Can I run the batch file in the background when I double click on it.

like image 414
Ghyath Serhal Avatar asked Jan 12 '11 07:01

Ghyath Serhal


People also ask

How do I run a batch file silently?

1] Hidden Start or HStartDrag, and drop the batch file onto the interface. Choose options including hiding console windows, UAC, and so on. You can also test it using test mode. You can also add command-line options if needed.

How do I run a batch file minimized?

Run batch file minimized from Windows Task Scheduler But if you want to run it minimized, then you have to make adjustments in the Actions tab. “^& exit” at the end is necessary to close the CMD window after the batch file is executed.

Can CMD run in background?

If you want to run additional commands while a previous command runs, you can run a command in the background. If you know you want to run a command in the background, type an ampersand (&) after the command as shown in the following example. The number that follows is the process id.

How do I run a Windows script in the background?

start /B command is the most given answer, but the command will be closed when the terminal closed. and then CTRL C, close the terminal, the command will continue to run in background.


1 Answers

Well, you can start it minimized with start, if that is enough. Really hiding it is difficult (although I can think of an option right now).

Basically you need to determine whether the batch has been started by double-clicking it. You can do this by defining a special variable and look for it:

@echo off
if not defined FOO (
  set FOO=1
  start /min "" %~0
  exit /b
)

rem here whatever you wanted to do originally in the batch

As long as the FOO variable isn't defined (which is probably the default almost everywhere), this batch will launch itself minimized again, but with the variable defined first. Environments are passed to subprocesses, which is why this works.

like image 130
Joey Avatar answered Nov 15 '22 10:11

Joey