So that FHEM is running constantly, even when noone is logged into my server, I'm going to try to get it running as a Windows Service, which I can configure to run as a particular user as soon as the server boots.
I'm going to base the some of the following on the excellent tutorial to install uTorrent as a service - http://www.deciphered.net/blog/2007/04/25/howto_install_utorrent_service_windows_home_server_whs_ctp, so many thanks to the original author.
Remote into your Windows Home Server box as the administrator. Download and install the Windows Server 2003 Resource Tools from http://www.microsoft.com/downloads/en/details.aspx?FamilyID=9d467a69-57ff-4ae7-96ee-b18c4790cffd&DisplayLang=en, using default installation settings.
Create Service User
- On your home server, click Start, right-click My Computer and select Manage
- Open Local Users and Groups, select Users then right-click and add a new user
- I've called my user FHEMService, make sure you:
- Uncheck "User must change password at next logon"
- Check "User cannot change password"
- Check "Password never expires"
- After you've created your new user, we need to temporarily give the user access to log on remotely:
- Double click on the newly created user account
- Select the Member Of tab and click Add
- Type in the group name "Administrators" then click OK
- Click OK again to apply the changes to the user account then log out of the remote session.
Log In as New User
So that we can be sure the new user has enough permission to execute the FHEM server, let's log into the
- Log out of the remote desktop session
- Log in as your new user
- Give the new user full access to the FHEM folder:
- Open Windows Exporer and browse to your FHEM folder, in my case "c:\strawberry\FHEM"
- Right-click the FHEM folder and click Properties
- In the Folder Properties window, select the Security tab then click Add
- Type in the name of the new user then click OK
- Select the added user and make sure the user has Modify, Read & Execute and Write access.
- Click OK when done.
- Create the new Windows Service:
- Open command prompt (Start > Run > cmd)
- Enter the following commands to create an empty windows service:
- "c:\Program Files\Windows Resource Kits\Tools\instsrv.exe" FHEM "c:\Program Files\Windows Resource Kits\Tools\srvany.exe"
- Configure the service to run FHEM
- We now need to configure our new service with the correct command
- Open the registry editor (Start > Run > RegEdit)
- Locate the key HKEY_LOCAL_MACHINE > SYSTEM > CurrentControlSet >Services > FHEM
- Add a new key called "Parameters"
- Add the following two string values to your new key:
- "Application" with a value point to your perl.exe, for example on my machine "c:\strawberry\perl\bin\perl.pl"
- "AppParameters" with a value pointing to your fhem.pl and config files, eg "c:\strawberry\fhem\fhem.pl config.txt"
- Close the Registry editor
- Configure the service security and test it:
- Open the Services management console (Start > Run > Service.msc)
- Double-click the new FHEM service
- Click the Log On tab and enter the FHEMService account details
- Click OK and attempt to run your service
- Check your log file to make sure the service is running successfully.
- Finally, connect to your server from a remote machine, and then fully log out from your server - you should be able to carry on using FHEM.
Remove Admin Privileges From FHEMService User
Don't forget to go back in as the server administrator and remove admin privileges from the FHEMService user - in theory this means if anyone finds a way to hack into your FHEM server remotely, there's very little they can do to the Windows environment.
It Didn't Work!
If you've run your service, and you either can't see your log file or your log file isn't updating correctly, there are a few gotchas to bear in mind:
- Because you're running the service using an absolute path, you can't rely on relative path references in your config file - I had to change my modpath from "." to "c:\strawberry\fhem" (make sure there's no trailing backslash)
- Try opening a command prompt, and copy-and-pasting the two registry entries you entered in step 5 above into a single command - does your server run or not?
- If all else fails, it may be possible to create a batch file in your fhem folder with the full command, and point your service to this batch file, but I've not tried this!