服务器设置系统启动时程序自动运行(不需要登录)

服务器设置系统启动时程序自动运行(不需要登录)

写在前面

本方法不适用于winform、WPF等UI程序。涉及到windows系统的用户会话管理的策略。
Windows系统中存在一个特殊的 Session,其 Session ID 为 0。这通常称为 Session0。所有 Windows 服务都在 Session0 中运行,并且 Session0 是非交互式的。非交互式意味着无法启动 UI 应用程序;

1. 通过组策略设置脚本随服务器启动

打开组策略编辑器

  1. 找到 计算机配置->Windows设置->脚本(启动/关机)
  2. 选中启动
  3. 将下列脚本添加进去,配置好启动程序路径的参数:
start.bat
1
2
3
4
5
6
7
@echo off
REM 声明采用UTF-8编码
chcp 65001
@REM pause
set input=%1%
echo 自启动程序路径参数:%input%
start %input%

2. 使用任务计划程序

需要注意的时,在选择用户执行时,如果创建任务的用户为Administrator,需要注意执行时的用户账户,如果是需要在服务器启动时运行,就不应该为Administrator,而是应该为SYSTEM

参考资料:

碰见的问题:

界面显示问题:

According to Microsoft (emphasis added):

You can specify that a task should run even if the account under which the task is scheduled to run is not logged on when the task is triggered. To do this, select the radio button labeled Run whether user is logged on or not . If this radio button is selected, tasks will not run interactively. To make a task run interactively, select the Run only when user is logged on radio button.

Essentially, if you select ‘Run whether user is logged on or not’, the process will not start a UI.

作者

zhang

发布于

2021-07-20

更新于

2023-09-19

许可协议

CC BY-NC-SA 4.0

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×