Is there any way to restart a windows service from the same service , as Application.Restart() in Windows forms, I don’t want to launch another process from the service to restart the service.
bobbymcr
23.8k3 gold badges56 silver badges67 bronze badges
asked Dec 6, 2009 at 17:20
1
I am a developer for an open source windows service hosting framework called Daemoniq. Setting service recovery options is one of its features. You can download it from http://daemoniq.org
Current features include:
- container agnostic service location via the CommonServiceLocator
- set common service properties like serviceName, displayName, description and serviceStartMode via app.config
- run multiple windows services on the same process
- set recovery options via app.config
- set services depended on via app.config
- set service process credentials via command-line
- install, uninstall, debug services via command-line
Thanks!
answered Dec 10, 2009 at 18:58
You also can add Custom Action to Commit folder of Custom Actions in your setup project. It must be a primary output of class library project with class inherited from System.Configuration.Install.Installer with [RunInstaller(true)] attribute. In this class you need to override one base method:
public override void Commit(IDictionary savedState)
{
base.Commit(savedState);
ProcessStartInfo psi = new ProcessStartInfo("sc", "failure \"You service name\" reset= 60 actions= restart/1000");
psi.CreateNoWindow = true;
Process proc = Process.Start(psi);
proc.WaitForExit();
}
It’s configuring your service to restart automaticaly after failure.
Than when you need to restart your service you can do
Environment.FailFast("Self restarting service...");
But it has one drawback — it will be fired an error message in event log.
answered Jun 14, 2015 at 11:31
- Remove From My Forums
-
Question
-
i got guide line to restart the windows service using below line. now i want to plug the code into my win service and call those code when a specific condition will satisfy but i am not sure that can we restart windows service from the service itself?
if any problem would occur then please advise me in advance. thanks
private void RestartWindowsService(string serviceName) { ServiceController serviceController = new ServiceController(serviceName); try { if ((serviceController.Status.Equals(ServiceControllerStatus.Running)) || (serviceController.Status.Equals(ServiceControllerStatus.StartPending))) { serviceController.Stop(); } serviceController.WaitForStatus(ServiceControllerStatus.Stopped); serviceController.Start(); serviceController.WaitForStatus(ServiceControllerStatus.Running); } catch }
Answers
-
Your application should try to recover internally. If it encounters an exception, deal with it. If you don’t know how to handle the exception, debug your application to find out exactly what is causing the exception.
In other words, don’t paper over your bugs. Just blindly restarting your service is a poor way of dealing with errors.
Having said this, there are scenarios where there may be justification to restarting an application that has failed. There are a couple of approaches, none of them particularly appealing. Your service can simply exit, but then you need a watchdog service,
which is completely separate, who’s sole responsibility is to watch for the service, and restart it if it fails. Another approach is for your service to launch a separate application, which will perform the shutdown/restart, when it encounters an unanticipiated
exception.-
Marked as answer by
Wednesday, October 8, 2014 7:48 PM
-
Marked as answer by
Is there a way to restart a Windows service from the command prompt?
asked Jun 24, 2011 at 17:54
You can use net stop [service name]
to stop it and net start [service name]
to start it up again basically restarting the service.
To combine them just do this — net stop [service name] && net start [service name]
.
There is also a command built specifically for messing with services: sc
DESCRIPTION: SC is a command line program used for communicating with the Service Control Manager and services. USAGE: sc [command] [service name] ... 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 (persistent). description-----Changes the description of a service. failure---------Changes the actions taken by a service upon failure. failureflag-----Changes the failure actions flag of a service. sidtype---------Changes the service SID type of a service. privs-----------Changes the required privileges of a service. managedaccount--Changes the service to mark the service account password as managed by LSA. 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. qfailureflag----Queries the failure actions flag of a service. qsidtype--------Queries the service SID type of a service. qprivs----------Queries the required privileges of a service. qtriggerinfo----Queries the trigger parameters of a service. qpreferrednode--Queries the preferred NUMA node of a service. qrunlevel-------Queries the run level of a service. qmanagedaccount-Queries whether a services uses an account with a password managed by LSA. qprotection-----Queries the process protection level of a service. 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. showsid---------Displays the service SID string corresponding to an arbitrary name. triggerinfo-----Configures the trigger parameters of a service. preferrednode---Sets the preferred NUMA node of a service. runlevel--------Sets the run level of a service. 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 EXAMPLE: sc start MyService QUERY and QUERYEX OPTIONS: If the query command is followed by a service name, the status for that service is returned. Further options do not apply in this case. If the query command is followed by nothing or one of the options listed below, the services are enumerated. type= Type of services to enumerate (driver, service, all) (default = service) state= State of services to enumerate (inactive, all) (default = active) bufsize= The size (in bytes) of the enumeration buffer (default = 4096) ri= The resume index number at which to begin the enumeration (default = 0) group= Service group to enumerate (default = all groups) SYNTAX EXAMPLES sc query - Enumerates status for active services & drivers sc query eventlog - Displays status for the eventlog service sc queryex eventlog - Displays extended status for the eventlog service sc query type= driver - Enumerates only active drivers sc query type= service - Enumerates only Win32 services sc query state= all - Enumerates all services & drivers sc query bufsize= 50 - Enumerates with a 50 byte buffer sc query ri= 14 - Enumerates with resume index = 14 sc queryex group= "" - Enumerates active services not in a group sc query type= interact - Enumerates all interactive services sc query type= driver group= NDIS - Enumerates all NDIS drivers
answered Jun 24, 2011 at 17:58
paradd0xparadd0x
9,1497 gold badges37 silver badges44 bronze badges
9
Please, note that if there are other services that depends on this service — usual net stop & net start
will not restart them. net stop /y
will stop all dependencies
Most common example — SQL Server & SQL Agent.
I do recommend PowerShell cmdlet to solve this:
powershell -command "Restart-Service MSSQLSERVER -Force"
After MSSQLSERVER starts — cmdlet starts all previously stopped dependancies.
PS: Make sure you are running command as admin
answered Mar 15, 2017 at 13:57
2
To restart a Windows service from the command prompt or scheduled tasks, use this:
cmd /c "net stop "Service Name" & sc start "Service Name""
answered Feb 12, 2013 at 7:27
KikiKiki
1311 silver badge2 bronze badges
1
You could also use PowerShell:
stop-Service
Gaff
18.6k15 gold badges57 silver badges68 bronze badges
answered Jun 24, 2011 at 18:12
devlifedevlife
2411 gold badge2 silver badges7 bronze badges
1
To solve the annoying Wacom Intuous Driver not running Error I get on every reboot.
Windows key + R, paste, Bam!
sc stop WTabletServicePro && sc start WTabletServicePro
Simon E.
3,8615 gold badges29 silver badges31 bronze badges
answered Oct 20, 2014 at 3:45
GeorgeGeorge
611 silver badge1 bronze badge
1
The PsService utility from PsTools provides a restart
command for services, with additional parameters to run it on another machine.
psservice [-accepteula] [\\Computer [-u Username [-p Password]]] restart <service-name>
The -accepteula
flag saves you the EULA window just in case it’s the first time you use this utility with the current user.
answered May 22, 2018 at 16:00
cdlvcdlvcdlvcdlv
1,4711 gold badge19 silver badges27 bronze badges
You must log in to answer this question.
Not the answer you’re looking for? Browse other questions tagged
.
Not the answer you’re looking for? Browse other questions tagged
.
on August 15, 2010
We normally use Services.msc to start or stop or disable or enable any service. We can do the same from windows command line also using net and sc utilities. Below are commands for controlling the operation of a service.
Command to stop a service:
net stop servicename
To start a service:
net start servicename
You need to have administrator privileges to run net start/stop commands. If you are just a normal user on the computer, you would get an error like below.
C:\>net start webclient System error 5 has occurred. Access is denied. C:\>
To disable a service:
sc config servicename start= disabled
To enable a service:
sc config servicename start= demand
To make a service start automatically with system boot:
sc config servicename start= auto
Note: Space is mandatory after ‘=’ in the above sc commands.
This SC command works on a Windows 7 machine and also on the down-level editions of Windows i.e Windows XP/2003 and Windows Vista. Again, if you do not have administrator previliges you would get the below error.
C:\>sc config webclient start= auto [SC] OpenService FAILED 5: Access is denied.
Note that the service name is not the display name of a service. Each service is given a unique identification name which can be used with net or sc commands. For example, Remote procedure call (RPC) is the display name of the service. But the service name we need to use in the above commands is RpcSs.
So to start Remote procedure call service the command is:
net start RpcSsTo stop Remote procedure call service
net stop RpcSs
These service names are listed below for each service. The first column shows the display name of a service and the second column shows the service name that should be used in net start or net stop or sc config commands.
Display Name of the service | ServiceName which should be used with ‘net’ and ‘sc config’ commands. |
Alerter | Alerter |
Application Layer Gateway Service | ALG |
Application Management | AppMgmt |
ASP.NET State Service | aspnet_state |
Windows Audio | AudioSrv |
Background Intelligent Transfer Service | BITS |
Computer Browser | Browser |
Bluetooth Support Service | BthServ |
Bluetooth Service | btwdins |
SMS Agent Host | CcmExec |
Indexing Service | CiSvc |
ClipBook | ClipSrv |
.NET Runtime Optimization Service v2.0.50727_X86 | clr_optimization_v2.0.50727_32 |
COM+ System Application | COMSysApp |
Cryptographic Services | CryptSvc |
Cisco Systems, Inc. VPN Service | CVPND |
DCOM Server Process Launcher | DcomLaunch |
DHCP Client | Dhcp |
Logical Disk Manager Administrative Service | dmadmin |
Logical Disk Manager | dmserver |
DNS Client | Dnscache |
Lenovo Doze Mode Service | DozeSvc |
Error Reporting Service | ERSvc |
Event Log | Eventlog |
COM+ Event System | EventSystem |
Intel(R) PROSet/Wireless Event Log | EvtEng |
Fast User Switching Compatibility | FastUserSwitchingCompatibility |
Windows Presentation Foundation Font Cache 3.0.0.0 | FontCache3.0.0.0 |
Group Policy Monitor | GPMON_SRV |
Help and Support | helpsvc |
HID Input Service | HidServ |
HTTP SSL | HTTPFilter |
ThinkPad PM Service | IBMPMSVC |
Windows CardSpace | idsvc |
IMAPI CD-Burning COM Service | ImapiService |
iPassConnectEngine | iPassConnectEngine |
iPassPeriodicUpdateApp | iPassPeriodicUpdateApp |
iPassPeriodicUpdateService | iPassPeriodicUpdateService |
IviRegMgr | IviRegMgr |
Server | lanmanserver |
Workstation | lanmanworkstation |
Lenovo Camera Mute | LENOVO.CAMMUTE |
Lenovo Microphone Mute | Lenovo.micmute |
TCP/IP NetBIOS Helper | LmHosts |
Intel(R) Management and Security Application Local Management Service | LMS |
McAfee Framework Service | McAfeeFramework |
McAfee McShield | McShield |
McAfee Task Manager | McTaskManager |
Machine Debug Manager | MDM |
Messenger | Messenger |
NetMeeting Remote Desktop Sharing | mnmsrvc |
Distributed Transaction Coordinator | MSDTC |
Windows Installer | MSIServer |
Net Driver HPZ12 | Net Driver HPZ12 |
Network DDE | NetDDE |
Network DDE DSDM | NetDDEdsdm |
Net Logon | Netlogon |
Network Connections | Netman |
Net.Tcp Port Sharing Service | NetTcpPortSharing |
Network Location Awareness (NLA) | Nla |
NT LM Security Support Provider | NtLmSsp |
Removable Storage | NtmsSvc |
Microsoft Office Diagnostics Service | odserv |
Office Source Engine | ose |
Plug and Play | PlugPlay |
Pml Driver HPZ12 | Pml Driver HPZ12 |
IPSEC Services | PolicyAgent |
Power Manager DBC Service | Power Manager DBC Service |
Protected Storage | ProtectedStorage |
Remote Access Auto Connection Manager | RasAuto |
Remote Access Connection Manager | RasMan |
Remote Desktop Help Session Manager | RDSessMgr |
Intel(R) PROSet/Wireless Registry Service | RegSrvc |
Routing and Remote Access | RemoteAccess |
Remote Registry | RemoteRegistry |
Remote Procedure Call (RPC) Locator | RpcLocator |
Remote Procedure Call (RPC) | RpcSs |
QoS RSVP | RSVP |
Intel(R) PROSet/Wireless WiFi Service | S24EventMonitor |
Security Accounts Manager | SamSs |
Smart Card | SCardSvr |
Task Scheduler | Schedule |
Secondary Logon | seclogon |
System Event Notification | SENS |
Windows Firewall/Internet Connection Sharing (ICS) | SharedAccess |
Shell Hardware Detection | ShellHWDetection |
Print Spooler | Spooler |
System Restore Service | srservice |
SSDP Discovery Service | SSDPSRV |
Windows Image Acquisition (WIA) | stisvc |
System Update | SUService |
MS Software Shadow Copy Provider | SwPrv |
Performance Logs and Alerts | SysmonLog |
Telephony | TapiSrv |
Terminal Services | TermService |
Themes | Themes |
ThinkVantage Registry Monitor Service | ThinkVantage Registry Monitor Service |
Telnet | TlntSvr |
On Screen Display | TPHKSVC |
Distributed Link Tracking Client | TrkWks |
TVT Scheduler | TVT Scheduler |
Windows User Mode Driver Framework | UMWdf |
Intel(R) Management & Security Application User Notification Service | UNS |
Universal Plug and Play Device Host | upnphost |
Uninterruptible Power Supply | UPS |
Volume Shadow Copy | VSS |
Windows Time | W32Time |
WebClient | WebClient |
Windows Management Instrumentation | winmgmt |
Portable Media Serial Number Service | WmdmPmSN |
Windows Management Instrumentation Driver Extensions | Wmi |
WMI Performance Adapter | WmiApSrv |
Security Center | wscsvc |
Automatic Updates | wuauserv |
SMS Remote Control Agent | Wuser32 |
Wireless Zero Configuration | WZCSVC |
Network Provisioning Service | xmlprov |
How to Restart the Service[s] in Windows Command Line
- Open PowerShell Terminal or PowerShell ISE as Administrator.
- Use the following Get-Service the command along with a -Name (or) -DisplayName parameter and List the Services you want to be restarted.
How do I force restart a Windows service?
- Click the Start menu.
- Click Run or in the search bar type services.msc.
- Press Enter.
- Look for the service and check the Properties and identify its service name.
- Once found, open a command prompt. Type sc queryex [servicename].
- Press Enter.
- Identify the PID.
- In the same command prompt type taskkill /pid [pid number] /f.
How do you restart services in Windows 10?
This document highlights steps on how to restart a Windows service, which can sometimes be done in lieu of restarting your computer.
- Open Services. Windows 8 or 10: Open Start screen, type services. msc and press Enter.
- In the Services pop-up, select the desired application and click the Restart Service button.
How do I restart a service remotely?
You can use mmc:
- Start / Run. Type “mmc”.
- File / Add/Remove Snap-in… Click “Add…”
- Find “Services” and click “Add”
- Select “Another computer:” and type the host name / IP address of the remote machine. Click Finish, Close, etc.
How do I restart a Windows Powershell service?
Stop and then restart one or more services. Syntax Restart-Service { [-name] string[] | [-displayName] string[] | [-inputObject ServiceController[]] } [-force] [-include string[]] [-exclude string[]] [-passthru] [-whatIf] [-confirm] [CommonParameters] Key -name string The service names to be restarted.
How do I restart Windows?
Use Ctrl + Alt + Delete
- On your computer keyboard, hold down the control (Ctrl), alternate (Alt), and delete (Del) keys at the same time.
- Release the keys and wait for a new menu or window to appear.
- In the bottom right corner of the screen, click the Power icon.
- Select between Shut Down and Restart.
How do I restart Winmgmt service?
How do I restart WMI?
- Click Start , click Run, type cmd, and then click OK.
- Stop the Windows Management Instrumentation service or at the command prompt, type net stop winmgmt, and then press ENTER.
- At the command prompt, type winmgmt /resyncperf, and then press ENTER.
How do I restart a Windows process?
How to restart Windows Explorer
- Open Task Manager. If you right-click on the task bar at the bottom of the screen, Task Manager should appear as an option.
- In Task Manager, click on the field labeled “Windows Explorer.”
- In the bottom right corner of Task Manager, click the button labeled “Restart.”
How do I force a service in Windows 10?
To start a service on Windows 10, use these steps:
- Open Start.
- Search for Services and click the top result to open the console.
- Double-click the service that you intend to stop.
- Click the Start button. Source: Windows Central.
- Click the Apply button.
- Click the OK button.
How can I access a service from another computer?
To connect to a remote services MMC, click the Services name in the left pane, go to Action, then Connect to another computer… Once connected, you can operate the services just like you do on the local system.
How do I run a Windows process remotely?
tasklist.exe /S SYSTEM /U USERNAME /P PASSWORD To execute, click on Start \ Run… and in the run window type cmd to open a command prompt. Then type the tasklist command, substituting SYSTEM for the remote computer you want to view processes, USERNAME and PASSWORD with an account/password on the remote Computer.
Can we restart Windows service from service itself?
Make the service restart itself: Open the services applet. (Start -> Settings -> Control Panel -> Administrative Tools -> Services) Find the service you wish to restart and right click on it. Choose Properties from the menu. On the first, second and subsequent failures dropdown boxes, choose your desired action to take when the service fails.
How to shutdown/reboot computer in CMD?
How to Shut Down or Restart Another Computer Using CMD Method 1 of 4: Using CMD. Click the Start button . It’s the button with the Windows icon in the lower-left corner. Method 2 of 4: Using the Remote Shutdown Dialog. Click the Start button . Method 3 of 4: Enabling Printer and File Sharing Through the Windows Firewall. Open the Control Panel. Method 4 of 4: Editing the Registry. Click the Start button .
How do you restart the Windows Update service?
Here are 2 Ways to Restart Windows Update Service in Windows 10 –. Step-1: Type Services in the Taskbar Search box. When the result appears, click on Open. Step-2: On the following Window, Scroll down through the list of services there and find Windows Update. Right-click on Windows update and click on Restart.
How do I start Windows Installer service?
Start the Windows Installer service. Click Start, type Services.msc and press {ENTER}. Double-click Windows Installer. Set the Startup type of Windows Installer to Manual. Click Start to start the service.