Forcing a Windows service to fail - windows-services

Whenever a specific Windows service fails I want to run a program I've created myself. However, I simply can't find a way to make it fail on purpose, so that I can actually test that everything works correctly.
Note that the service in question is not something I've written myself, so I can't make it fail programmatically from inside the code. I wouldn't, however, mind writing a program that can make a service fail.
Of course I would prefer just having a "Make service fail" button somewhere in services.msc ... ;)
The server I'm doing this on is running Windows Server 2012.

If you don't want to use command line :
As an admin open the Windows Task Manager, in the Services tab find the service you want to test. Right click the service and click on Go to process. The selected process (if any) is the one corresponding to your service. Kill this process to simulate a service failure.
Be aware that killing a process this way can lead to problems.

Define "fail". If you want the process to end, just use pskill or a similar tool that can terminate a process elevated (as an admin).

Related

w3wp process not found

I use Visual Studio 2010 to debug a asp.net MVC project in my local machine. The steps are:
Click Debug and try to attch process "w3wp.exe". However it is not in the list.
I am sure "Show processes in all sessions" is clicked.
w3wp.exe won't show in the running process' unless there is actually an instance of the web application running.
Try to access your web page first, when it is displayed for the first time, try to attach your debugger. The process should now show up.
You need to click Show Processes from All Users at the bottom of the Task Manager list. This is what i needed to do.
Restart IIS
Right click your site >> Manage Website >> Browse
Back into Visual Studio refresh the processes list
Try to check on :"Show Processes for All Users"
When on the 'Attach to Process' window in the bottom left there is a checkbox 'Show Processes for All Users'
An easy way that works ,when w3wp dont appear in the list,open a browser and write localhost ,then enter.After that w3wp appears to list.
Just because an IIS application pool is started, there may not be an
IIS worker process w3wp running. IIS will not start the worker
process until the first web request is received.
In my case, I first launched the application, still could not find w3wp.exe. Then I took the worker process ID from (IIS->Worker Process -> Process Id) and then search by id in the details tab and found it.
Clear the Solution
Rebuild the solution
Reset the IIS by writing the command iisreset in command prompt
Go to IIS and browse your website
Run VS as administrator
Tools -> Attach to process -> Ensure the 'Show Processes for All Users' checkbox is checked -> w3wp.exe
Attach to w3wp.exec
If you are using something like Advanced Rest Client to test routes, call your route again then refresh the list of processes and it will show up
GoTo Web Project properties -> Select (Web) on the left sidebar -> GoTo under (Servers) header -> Click to dropdown and select "Local IIS"
and apply. Then, when you start debugging you will see w3wp.exe on the proccess list.
I just ran into this issue - you may want to also double check your host settings and verify that you are actually pointed to localhost and not a production server.
I forgot I was pointed at a remote server, and thus, though I was accessing the site, it wasn't anything local so w3wp wasn't running, despite my superficially being able to see the site running.
In my case, I have not opened the Visual Studio in Admin mode that is why the w3wp.exe was not showing on the list.
When I opened the Visual Studio in Admin mode, it worked.
Right click on Visual Studio -> Open in Admin mode.
In my case, once I rebuild the web project and raise the limit of Connection Time out (in seconds), it automatically shows in Debug/Attach to Process list and keeps working.
I would just like to share my experience as well for future readers.
Be aware that, in the event that you have a web server cluster configuration (for load balancing etc) the w3wp process might not start on the same machine that you expect it to.
Unless your website is configured to only run on a single IIS instance, the w3wp process might be spinning up on one of the other machines inside of your web cluster.
This might be faulty configuration from the networking team/department or intended behaviour. I don't have the neccessary experience to pin point exactly how it should be configured.
Found a related page on MSDN as well:
https://msdn.microsoft.com/en-us/library/bb742600.aspx
In my case, I needed to connect from one Visual Studio to the process which running from another VS studio window.
The problem was next: one VS was launched with Administrator permissions. For resolving that issue you should launch both VS with Admin perm.
In my case, the problem was that I wasn't running Visual Studio as Administrator. My machine had restarted after an update and relaunched all the previously running processes, but had only relaunched VS in non-admin mode. When I restarted VS in admin mode, the w3wp.exe processes were available again for debugging.
Run the remote debugger as an administrator.
I followed every suggestion to resolve the issue, but it was not until I ran the remote debugger as an administrator that I was able to see the w3wp process
Try the following steps:
Create a virtual path from Solution Explorer.
Go to inetmgr to confirm that your own pool is created.
Go to Attach Process (Ctrl+Alt+P) and show processes for all users.
Then you will see w3wp.exe will be there.
Be aware that even after jumping through all these hoops (kicking off an instance using a web browser, starting your remote debugging session as an admin, ensuring that "show all users" is checked, etc., ensuring you aren't on a server farm, etc), you may still at times be out of luck.
There are times when the remote process, usually a WCF service in my case, simply will not show up in the list of processes to attach to, and there's nothing that can be done about it. I'm always careful to make my target process readily identifiable by keeping it and only it in a certain App Pool. Sometimes you just can't get there from here. This is undoubtedly the most frustrating thing about remote debugging Microsoft has ever done.
my answer is late for sure, but maybe my answer will help someone.
Go To IIS.
Application Pools.
Advance Setting.
enable 32.bit Application.
Make your Application Running Under enable 32.bit Application.

restart service in case of exception

I'm developing service application that must restart itself. What are the ways of doing that? Is it possible to ask system start application again if it is stopped? I'm using Delphi 2007.
Your service can programably configure recovery options for itself by calling the Win32 API ChangeServiceConfig2() function inside of its AfterInstall event. Set the dwInfoLevel to SERVICE_CONFIG_FAILURE_ACTIONS and set the lpInfo to point at a SERVICE_FAILURE_ACTIONS record describing what you want to happen when the service fails.
If you go into services.msc you can configure this for you service. You don't have to do it in code. See the Recovery tab when you open the properties of you service.

Nant: Process id of a service that is already running

Does anyone know how to get the PID of the process that a windows service is running in from Nant?
I am having file locking problems. I issue a stop command to the service and then try and delete the Windows service's files. However these are quite often locked by the process that the Windows service is running in. I want to find the PID so that I can wait for it to terminate before deleting.
Thanks
You might have to create a custom nant task to do this. However you might want to see about just adding maybe a delay like 1 min or something before you try to replace your files. Might be easier.

(SC) DeleteService FAILED 1072

Last time I create WAS profile and WASService then I try to config and run many script for learn how to config WAS, Finally it crash so i use wasprofile delete this profile and forgot delete WASService.
Now I found IBM Webphere Application Server service display in services.msc list, so I tried to delete it with WASService.exe -remove command and windows SC command but I got message
C:\Program Files\IBM\WebSphere\AppServer\bin>sc delete "IBMWAS61Service - DEV"
[SC] DeleteService FAILED 1072:
The specified service has been marked for deletion.
make sure the service is stopped, the services control panel is closed, and no open file handles are open by the service.
Also make sure ProcessExplorer is not running.
I had a similar problem and what I did to overcome it was the following:
Stop the service: net stop "ServiceName"
Ensure: the "mmc.exe" process does not exist (The "Services" list window): taskkill /F /IM mmc.exe
Delete the service: sc delete "ServiceName"
C:\server>sc delete "ServiceName"
[SC] DeleteService SUCCESS
Now, if I execute another sc command, what I get is the following:
C:\server>sc delete "ServiceName"
[SC] OpenService FAILED 1060:
The specified service does not exist as an installed service.
But not the 1072 error message
What I've done is go to this location in regedit:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services
From here, you will see a folder for every service on your machine. Simply delete the folder for the service you wish, and you're done.
N.B: Stop the service before you try this.
For some buggy reason both Event Viewer and/or Services.msc won't do a proper refresh when you tell them to!
Close them and restart, and the service would have been deleted anyway.
I had the same issue. After I closing and re-opening the Computer Management window the service was removed from the list. I'm running windows 7
In Windows 7, make sure Event Viewer closed before deleting.
I had this error also, make sure the exe the service is pointing to is stopped. Also make sure you don't have any Windows dialog boxes behind your other windows. That is why mine wasn't deleting. There was a windows message behind it saying this service has been deleted or something similar.. just had to click ok, there it went.
I had the same error due to a typo in the service name, i was trying to delete the service display name instead of the service name.
Once I used the right service name it worked fine
Logging-out and logging-in again close all blocking apps thus resolves the problem.
The 3rd party application uninstaller had removed the files for the service and then left the service in this pending deletion state.
After trying to close all applications, identifing PID of service(couldn't) for kill, logging off all other users and logging off and on, rebooting was the only fix that worked for me.
One situation where this can also happen is if there is some other service or application that is holding open a service handle obtained with OpenService. For example, a monitoring service that starts and stops services based on some external event can keep open handles to each of the services it monitors. In this case, uninstalling the service would leave it in the "marked for deletion" state until all handles obtained with OpenService are closed.

Starting a windows service fails with error 1053

I have a windows service that is failing to start, giving an error "Error 1053: The service did not respond to the start or control request in a timely fashion".
Running the service in my debugger works fine, and if I double click on the the service .exe on the remote machine a console window pops up and continues to run without problem - I can even see log messages showing me that the program is processing everything the way it should be.
The service had been running fine previously, though this is my first time, personally, trying to deploy it with the most recent changes made to the program. I've evaluated those changes and cant figure out how they might cause this problem, particuarly since everything runs fine when not started as a service.
The StartRoutine() method of the service impelmentation is empty, so should be returning in a "timely fashion".
I've checked the event logs on the computer, and it doesn't give any additional information other than it didn't hear back from the service in the 30 second requisite time frame.
Since it works on my machine, and as a double-clicked executable, how would I go about figuring out why it fails as a service?
Oh, and it's .NET 2.0, so it shouldn't be affected by the 1.1 framework bug that exhibited this symptom (http://support.microsoft.com/kb/839174)
The box is a windows server 2003 R2 machine running SP2.
This is a misleading error. It's probably an unhandled exception.
Empty your OnStart() handler then try this in your constructor...
public MainService()
{
InitializeComponent();
try
{
// All your initialization code goes here.
// For instance, my exception was caused by the lack of registry permissions
;
}
catch (Exception ex)
{
EventLog.WriteEntry("Application", ex.ToString(), EventLogEntryType.Error);
}
}
Now check the EventLog on your system for your Application Error.
Could be a number of things and it might help to get a stack trace on the machine exhibiting the problem. There are a number of ways to do this but the point is that you have to see where this is failing in the code.
You can do this with remote debugging, but a simple thing might be to just log to the event logger, or file log if you have that. Literally, putting "WriteLine("At class::function()") throughout portions of the code to see if you've made it there.
This will at least get you looking in the right direction (which ultimately is the code).
Update:
See Microsoft's How to Debug Windows Services article for details in troubleshooting startup problems using WinDbg.
This related question details nice ways to debug services that are written in .NET.
I agree with Scott, the easiest way to find out what's happening is to put some traces in the start-up code (maybe it doesn't even come to your start-up code).
If this doesn't help, you can post your code here so others can take a look.
perhaps lacking some dependence, try this :
- deregister your service
- register again
If fail at register means that lack an module.
If the StartRoutine is empty, you are probably starting it somewhere else.
IIRC you need to fire off a worker thread, and then return from StartRoutine.
One of the problems which may lead to this error is if windows service which needs to be deployed consists of some error i.e it may be simple authorization error or anything as in my case I have referenced some folders and files for logging which were not existing, but when provided the right path of those file and folders it solved my problem.
I ran through every post on this particular subject and none of the responses solved the problem, so I'm adding this response in case this helps someone else. Admittedly this only applies to a new service, not this specific case.
I was writing a File listening service. As a console app, it worked perfectly. When I ran it as a service, I got the same error as above. What I didn't know (and many of the MSDN articles about services conveniently leave out) is that you need to have your class executed from within ServiceBase.Run( YourClassName());. Otherwise, your app executes and immediately terminates and because it terminated, you get the error above even if no error or exception occurred. Here is a link to an article about this. It actually discusses setting up your app for dual use - Console app and service: Create a combo command line / Windows service app
I had that issue and the source of my problem was config file. I edited it in notepad and notepad added one special character which cause service not to run properly because config file was ruined. I saw that special character in notepadd++ and after delete it, service started to run successfully as previous did.
In my case, the correct .NET framework was not installed on the server that I was installing the Windows service on.
One other reason is If you copy the DLL in 'debug' mode to installation folder this issue will come.What you need to do is Run the project in 'Release' mode copy the DLL or directly form Release folder rather than Debug folder,,and copy that DLL in to installation folder,it will work.You can see the reduction in size of DLL ,it will not contain any debug symbols and like that

Resources