C# remote watchdog - c#

We have a number of services (mostly console applications) running across several remote computers.
I need to implement some form of a watchdog to make sure all these services are running as expected.
Some services are executed via Windows Task Scheduler, and some are long-running apps.
What would be the best way of logging that those applications are run? I don't want to modify my running services.
I have administrator access to all machines.
A few options comes to mind:
For the long running applications:
Use psexec and retrieve a list of running processes and make sure my application is in that list.
For tasks invoked by task scheduler:
Use psexec and query schtasks and find out when my services was run and what the return code was.
Do you have any other ideas of doing this?
For the record, this will be implemented in C#/WPF.
EDIT:
Ok, I solved this in a pretty straight forward way. Thanks for your input.
Source: http://pastebin.com/fr6dDMp5

Have you seen WMI Tasks: Process? http://msdn.microsoft.com/en-us/library/aa394599%28VS.85%29.aspx

As I stated in my question, i solved this in a pretty straight forward way using Process.GetProcesses(string machineName) which can get processes on a remote machine. Source here: http://pastebin.com/fr6dDMp5

Have you considered to use XYNT Service (
http://www.codeproject.com/KB/system/xyntservice.aspx )?
It can run your console applications as a windows service and resetart them, if they stop.
You don't have to modify your existing services.

Related

Run a function / process as background in C# asp.net mvc

I'm new to this but I have a question: what is the best way, to run a function / process as background in C# ASP.NET MVC ?
For context, I'm creating a website for a project that does monitoring through snmp. The thing is that the various alerts have different timeouts between them. I want to go through all of alerts and see if the time between the last check and now is already passed. If so, the program will do another monitoring for that alert. It is supposed to run as background so it doesn't affect the rest of the site.
Can someone help me?
Thanks in advance ^^
Check this great article here: https://www.hanselman.com/blog/HowToRunBackgroundTasksInASPNET.aspx
TLDR: you have built in and third party options.
The built in is not as polished and with as many offerings as for example hangfire
The built in tries to delay the app pool recycle to finish the job.
Hangfire has good connectivity with redis and other persistence options
Both are not suitable for long running jobs, especially longer than the application pool restart period.
Both need some recovery mechanism in case the task is interrupted abruptly.
You can build an application running on a scheduler or a windows service or a linux cron job or even try something on the cloud as web jobs. Those are not related to the iis lifecycles and have none of the drawbacks mentioned above.

How to create background application running on Window Server

I need to write an application that run in the background (i.e., invisible to user). It should be always running when the server is on, regardless of user login or off. Or at least it should run on schedule (e.g., hourly). It also should still run after the server shut down and turn on again.
The application is used to backup some data from one server (linux) to the local server where the application run (windows server).
From my research, many suggest to use Window Service. But I'm newbie on C# and also on this area like Window Service.
Can anyone direct me where I should start?
Is Window Service a suitable solution? Or if there is better solution? Please explain.
Thank you in advance.
[CLOSED]
Thank you for all who has responded.
Yes, the best practice is windows services.
However, if your operations are simple enough to write in DOS batch file. You can schedule your task in windows task scheduler. It will save a lot of time and is easy to setup.
UPDATE (FYI)
I have a batch_update.sql file that needs to be executed at 1.00AM every day. I created batch_update.sql file and a batch file batch_update.bat in C:\bat.
batch_update.sql includes all SQL operations.
batch_update.bat calls batch_update.sql file to execute it as follow.
sqlcmd -U adminuser -P password -S (local) -i C:\bat\batch_update.bat -o C:\bat\batch_update.txt
Then I created a task in windows task scheduler to run at 1.00AM which works pretty well.
A windows service definitely sounds like the long term solution. For simplicity however, you could create a command line project in C# and then use windows scheduler to run it in whatever intervals you like.
I recommend windows service too, you can have some information right here.
Yes, Windows Service is the solution you're looking for. Here's a good starting point on MSDN.

Windows program that always runs

I have a requirement that a Windows Forms C# .NET 2.0 program running in user-space (not a service) must always be running. As I'm not infallible and make coding mistakes, I wanted to know of any extra safeguards I could use to ensure this requirement is met. The things I've been thinking of are TaskScheduler to check it every 5 minutes, A stub watcher or a secondary process. Are these good / bad ideas?
Thanks,
Richard
EDIT: The reason I didn't use a service (the obvious and sensible answer!) was the program runs in a kiosk type environment and has has a heavy GUI component. The service option didn't work well across Windows 2000 - W7.
EDIT: The second reason not to use a service was the app needs internet access and on some of our customer sites, proxies are set up to only allow specific users (not the local system account) so it would be tricky to ensure access if multiple users log onto the machine.
Task scheduler is a cheap solution for this which does work. I use this to keep our Perforce Proxy server running (had some issues with the service), and so far there's been no problems - though now I've said that the server's probably exploded!
However, the most complete solution is a Windows service which invokes your app. You can make that service catch error return codes from the app, restart it on failure and notify you by email, which may help you diagnose and fix those issues. I think the Task Scheduler does something similar but it won't be able to provide as much insight into your application as a custom service.
If you're unsure of how to do that, then something like this should work:
http://www.codeproject.com/KB/install/csharpsvclesson1.aspx
There are three approaches that you can take:
Heartbeat Message.
A heartbeat is useful in a distributed application and is simply message that is sent (from let say a client to server) to confirm that it is still healthy/running.
Manager Process
A stub program, implemented as either a user process or a service. It launches the main application, monitors any unhandled exceptions, reports errors, and restarts on failure.
An exception guard on the entry point.
A try-catch-all in the application entry point.
I would recommend either of the first two options; the third option, the try-catch-all, is a particular nasty hack for the lazy and inexperienced programmer (IMHO).
I have successfully used both heartbeat and manager process in a large distributed application.
UPDATE
As for ready-to-go™ restart managers, take a look at the Windows API Codepack as discussed in Emmanuel Istace blog post (http://istacee.wordpress.com/2013/09/21/automatic-recovery-restart-in-net-application/).
You can install the package from https://www.nuget.org/packages/WindowsAPICodePack-Core/

Monitor running apps in .NET?

We have several apps that run on a Windows 2003/2008 server. Some of these apps are Java JAR files that are kicked off with a Scheduled Task using a app.cmd file.
Others are "big ones" like SQL Server and IIS.
I'd like to write an app (or service, actually) that simply monitors those programs and sees if they are running or not.
This is a little beyond what I've done before. Oh, this needs to be written in C#.
I was thinking of some type of "heart beat" pattern so that every few minutes, I check if the thread is running (again, new advanced threading) and if so, send out an "All's OK" message (using SMTP or something).
Any tips where to get started?
Thanks for any suggestions.
You can use Process.GetProcesses():
Use this method to create an array of new Process components and associate them with all the process resources on the local computer.
You should probably handle each application on a case-by-case basis, for exaple although you could just check for the existance of the SQL Server process you would be better off running a query that (for example) checks for the existance of a given database in the server.
Similarly you could run a simple HTTP request against an IIS server to check to make sure that the permissions are set up correctly etc...
Obviously the way that you test your Java processes would depend on what they do, although you could still just check for to see if the process is running (be aware that if they are running as a service they may be running inside one of the svchost.exe processes - this doesn't apply to scheduled tasks though).

When do we use windows service?

Are there situations that we should use a windows service ?
I am building a client-server project (c#) and the server supposed to work alone without any user so someone advised me to use a windows service to run the server, is this right ? or there are a better solutions ?
Windows services are normally used when an application needs to continuously run. For example if they need to:
Wait for incoming requests. (Like through remoting or wcf)
Monitor a queue, file system etc.
If a program just needs to run periodically, like once a day. It is normally easier to create a scheduled task.
In your situation I would use a service for the following reasons:
You don't need to have a session running. This is good for security, and also reduces overhead on the server.
You get some of the managment commands built in for free
Start
Stop
Pause
Continue
You can handle server events such as shutdown.
Windows service can start running as soon as the machine is powered up, which makes ideal for running as a server, http server for example. No one is required to login.
You should create a Windows Service to run code in the background, without user interaction.
For example, a Windows Service will run even if no-one is logged on.
Any server that accepts connections (such as a mail, web, or FTP server) should usually be a Windows Service.
Well, a Windows Service provides a full framework for your application to work and to remain active while you want it to, so I think its ok.
Windows services are the right thing to use for something that should run all of the time, whether or not a user is logged in.
If you need something to run without an active user logged in, you need to use a windows service.
When you need the application to start running even when no one has physically logged into the machine, which is common with server machines, a service is a good candidate in this case. Especially because the service can be configured to auto start, which means the service will start when the machine is rebooted withut human intervention.
If however you are wanting to host web services (WCF) while a service is an option, you might consider hosting in IIS, this relieves you of writing the actual hosting code etc.

Categories

Resources