NT Service not working with WinSCP FTP Bat file

Started by rickmds, December 06, 2016, 02:49:52 AM

Previous topic - Next topic

rickmds

Hi
I have a Winbatch script, watch.wbt (attached) that polls a server directory (D:\MAST22\Funnel\) awaiting files to arrive several times per day.  When the files arrive the script copies and moves them to several directories on the server and then SFTPââ,¬â,,¢s them using the WinSCP bat file to our development server.  The WinSCP bat file works perfectly standalone or within the wbt & compiled exe (line 122 or 123 in script).  However when the script is compiled to an exs NT service the WinSCP no longer works. 

The contents of the FTPTrans.Bat file follow:
winscp.com /command "option confirm off" "open SFTPSITE:USERNAME:22" "lcd c:\ftptosend" "put -delete *.*" "close" "exit" "/log=C:\FTPSent\winscp_log.txt"

I also attached the script for creating the NT service (AddWatchService.wbt).

Can you please advise on troubleshooting in order to make this work?

Best
Rick

td

Just changing the extension of a compiled script does not make the script a service.  Using the third party Watch extender to suspend the service will only make the system think that the service is hung.  Assuming your script is running on Windows Vista/2008 or newer, your service cannot interact with the Windows desktop.  Also, a service running as "localsystem" often does not have access to network resources unless the computer domain account of the computer running the service has network privileges.

See the 'Service Script' topic in the WinBatch Help File for more details on the above.  The topic includes a template service script. 
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

rickmds

Tony
I would like to summarize my situation to be sure I understand your comments.  I have relied on the WinBatch documentation and technical support templates to create the NT services successfully in the past.  Past successful NT Service scripts have had the same functionality as this one, but used the internal Winbatch FTP capabilities; they work flawlessly but they are not secure.  The attached script in this posting required the SFTP protocol and since Winbatch did not have this capability I used WinSCP.   With this script, compiled as an NT service all functionality is working except the call to WinSCP to invoke the SFTP file transfers. 

Based on your comments I am concluding that using WinSCP requires Desktop interaction and as a result, it will not work as a compiled NT service.  Is this what you are saying?   If yes my workaround is to pull the WinSCP SFTP bat out of the NT service and have it run on an hourly basis using Windows Task scheduler.  This works but it is not elegant.

Thanks
Rick

td

You never mentioned which version of Windows you are using but your script is not a proper service on any modern version of Windows.  The previously mentioned help file topic is out of date but it does have a still useful template for Service Control Manger friendly service.  Your script as written will not respond to some important Service Control Manger commands and that is not a good thing despite your perceived successes in the past.

As to the immediate cause of your problem, it could be one or more of the previously mentioned issues or just cause by a non-existent file association because the script is running as localsystem.  You will  have to sort that out.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

rickmds

The script is being used on Windows server 2008 & 2012 and loads and runs as a service on both versions.  Can you provide feedback on troubleshooting the WinSCP batch call when it is running as a service?  If I understand you correctly it should be working?

rickmds

After changing the service Logon from local user to administrator the SFTP WinSCP upload worked as designed.  Problem solved, thanks for you suggestions and feedback Tony.

td

Changing the service's account worked because of the previously mentioned file association issue.  The localsystem account uses the default user profile which is likely lacking many file associations among other things. However, it is still recommended that you rewrite your script to work as Service Control Manager friendly service.   Leaving it as is because it always worked in the past, is the man who never wears a seat belt because he never needed one.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade