Windows Server 启动时自动运行程序

发布于 2024-11-06 22:08:58 字数 211 浏览 4 评论 0原文

我正在尝试在始终打开的 Windows 服务器上创建并安装 .NET 应用程序,但遇到了问题。我听说我不应该将其设为 Windows 服务(并且我个人更愿意有一个对话框应用程序,以便我可以看到进度等),因此我尝试使用任务计划程序。但是,任务计划程序一直试图过早关闭我的应用程序,而它应该只打开它并使其保持打开状态。

有什么想法可以让我的对话框应用程序在启动时在我的服务器上运行(并保持运行)吗?

I am trying to create and install a .NET app on a windows server that is always on, but I am running into problems. I have heard that I should not make it a Windows service (and would personally rather have a dialog app so I can see progress, etc) so I am trying to use the task scheduler. However, the task scheduler keeps trying to close my app prematurely, when it should only open it and leave it open.

Any ideas how I could have my dialog app run on startup (and stay running) on my server?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

若言繁花未落 2024-11-13 22:08:58

您可以使用计划任务设置来在启动时间运行或按计划运行,这样用户登录/注销时就不会出现问题。

  1. 打开任务计划程序,Windows 键 + R

Taskschd.msc

  1. 单击操作菜单 >创建任务

enter此处的图像描述

  1. 将用户更改为管理员或最好是服务帐户,并注意无论用户是否登录“运行”的选项

<一href="https://i.sstatic.net/i95Ve.png" rel="noreferrer">在此处输入图像描述

4a。设置启动时 - 与用户登录或注销无关

在此处输入图像描述

4b.或设置登录时 - 取决于登录

在此处输入图像描述

4c.或者设置按计划工作,无论用户是否登录:

在此处输入图像描述

  1. 设置要启动的程序:

在此处输入图像描述

注意: 请使用服务帐户来执行此操作,如果您使用管理员帐户,则更改密码后它将失败,

注意:对于无人值守的应用程序,请选择控制台应用程序,因为一些应用程序可以具有用户界面除非用户登录,否则会出现问题,这取决于YMMV。一般来说,不要让窗口保持活动状态,即不要生成 MsgBox - 使用后杀死所有窗口,或者仅让一个控制台应用程序在后台无人值守地运行。您最不希望发生的事情就是因太多句柄和未处理的 GDI 对象而导致计算机崩溃。

start /B your_console_app.exe

/B 标志告诉 start 在后台运行应用程序而不创建新窗口。

输入图像描述这里

You can do it with a Scheduled Task setup to run at startup time or on a schedule so there wont be problems with users logging on/off.

  1. Open Task Scheduler, Windows Key + R

Taskschd.msc

  1. Click Action menu > Create Task

enter image description here

  1. Change the User to a Administrator or preferably a Service Account and note the option to Run whether user is logged in "or not":

enter image description here

4a. Set At startup - nothing to do with Users logging in or off

enter image description here

4b. Or set At Log On - depends on Users logging on

enter image description here

4c. Or set On a schedule to work regardless of users being logged in:

enter image description here

  1. Set a program to start:

enter image description here

NOTE: Please use a Service Account to do this, if you use your Admin account then it will FAIL after changing password,

NOTE: For unattended applications choose a Console app as some user interfaces apps can have issues unless a user is logged on, it depends YMMV. In general don't keep windows alive, ie dont spawn MsgBox's - kill all windows after use or only have the one console app running in the background unattended. The last thing you want is to crash the computer with too many handles and GDI Objects left undisposed.

start /B your_console_app.exe

The /B flag tells start to run the application in the background without creating a new window.

enter image description here

┼── 2024-11-13 22:08:58

如果您正在寻找“登录后自动运行”,这里是答案:

在“运行”提示符中输入 shell:Startup

在此处输入图像描述

这将在其他位置打开一个窗口隐藏的AppData路径开始菜单/程序/启动 ->添加您要启动的程序的可执行文件的快捷方式。

当然,这仅适用于您当时使用的登录帐户。

对于所有用户,输入 shell:Common Startup

In case if you are looking for "automatically run after login" here is answer:

In the "Run" prompt type shell:Startup

enter image description here

This will open a window in the otherwise hidden AppData path for the Startmenu/Programs/Startup -> Add a shortcut to the executable for the program you want to start.

This will of course only apply to the logon account that you're using at the time.

For all users type shell:Common Startup

£冰雨忧蓝° 2024-11-13 22:08:58

你应该创建一个Windows服务;这是唯一可靠的方法。
否则,您将遇到问题,例如用户注销。

如果你想要 UI,你可以制作一个单独的 GUI 来与服务通信(可能使用 WCF)。

You should make a Windows Service; it's the only reliable way to do this.
Otherwise, you will run into problems if, for example, the user logs off.

If you want UI, you can make a separate GUI that communicates with the service (probably using WCF).

轮廓§ 2024-11-13 22:08:58

我更喜欢在任务计划程序中创建任务并将触发器设置为“启动时”,但您也可以使用启动脚本。

您还可以使用本地组策略编辑器(执行 gpedit)并添加启动脚本。复制 http://technet.microsoft.com/en-us/library 中的步骤/cc770556.aspx 这样,如果他们将其删除,仍然可以在 SO 找到它。

分配计算机启动脚本

  1. 打开本地组策略编辑器。
  2. 在控制台树中,单击脚本(启动/关闭)。路径为计算机配置\Windows 设置\脚本(启动/关闭)
  3. 在结果窗格中,双击启动
  4. 启动属性对话框中,单击添加
  5. 添加脚本对话框中,执行以下操作:
    • 脚本名称框中,键入脚本的路径,或单击浏览在域控制器上的 Netlogon 共享文件夹中搜索脚本文件。< /里>
    • 脚本参数框中,键入所需的任何参数,方式与在命令行中键入参数的方式相同。例如,如果您的脚本包含名为 //logo(显示横幅)和 //I(交互模式)的参数,请键入 //logo //I
  6. 启动属性对话框中,指定所需的选项:
    • <组策略对象>的启动脚本:列出当前分配给所选组策略对象 (GPO) 的所有脚本。如果您分配多个脚本,则会按照您指定的顺序处理这些脚本。要在列表中向上移动脚本,请单击它,然后单击向上。要在列表中向下移动脚本,请单击该脚本,然后单击向下
    • 添加:打开添加脚本对话框,您可以在其中指定要使用的任何其他脚本。
    • 编辑:打开编辑脚本对话框,您可以在其中修改脚本信息,例如名称和参数。
    • 删除:从启动脚本列表中删除选定的脚本。
    • 显示文件:显示存储在所选 GPO 中的脚本文件。

I prefer to create a task in the Task Scheduler and set the trigger as "On Startup", but you can also use Startup Scripts.

You can also use the Local Group Policy Editor (executing gpedit) and add a Startup script. Copying the steps from http://technet.microsoft.com/en-us/library/cc770556.aspx so that if they take it down, it still can be found at SO.

To assign computer startup scripts

  1. Open the Local Group Policy Editor.
  2. In the console tree, click Scripts (Startup/Shutdown). The path is Computer Configuration\Windows Settings\Scripts (Startup/Shutdown).
  3. In the results pane, double-click Startup.
  4. In the Startup Properties dialog box, click Add.
  5. In the Add a Script dialog box, do the following:
    • In the Script Name box, type the path to the script, or click Browse to search for the script file in the Netlogon shared folder on the domain controller.
    • In the Script Parameters box, type any parameters that you want, the same way as you would type them on the command line. For example, if your script includes parameters called //logo (display banner) and //I (interactive mode), type //logo //I.
  6. In the Startup Properties dialog box, specify the options that you want:
    • Startup Scripts for <Group Policy object>: Lists all the scripts that currently are assigned to the selected Group Policy object (GPO). If you assign multiple scripts, the scripts are processed in the order that you specify. To move a script up in the list, click it and then click Up. To move a script down in the list, click it and then click Down.
    • Add: Opens the Add a Script dialog box, where you can specify any additional scripts to use.
    • Edit: Opens the Edit Script dialog box, where you can modify script information, such as name and parameters.
    • Remove: Removes the selected script from the Startup Scripts list.
    • Show Files: Displays the script files that are stored in the selected GPO.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文