简体   繁体   中英

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.

An alternate solution is to use the PsExec to launch your monitoring tool on the remote machine in detached mode, ie:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM