定时开启服务和关闭服务

创建 PowerShell 脚本

创建一个 PowerShell 脚本来启动和停止服务。

  • 启动服务的脚本start_service.ps1):

  • Start-Service -Name "jl-tellus"
    Write-Output "Service jl-tellus started at $(Get-Date)"
    

      

  • 停止服务的脚本stop_service.ps1
  • Stop-Service -Name "jl-tellus"
    Write-Output "Service jl-tellus stopped at $(Get-Date)"
    

      

    2. 配置任务计划程序

    使用任务计划程序来设置每天 18:00 启动服务,以及每天 9:00 停止服务。

    2.1 创建启动服务的任务
    1. 打开 任务计划程序(Task Scheduler)。

    2. 点击右侧的 “创建任务”

    3. 在 “常规” 选项卡中:

      • 输入任务名称,例如 Start_jl-tellus_Service

      • 选择 “不管用户是否登录都要运行”

      • 勾选 “使用最高权限运行”

    4. 在 “触发器” 选项卡中:

      • 点击 “新建”

      • 设置 “开始任务” 为 “按计划”

      • 设置 “每日”,时间为 18:00

      • 点击 “确定”

    5. 在 “操作” 选项卡中:

      • 点击 “新建”

      • 设置 “操作” 为 “启动程序”

      • 在 “程序或脚本” 中输入:

      • powershell.exe
      • 在 “添加参数” 中输入:
      • -ExecutionPolicy Bypass -File "C:\Scripts\start_service.ps1"
        

          

      • 2.2 创建停止服务的任务
        1. 重复上述步骤,创建一个新任务。

        2. 在 “常规” 选项卡中:

          • 输入任务名称,例如 Stop_jl-tellus_Service

        3. 在 “触发器” 选项卡中:

          • 设置时间为 9:00

        4. 在 “操作” 选项卡中:

          • 在 “程序或脚本” 中输入:powershell.exe

          • 参数:   -ExecutionPolicy Bypass -File "C:\Scripts\stop_service.ps1"
          • 可以通过以下命令查看服务名称:
          • Get-Service -Name "jl-tellus"

 

 

日志记录

 

  • 如果需要记录日志,可以在脚本中添加日志输出,例如:

Add-Content -Path "C:\Scripts\log.txt" -Value "Service jl-tellus started at $(Get-Date)"

  

 

posted @ 2025-02-17 14:15  CrossPython  阅读(29)  评论(0)    收藏  举报