How to create background application running on Window Server - c#

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.

Related

Launch an exe application from windows service

I have a service which should open an exe application ( C# Application ) based on certain conditions. When the service is started in debugging mode ( Visual studio ) it opens the application. But when it is installed as a service, it does not do so. It fails to open the application. Why is this happening?
What you're trying to do isn't directly possible under normal circumstances - simply launching an app in a new process from your Windows Service code is not going to interact with the GUI of the currently logged in user I'm afraid.
There are ways of communicating between a service and the GUI however.
This discussion might point you in the right direction.
Based on your comments, I think what you are really looking for here is a normal userspace application and a scheduler. You might want to use Windows' own scheduler to run the application every monday, if it is an always-on box, or place the application in Startup. When the application runs, it should check the current day of week, and if it is monday and the application has not previously run this day, the application should start. If not, you can safely terminate the application entirely.
Thanks for the answers! I found out a solution for it and im posting it here.
I created a dummy app which is hidden on startup and it does the exact same function that the service was intended to.
1.create a dummy app (copy paste code from service to form application)
Hide it after start up.
2.start the application right after installation.
3.add registry key so that it starts up after a system reboot.
in simple words, clone service behavior.

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).

C# remote watchdog

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.

How to run a command-line program on a remote server

I have written a tool that is run on the command line.
I can test it on the command line on my machine, and it works fine.
I then remote into the target server machine and run it on the command line there, and it still works.
However...this tool needs to remain running (its a monitoring tool).
I can't leave my remote session to that machine open for too long, as other people need to use it, and when I close my remote session, that command line tool ends.
I don't know how to, and would like to avoid needing to, write a windows service, but essentially I need that capability. I need to almost wrap my command line tool with a windows service that can remain running on the target machine while I disconnect.
Can anyone provide any insight/strategy on how to do this?
Edit:
Both of the below posts are helpful.
If it's Windows Server 2003 or earlier you can use Srvany.exe to run any application as a service. See here for details.
If it's Windows Server 2008, SrvAny isn't supported officially, but you might be able to hack it to work by copying it from Win2003 Resource Kit or earlier and then combining it with the instructions in Thomas answer in some way.
However, writing a proper Windows Service is really quite simple and it's the best way to make sure that it'll coexist happily with everything else.
Just create a Windows Service project in Visual Studio. In the OnStart you create a thread that runs your code. In the OnStop you use a flag to stop the service. Then you can make it better later.
See if this page helps:
http://www.msfn.org/board/topic/83272-how-to-run-a-program-as-a-service/
An alternate solution is to use the PsExec to launch your monitoring tool on the remote machine in detached mode, i.e.:
PsExec.exe \\remote_machine -d C:\...\monitoring_tool.exe
PsExec.exe launches monitoring_tool.exe on the remote machine and then terminates itself. It does not wait for monitoring_tool.exe to terminate.

How can I start an application on a different system remotely?

I've got a situation where i have one system running a master application that communicates with slave applets on other systems, problem is, i can't guarentee that the slave applets will be running, nor that an operator can physically utilize the system to start the applet (and I'm also assuming they don't know how to use Remote desktop, these are minimum wage test operators)
Is there an easy way to remotely launch an executeable on another system?
security is not a concern here as this is a closed network.
If you're on the same network and have access credentials (username+password) to the remote computer, then Mark Russinovich's PsExec is just the tool you need.
WMI is your friend. See the following article which covers starting processes locally and remotely using WMI:
How To: (Almost) Everything In WMI via C# Part 2: Processes (CodeProject)
Kev
You can use the Windows AT command to schedule tasks on a remote machine.
Any chance of converting the remote applets into Windows services? They can be configured to start when the system starts; to retry if they fail; and to notify someone if there are problems. They're quite trivial to create with Visual Studio.
here's a 2 step solution
Write a batch file to kick start
the application/service in the
context
Have the batch file on a
shared network path which can be
accessed from the machine would like
to launch the application/service.
Honestly, I would suggest the PsExec solution by Piskvor. But you can try out this as well ;)
another simple solution if you're not on the same network (accessing your home computer to run teamviewer for example) is to create a rule in Outlook (if you have it) whereas if you receive an email say from yourself, you can have it start an application if there are certain pieces of text in the message body or subject line, for example:
Create a mailbox rule:
Step 1 - Conditions: From & With specific words in the body (from being your mail address and the message body: Start Teamviewer
Step 2 - Start Application (browse to the location of the .exe you want to launch)
Step 3 - enable that bad boy!
Obviously, you have to have Outlook open and accepting incoming mail in order for this to work.
you can tailor this to your requirements, ie: if Start Teamviewer is too easy, you can put codes or passwords in there too, so when the email arrives, it'll read the message, recognise the info based on the rule, and launch the application. :)

Categories

Resources