如何在 Windows 的后台运行命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21031171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 11:14:26  来源:igfitidea点击:

How to run a command on the background on Windows?

windows

提问by Louis

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…

在linux下可以使用command &在后台运行command,shell下线后同样会继续。我想知道窗户有没有类似的东西......

采纳答案by drew_w

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:

我假设您想要做的是在没有界面的情况下运行命令(可能是自动的?)。在 Windows 上,有许多选项可以满足您的需求:

  • 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 herefor more information. An example is:

    Set objShell = WScript.CreateObject("WScript.Shell")
    objShell.Run("C:\yourbatch.bat"), 0, True
    

    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.

  • Worst: Start the command using the startup folder. This runs when a user logs into the computer
  • 最佳:将您的程序编写为 Windows 服务。这些将在没有人登录服务器时启动。它们让您选择用户帐户(可能与您自己的帐户不同),如果失败,它们将重新启动。它们始终运行,因此您可以在特定时间或定期从其中自动执行任务。有关如何编写 Windows 服务的更多信息,您可以在线阅读教程,例如 ( http://msdn.microsoft.com/en-us/library/zt39148a(v=vs.110).aspx)。

  • 更好:启动命令并隐藏窗口。假设命令是 DOS 命令,您可以为此使用 VB 或 C# 脚本。请参阅此处了解更多信息。一个例子是:

    Set objShell = WScript.CreateObject("WScript.Shell")
    objShell.Run("C:\yourbatch.bat"), 0, True
    

    您仍然必须手动启动命令或编写任务来启动命令。这是该策略最大的跌幅之一。

  • 最差:使用启动文件夹启动命令。这在用户登录计算机时运行

Hope that helps some!

希望能帮到一些人!

回答by Oeste

I believe the command you are looking for is start /b *command*

我相信你正在寻找的命令是 start /b *command*

For unix, nohuprepresents 'no hangup', which is slightly different than a background job (which would be *command* &. I believe that the above command should be similar to a background job for windows.

对于 unix,nohup表示“无挂断”,这与后台作业略有不同(即*command* &。我相信上面的命令应该类似于 windows 的后台作业。

回答by Nicholas DiPiazza

You should also take a look at the atcommand in Windows. It will launch a program at a certain time in the background which works in this case.

您还应该查看atWindows中的命令。它将在后台的某个时间启动一个程序,在这种情况下有效。

Another option is to use the nssmservice manager software. This will wrap whatever command you are running as a windows service.

另一种选择是使用nssm服务管理器软件。这将包装您作为 Windows 服务运行的任何命令。

UPDATE:

更新:

nssmisn't very good. You should instead look at WinSW project. https://github.com/kohsuke/winsw

nssm不是很好。您应该查看 WinSW 项目。https://github.com/kohsuke/winsw

回答by Josh Withee

Use the startcommand with the /bflag to run a command/application without opening a new window. For example, this runs dotnet runin the background:

使用start带有/b标志的命令可以在不打开新窗口的情况下运行命令/应用程序。例如,这dotnet run在后台运行:

start /b dotnet run

You can pass parameters to the command/application too. For example, I'm starting 3 instances of this C# project, with parameter values of x, y, and z:

您也可以将参数传递给命令/应用程序。例如,我开始这个C#项目的3个实例,用的参数值xy以及z

enter image description here

enter image description here

To stop the program(s) running in the background: CTRL + BREAK

要停止在后台运行的程序: CTRL + BREAK

In my experience, this stops all of the background commands/programs you have started in that cmdinstance.

根据我的经验,这会停止您在该cmd实例中启动的所有后台命令/程序。

According to the Microsoft docs:

根据微软文档

CTRL+C handling is ignored unless the application enables CTRL+C processing. Use CTRL+BREAK to interrupt the application.

除非应用程序启用 CTRL+C 处理,否则将忽略 CTRL+C 处理。使用 CTRL+BREAK 中断应用程序。