- How to run a command in the background on Windows?
- 5 Answers 5
- How can I execute a Windows command line in background?
- 11 Answers 11
- Apps are launching behind the Files UWP app window #985
- Comments
- Taras-Parfeniuk commented Jun 10, 2020 •
- XPoppyX commented Jun 10, 2020
- Jaiganeshkumaran commented Jun 15, 2020
- yaichenbaum commented Jun 15, 2020
- lampenlampen commented Jun 16, 2020
- yaichenbaum commented Jun 17, 2020
- Jaiganeshkumaran commented Jun 17, 2020
- XPoppyX commented Jun 17, 2020
- Jaiganeshkumaran commented Jun 20, 2020
- FibYar commented Jun 20, 2020
- duke7553 commented Jun 25, 2020
- XPoppyX commented Jun 25, 2020
- duke7553 commented Jun 25, 2020
- gave92 commented Jul 2, 2020 •
- jonnypjohnston commented Jul 2, 2020 •
- duke7553 commented Jul 2, 2020
- gave92 commented Jul 3, 2020 •
- Run in the background indefinitely
- Run while minimized
- Run background tasks indefinitely
How to run a command in the background on Windows?
In linux you can use command & to run command on the background, the same will continue after the shell is offline. I was wondering is there something like that for windows…
5 Answers 5
I’m assuming what you want to do is run a command without an interface (possibly automatically?). On windows there are a number of options for what you are looking for:
Best: write your program as a windows service. These will start when no one logs into the server. They let you select the user account (which can be different than your own) and they will restart if they fail. These run all the time so you can automate tasks at specific times or on a regular schedule from within them. For more information on how to write a windows service you can read a tutorial online such as (http://msdn.microsoft.com/en-us/library/zt39148a(v=vs.110).aspx).
Better: Start the command and hide the window. Assuming the command is a DOS command you can use a VB or C# script for this. See here for more information. An example is:
You are still going to have to start the command manually or write a task to start the command. This is one of the biggest down falls of this strategy.
How can I execute a Windows command line in background?
How can I execute a windows command line in the background, without it interacting with the active user?
11 Answers 11
Your question is pretty vague, but there is a post on ServerFault which may contain the information you need. The answer there describes how to run a batch file window hidden:
You could run it silently using a Windows Script file instead. The Run Method allows you running a script in invisible mode. Create a .vbs file like this one
and schedule it. The second argument in this example sets the window style. 0 means «hide the window.»
This is a little late but I just ran across this question while searching for the answer myself and I found this:
which, on Windows, is the closest to the Linux command:
From the console HELP system:
One problem I saw with it is that you have more than one program writing to the console window, it gets a little confusing and jumbled.
To make it not interact with the user, you can redirect the output to a file:
I suspect you mean: Run something in the background and get the command line back immediately with the launched program continuing.
Which is the Unix equivalent of
the above one is pretty closer with its Unix counterpart program &
You can use this (commented!) PowerShell script:
Save it as a .ps1 file. After enabling script execution (see Enabling Scripts in the PowerShell tag wiki), you can pass it one or two strings: the name of the executable and optionally the arguments line. For example:
I confirm that this works on Windows 10.
This is how my PHP internal server goes into background. So technically it should work for all.
A related answer, with 2 examples:
- Sometimes foreground is not desireable, then you run minimized as below:
call START /MIN «my mongod» «%ProgramFiles%\MongoDB\Server\3.4\bin\mongod.exe»
Hope that helps.
If you want the command-line program to run without the user even knowing about it, define it as a Windows Service and it will run on a schedule.
You can see the correct way to do this in this link:
Summarizing, you have to checkbox for ‘Run whether user is logged on or not’. Task user credentials should be enter after pressing ‘Ok’.
I did this in a batch file: by starting the apps and sending them to the background. Not exact to the spec, but it worked and I could see them start.
Apps are launching behind the Files UWP app window #985
Comments
Taras-Parfeniuk commented Jun 10, 2020 •
Describe the bug
If Windows Terminal set as default terminal app, after a click on ‘Open in Terminal’ context menu option the terminal window opens behind the Files UWP window, and the Files UWP window remains in focus.
To Reproduce
Steps to reproduce the behavior:
- Go to ‘Settings / Preferences’
- Select ‘Windows Terminal’ in ‘Terminal Applications’ dropdown
- Go to any directory
- Open right click context menu
- Click on ‘Open in Terminal’ option
- The Windows Terminal window opens, but the Files UWP window remains in focus
Expected behavior
A newly opened terminal window should be focused like it is for CMD terminal application.
Screenshot
Desktop (please complete the following information):
- OS Version: Windows 10 1909 (Build: 18363.900)
- Files UWP Version: 0.9.2.0
- Windows Terminal Version: 1.0.1401.0
The text was updated successfully, but these errors were encountered:
XPoppyX commented Jun 10, 2020
for me any file opens behind the app
Jaiganeshkumaran commented Jun 15, 2020
@XPoppyX Same for me. I think it’s a bug.
yaichenbaum commented Jun 15, 2020
When opening the app we need to set it to be focused, I haven’t looked into it but I would assume it’s a simple parameter.
lampenlampen commented Jun 16, 2020
for me only for the first or second time, launching the terminal, the terminal is in the background. After the third time it always opens in the foreground
yaichenbaum commented Jun 17, 2020
I have not been able to reproduce this issue, @XPoppyX @Jaiganeshkumaran @lampenlampen what version of Windows are you running? Perhaps the behavior changed with 2004 and that is why I cannot reproduce this.
Jaiganeshkumaran commented Jun 17, 2020
@yaichenbaum I am using Windows 10 Insider Preview Fast Ring.
XPoppyX commented Jun 17, 2020
in the microsoft store version it does happen, but with the last changes to the master branch I can no longer reproduce this issue
Jaiganeshkumaran commented Jun 20, 2020
If you’re using Windows 10 Insider Preview, the issue of apps opening in the background has been fixed with Build 20150.
FibYar commented Jun 20, 2020
If you’re using Windows 10 Insider Preview, the issue of apps opening in the background has been fixed with Build 20150.
Can it be fixed in «regular» Windows 10 (I use 19.09)?
duke7553 commented Jun 25, 2020
I noticed Command Prompt doesn’t have this issue. Can anyone else verify if this bug is with the Windows Terminal app only?
XPoppyX commented Jun 25, 2020
only with the terminal app for me
duke7553 commented Jun 25, 2020
We could always bring the Window to the foreground with Win32 APIs shortly after launching it.
@gave92 This sounds right up your ally. 😀
gave92 commented Jul 2, 2020 •
@duke7553 I’m not finding a good way of bringing the window to the front :/
Issue is finding what was launched:
- Launcher.LaunchFileAsync won’t return the process/window that was opened
- Process.Start(file_path) often returns null if the launched app was UWP
jonnypjohnston commented Jul 2, 2020 •
I have this same issue and also when opening a PDF file. Acrobat Pro opens in the background. I would add more details to this response to the email I received however, I have no idea where to look. I just really love this file manager!
JP Johnston ME CSWP
Rogue Exploration Technologies
duke7553 commented Jul 2, 2020
@gave92 So, you’re saying Process.MainWindowHandle would return null, so we cannot use it in BringWindowToTop() ?
gave92 commented Jul 3, 2020 •
@duke7553 The issue of launched app appearing on the background does not seem limited to the Window Terminal (#1270).
So I was hoping to find something that works in the general case (e.g. opening a file).
- Where we have a process object, like here, we can use Process.MainWindowHandle and bring the window to the top (this should work for windows terminal)
- In other places a process object is not available:
- like here we don’t get a process or window handle out of LaunchFileAsync
- and here we’ll get a null return value from Process.Start in case the file was launched in a UWP app
Edit: but perhaps we can start by fixing Window Terminal and think on the rest later 🙂
Edit 2: opened a PR with some test code that may solve this: @XPoppyX could you test this and see if it solves the issue? thanks!
Edit 3
The above PR #985 succeeded in consistently bringing the window to the front but I was not able to activate it (user had to click on it)
Run in the background indefinitely
To provide the best experience for users, Windows imposes resource limits on Universal Windows Platform (UWP) apps. Foreground apps are given the most memory and execution time; background apps get less. Users are thus protected from poor foreground app performance and heavy battery drain.
However, developers writing UWP apps for personal use (that is, side loaded apps that won’t be published in the Microsoft Store), or developers writing Enterprise UWP apps, may want to use all resources available on the device without any background or extended execution throttling. Line of business and personal UWP applications can use APIs in the Windows Creators Update (version 1703) to turn off throttling. Be aware that you can’t put an app into the Microsoft Store if it uses these APIs.
Run while minimized
UWP apps move to a suspended state when they are not running in the foreground. On desktop, this occurs when a user minimizes the app. Apps use an extended execution session in order to continue running while minimized. The extended execution APIs that are accepted by the Microsoft Store are detailed in Postpone app suspension with extended execution.
If you are developing an app that is not intended to be submitted into the Microsoft Store, then you can use the ExtendedExecutionForegroundSession with the extendedExecutionUnconstrained restricted capability so that your app can continue to run while minimized, regardless of the energy state of the device. В
The extendedExecutionUnconstrained capability is added as a restricted capability in your app’s manifest. See App capability declarations for more information about restricted capabilities.
Add the xmlns:rescap XML namespace declaration, and use the rescap prefix to declare the capability.
For more information, see the Restricted Capabilities section of App capability declarations.
When you use the extendedExecutionUnconstrained capability, ExtendedExecutionForegroundSession and ExtendedExecutionForegroundReason are used rather than ExtendedExecutionSession and ExtendedExecutionReason. The same pattern for creating the session, setting members, and requesting the extension asynchronously still applies: 
You can request this extended execution session as soon as the app comes to the foreground. Unconstrained extended execution sessions are not limited by energy quotas or by the operating system battery saver. As long as a reference to the session object exists, the app will stay in the running state and not enter the suspended state. If the app is closed by the user, the session will be revoked.
Registering for the Revoked event will enable your app to do any cleanup work required. In the suspending state, you can create an extended execution session with ExtendedExecutionReason.SavingData to save user data before the app is terminated and removed from memory.
Run background tasks indefinitely
In the Universal Windows Platform, background tasks are processes that run in the background without any form of user interface. Background tasks may generally run for a maximum of twenty-five seconds before they are cancelled. Some of the longer-running tasks also have a check to ensure that the background task is not sitting idle or using memory. In the Windows Creators Update (version 1703), the extendedBackgroundTaskTime restricted capability was introduced to remove these limits. The extendedBackgroundTaskTime capability is added as a restricted capability in your app’s manifest file:
Add the xmlns:rescap XML namespace declaration, and use the rescap prefix to declare the capability.
For more information, see the Restricted Capabilities section of App capability declarations.
This capability removes execution time limitations and the idle task watchdog. Once a background task has started, whether by a trigger or an app service call, once it takes a deferral on the BackgroundTaskInstance provided by the Run method, it can run indefinitely. If the app is set to Managed By Windows, then it still may have an energy quota applied to it, and its background tasks will not activated when Battery Saver is active. This can be changed with OS settings. More information is available in Optimizing Background Activity.
The Universal Windows Platform monitors background task execution to ensure good battery life and a smooth foreground app experience. However, personal apps and Enterprise line-of-Business apps can use extended execution and the extendedBackgroundTaskTime capability to create apps that will run as long as needed regardless of the device’s resource availability.
Be aware that the extendedExecutionUnconstrained and extendedBackgroundTaskTime capabilities can override default policy for UWP apps and may cause significant battery drain. Before using these capabilities, first confirm that the default extended execution and background task time policies do not meet your needs and perform testing in battery-constrained conditions to understand the impact your app will have on a device.