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.
Related
I have an application on Windows 10 VPS that I will be only able to have access via the command prompt. After the application is ran, I want it to be able to retrieve input from the command prompt, and give back output. Although, after disconnecting the application should still run in the background.
When I reconnect to the VPS, I want to regain the ability to input and output text through the command prompt, without restarting the application.
Here's some documentation example code of how to create a TCP client in C# if you decide to go the TCP/Sockets route over WCF: http://www.winsocketdotnetworkprogramming.com/clientserversocketnetworkcommunication8q.html
And here's the MSDN page on WCF: https://msdn.microsoft.com/en-us/library/ms731082(v=vs.110).aspx
This is the MSDN page on creating a service application: https://msdn.microsoft.com/en-us/library/zt39148a(v=vs.110).aspx
Do note: "The Windows Services project template that is required for this walkthrough is not available in the Express edition of Visual Studio."
But don't let that concern you. All this means is that your program would not be able to register as a windows service, and therefore could not truly operate like a service which can start on boot, pause, stop restart, etc. Howerver, this doesn't stop you from creating a program that automatically starts. You could simply place a shortcut to your program in your startup folder:
C:\Users\Username\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
Hopefully that helps get you started in the right direction!
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.
If my .NET client uses System.IO.File.Copy to copy a file from \server1\share1\file1.txt to \sever1\share2\file2.txt, the file's data get read down to the client and then written back up to the server.
Is there an easy way to speed up things a bit by putting a process on the network-machine that realy does the copying?
I dont look for advice on programming such a service. I would rather find the tool or windows-functionality that already does this.
This sounds like a job for telnet or ssh, but these can be a pain to set up. I recommend you look at PsExec from microsoft which allows you to execute programs on a remote machine. You could simply use the copy program found in system32 through PsExec.
There is no need to create your own program to do this, just use the command line tools found on the target machine.
Almost certainly. I can think of a couple basic ways to do it:
Create a simple console applicaton that takes the source and destination paths/filenames, and performs a File.Copy(). Place it on the remote machine in a known location. Then invoke the process on the remote machine using PsExec. Your client app must be running in full trust in order to invoke a process programmatically, and the user running the app must have permission to run programs on the remote server.
Create a simple web method that again takes the source and destination and performs the copy. This requires setting up IIS on the network server with the requisite permissions to perform file access outside of the IIS "sandbox". However, it requires fewer client code permissions.
FYI CopyFileEx already does this if your client and server are Vista and later. So, not point in duplicating any of that effort in that case.
-scott
I wrote a C# application that is running well on my xp development machine.
When my client is running it on different PC, it seems to freeze on both vista and 7 machines.
I would like to know if there is any issue on running dotnet applications on vista/7.
I compiled the application for both framework 2.0 and 3.5 but it didn't help.
The log file neither doesn't help.
Is there any way to know where is the application freezing (of course, the client doesn't have VS for attaching to the application) ?
Thank you in advance for any help.
Pierre.
Generally, .NET application should run the same on any Windows version with .NET framework installed. Most common problem that occur are related to the new User Account Control policy in Vista and 7, which prevents applications from accessing areas requiring elevated privileges (which would otherwise run fine under an Administrator account in XP).
For example, if your application is trying to write configuration settings to the Program Files folder (which is a really bad idea, btw), or is using some hardcoded disk or registry paths instead of environment variables provided in .NET classes, it may fail under Vista.
To quickly check if this is an UAC privilege problem, try to run the application as an administrator (have your client right click the .exe file and select Run as Administrator). If it works, then this suggests that you need to examine your code and update it to make sure you are only accessing allowed areas.
Check this link for more information: Making apps UAC aware.
As redsquare suggested, the best way to test your application in a variety of Windows systems is to run them in several virtual machines (MS Virtual PC or VMWare Player, both of them free for download).
You do not give much information in order for us to help a great deal.
I would start by trying to recreate the issue locally on a virtual machine (vista one available here) and then profiling the app with something like memprofiler.
Ask your client to capture a process dump using Windows Task Manager,
In Task Manager, find the process that hangs.
Right click and capture a memory dump.
Then you can do some analysis on the dump to see why it hangs.
If you are not familiar with dump analysis, find someone who can help or open a support case via http://support.microsoft.com
csharp applications are supposed to run on vista/win7. This is their home:)
Your problem is not the OS. its your application.
Most probably the application is looking for a file, a folder, something on the client side that is not there and its freezing. this is my experience.
Check for project independencies and make sure you have shipped to your clients everything your project needs.
and last, distribute to one client the debug version with debug messages enable and get the feedback from their system.
I'd like to know how to restart a computer in such a way that once restarted it will log in using given credentials via a command-line command or perhaps using some other method perhaps in C# code.
To give context, I'm going to need to run this remotely on Windows XP, Vista, And Windows 7 and I am attempting to form a large list of commands to be given to psexec in the command line in a consecutive order. At given points, it may be necessary to restart and log in and resume executing the commands, and I am wondering how I can accomplish this. I know PsShutdown exists and it handles restarting, but how about logging in? How will I know when the machine haqs fully restarted so I can give the login command? C# code will work as well but it's not preferrable.
So main issues:
how to restart a machine (I already pretty much know)
how to know when a machine has restarted
how to log in as soon as it is capable
how to know when it is fully logged in and ready to execute commands
If you have access to the registry on the target PCs, you can set the DefaultPassword, DefaultUserName and (optionally) DefaultDomain.
http://support.microsoft.com/kb/315231
This definitely works with XP. I think it is the same withe Vista/Windows 7.
Once this is set, you can reboot the PC using PsShutdown.