AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 524
In Process
Jason Baker
Jason Baker
Asked: 2009-05-01 04:12:37 +0800 CST2009-05-01 04:12:37 +0800 CST 2009-05-01 04:12:37 +0800 CST

如何使用脚本远程重启 Windows 服务?

  • 772

我有一个在 CherryPy 服务器中运行的 Python Web 应用程序,该服务器作为 Windows 服务运行。我有一个批处理文件来部署这个应用程序,但我仍然需要远程桌面到服务器来重新启动服务。有没有办法编写这个脚本?

我试过了:

psexec \\server "net restart cherrypyservice"

但这似乎不起作用。

windows-service windows-server-2003 python pstools
  • 10 10 个回答
  • 35887 Views

10 个回答

  • Voted
  1. Keng
    2009-05-01T04:18:42+08:002009-05-01T04:18:42+08:00

    您可以使用 sc 命令行工具,但我不知道如何在 python 中专门执行此操作。

    https://stackoverflow.com/questions/133883/stop-and-start-a-service-via-batch-or-cmd-file/133926#133926

    描述:SC 是一个命令行程序,用于与 NT 服务控制器和服务进行通信。用法:sc [命令] [服务名称] ...

      The option  has the form "\\ServerName"
      Further help on commands can be obtained by typing: "sc [command]"
      Commands:
        query-----------Queries the status for a service, or
                        enumerates the status for types of services.
        queryex---------Queries the extended status for a service, or
                        enumerates the status for types of services.
        start-----------Starts a service.
        pause-----------Sends a PAUSE control request to a service.
        interrogate-----Sends an INTERROGATE control request to a service.
        continue--------Sends a CONTINUE control request to a service.
        stop------------Sends a STOP request to a service.
        config----------Changes the configuration of a service (persistant).
        description-----Changes the description of a service.
        failure---------Changes the actions taken by a service upon failure.
        qc--------------Queries the configuration information for a service.
        qdescription----Queries the description for a service.
        qfailure--------Queries the actions taken by a service upon failure.
        delete----------Deletes a service (from the registry).
        create----------Creates a service. (adds it to the registry).
        control---------Sends a control to a service.
        sdshow----------Displays a service's security descriptor.
        sdset-----------Sets a service's security descriptor.
        GetDisplayName--Gets the DisplayName for a service.
        GetKeyName------Gets the ServiceKeyName for a service.
        EnumDepend------Enumerates Service Dependencies.
    
      The following commands don't require a service name:
      sc   
        boot------------(ok | bad) Indicates whether the last boot should
                        be saved as the last-known-good boot configuration
        Lock------------Locks the Service Database
        QueryLock-------Queries the LockStatus for the SCManager Database
    

    示例:sc 启动 MyService

    • 9
  2. gabr
    2009-05-01T23:12:06+08:002009-05-01T23:12:06+08:00

    使用 Russinovich 的psservice:

     psservice \\server restart cherrypyservice
    
    • 7
  3. K. Brian Kelley
    2009-05-01T20:50:54+08:002009-05-01T20:50:54+08:00

    如果你想使用 psexec:

    psexec \\Server cmd "/c net stop servicename"
    psexec \\Server cmd "/c net start servicename"
    

    虽然在这种情况下,建议使用 sc。如果您要掏钱,它可以满足您的一切需求。

    • 3
  4. Richard Slater
    2009-05-01T04:18:32+08:002009-05-01T04:18:32+08:00

    尝试

    psexec \\server net stop cherrypyservice
    psexec \\server net start cherrypyservice
    
    • 2
  5. Richard Gadsden
    2009-05-01T04:20:08+08:002009-05-01T04:20:08+08:00
    net stop cherrypyservice
    net start cherrypyservice
    

    使用您喜欢的任何远程执行引擎。

    • 2
  6. Kiminonawa
    2022-01-30T05:20:53+08:002022-01-30T05:20:53+08:00

    如果您有一台在计算机上启用了 WinRM 的服务器,并且您从中部署批处理文件的 PC 被添加到服务器的 WinRM 受信任主机,您可以使用 Invoke-Command

    Invoke-Command -ScriptBlock {Restart-Service cherrypyservice} -ComputerName <Server_Name>
    

    您使用 Invoke-Command 触发批处理文件。

    我个人在尝试在多个 windows 服务器上进行操作时使用 Invoke-Command。

    希望能帮助到你 :)

    • 1
  7. James Pogran
    2009-05-01T19:17:48+08:002009-05-01T19:17:48+08:00

    交互式使用 PowerShell(本地):

    get-service $servicename  | restart-service
    

    以交互方式(远程)使用 PowerShell:

    (gwmi win32_service -computer $comp -Filter "name='$serviceName'").StopService()
    (gwmi win32_service -computer $comp -Filter "name='$serviceName'").StartService()
    

    在一个函数中(远程):

    function restart-remoteservice{
         param($servicename,$computer)
         (gwmi win32_service -computer $computer -Filter "name='$serviceName'").StopService()
         (gwmi win32_service -computer $computer -Filter "name='$serviceName'").StartService()
    }
    
    • 0
  8. Joshua
    2009-12-08T07:08:09+08:002009-12-08T07:08:09+08:00

    使用 WMI 方法

    (Get-WmiObject win32_service -computer stp7cor1737ltv4 -filter "Name='SPtimerv3'").invokemethod("StartService",$null)

    • 0
  9. gWaldo
    2010-08-26T04:51:17+08:002010-08-26T04:51:17+08:00
    psservice \\server restart cherrypyservice
    

    (其中 psservice 是另一个 SysInternals 应用程序)

    或者

    sc \\server stop cherrypyservice
    sc \\server start cherrypyservice
    
    • 0
  10. Guest
    2022-01-30T05:01:24+08:002022-01-30T05:01:24+08:00

    写一个bat文件:

    网络停止“服务名称”

    网络启动“服务名称”

    您可以使用以下命令在主机上远程执行文件:

    psexec \\主机名 -c batfile.bat

    • 0

相关问题

  • IIS 6.0 (Windows Server 2003) 上的 HTTP 压缩

  • 从 2003 年迁移到 2008 年 Microsoft 群集技术

  • 有什么理由使用 Windows Server 2003 而不是 Server 2008?

  • 在 Windows Server 2003 下使用 wscipt 从 .asp 文件运行 .exe

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    从 IP 地址解析主机名

    • 8 个回答
  • Marko Smith

    如何按大小对 du -h 输出进行排序

    • 30 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    Windows 中执行反向 DNS 查找的命令行实用程序是什么?

    • 14 个回答
  • Marko Smith

    如何检查 Windows 机器上的端口是否被阻塞?

    • 4 个回答
  • Marko Smith

    我应该打开哪个端口以允许远程桌面?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    MikeN 在 Nginx 中,如何在维护子域的同时将所有 http 请求重写为 https? 2009-09-22 06:04:43 +0800 CST
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    0x89 bash中的双方括号和单方括号有什么区别? 2009-08-10 13:11:51 +0800 CST
  • Martin Hope
    kch 如何更改我的私钥密码? 2009-08-06 21:37:57 +0800 CST
  • Martin Hope
    Kyle Brandt IPv4 子网如何工作? 2009-08-05 06:05:31 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve