Search This Blog

Monday 19 April 2010

WatchDirectory Tasks as a Windows Service

Q&A

Here are a few tips if you have trouble running a WatchDirectory task as a Windows Service.

Make sure it works at all

As a first step, please make sure the task works properly when you manually start it (Not as a Windows Service). Set How to Run to Manually. When you have the task working properly "Manually", read on.

Network Directories

If your task only works on "local directories" (typically C:\ or D:\), you can skip this. If your task accesses drives on other computers ("The Network"), read on.

The Windows Service created by WatchDirectory runs as the user "Local System". Notice the word "Local"? The task can not access any network resources. Only directories on the computer where this task runs are accessible. So if you have a task that reads (monitors) or writes to network directories, you need to change the user that runs this task to someone who has access to those directories, see Change the User (last paragraphs of this post).

P:\

Are you accessing Mapped Network Drives? Please note that drive mappings (typically something like P:\ or X:\) are set by Windows based on the logged in user. Also, depending on the Windows version, those mappings are not available to Services even if you Change the User of the Service. Always use UNC naming for network drives (\\Server\Sharename\Directory instead of P:\Directory).

Network Share Permissions

When you share a directory on the network, older Windows versions by default gave Full Control (read, write, delete...) to anyone who is allowed to access the share. Current Windows releases default to read-only access. If your task needs to write to a network share, please check if the User running the task has proper privileges.

Batch Files

All the network issues above also apply to bat/cmd files you write. Please access network drives using their UNC name (\\server\share\directory\file.txt).

PATH
Please note the Windows PATH, where Windows can find executables to run, depends on the logged in user (the user that "runs" the WD task). If your script depends on programs that should be on your PATH, you may need to change the PATH in your bat script:

SET PATH=C:\My Binaries;%PATH%

Use Full Paths for files
If you currently have commands like

DoIt.exe "%WD_FILE%"

in your script, assuming DoIt.exe is in the current directory or your PATH, please change it to

"C:\Program Files\Do It Incorporated\DoIt.exe" "%WD_FILE%"

Note: %WD_FILE% contains the full path to the detected file, see Environment Variables.

Do not depend on the Working Directory
You should consider the "Current" (or "Working") directory to be RANDOM when your script is started. So, if you need a specific working directory, your script should set it itself.

Printing

Just like mapped drives, printers are maintained by Windows separately for each user account. Please see the last paragraph of this blogpost: Printing from a Windows Service.

Change the User for the WatchDirectory Service

Currently you can not change the user that runs the Windows Service inside the WatchDirectory Control Center. You must change it on the Windows Control Panel Applet "Windows Services":

  • Inside the WatchDirectory Control Center, select the menu "Tools -> Windows Services"
  • Open the settings for the service called watchDirectory:TheNameOfYourWatchDirectoryTask
  • Change the user/password for the Service

A future WatchDirectory version will make changing this user/password easier, from inside the WD Control Center.

Perhaps it is a good idea to create a new (domain) user called WatchDirectory specifically for your WD tasks. One important aspect of this user is to let its password "never expire". If you set it up so the password does expire, you will need to change the password for all WatchDirectory tasks every month...

Blog auf Deutsch lesen