HOWTO: Run CouchPotatoServer as a Service on Windows

Most commonly asked questions will have thire awnsers here, please search before asking!
bengalih
Senior Member
Posts: 334
Joined: 14 May 2012 23:39
Contact:
Status: Offline

HOWTO: Run CouchPotatoServer as a Service on Windows

Postby bengalih » 31 May 2012 22:24

Joel posted a couple of options for installing as a Windows Service at the bottom of his setup guide here. Below I present an alternate method of using the Windows Task Scheduler to run as a psuedo-service at boot.

This method may be a bit easier, and its already built into Windows 7+ (and maybe Vista... don't have a box to check all the details on).

Before you do anything else, you want to get a working CPS install (try following Joel's setup guide linked above). Make sure everything is working 100% with an interactive install before moving on to the below.

Once you have it working, be sure you REBOOT your windows box before moving onto the steps below. There are some strange things that can happen with GIT and reported version numbers with this method if you don't reboot after install.

Installing CouchPotatoServer as a Scheduled Task (psuedo-service)

First thing I like to do is relocate the CP data. By default this gets stored under your user profile (%appdata%). So, if you are user JSmith it would be here (on Vista+):

C:\Users\JSmith\AppData\Roaming\CouchPotato

If you want to run as a service it generally isn't a good idea to have your data associated with any particular user. So, to move the data we need to do the following:

1) Create a directory to house the CouchPotato configuration data

Windows already provides a "standard" directory for non user-specific application data:

On Vista/Win7 this is: c:\ProgramData

So, on my Windows 7 system I would create the following:

C:\ProgramData\CouchPotato\

2) Update CP with the new config file location

Go into CP and go to Settings > About. You will see a section that shows something similar to the following

Code: Select all

Startup Args
Namespace(config_file='C:\\Users\\jsmith\\AppData\\Roaming\\CouchPotato\\settings.conf', console_log=False, daemon=False, data_dir='C:\\Users\\jsmith\\AppData\\Roaming\\CouchPotato', debug=False, pid_file='C:\\Users\\jsmith\\AppData\\Roaming\\CouchPotato\\couchpotato.pid', quiet=False)


You can see above that it is loading the settings.conf file from your user's application directory. We want to change this to the new location we have created. To do this:
  • Click on the "Show Advanced Settings" box on the top right and then on the "General" settings.
  • Under the "Advanced" section you will see a "Data Dir" option.
  • Navigate to the new directory you created and click on Save. You will most likely have to check the "Hidden Folders" option in order to see the ProgramData directory.

Once you have saved the settings, go ahead and SHUT DOWN CouchPotato by using either the shutdown option in the settings menu, or (less preferable) by killing the python process.

3) Copy the configuration data

Copy the entire contents of the old %appdata% directory to the new location. The data contents you are copying will be your settings.conf file as well as a few subdirectories (cache, db_backup,logs) and your couchpotato.db If there is any data in your new directory, you should overwrite it with the copied data.

I recommend using a COPY instead of a move so that the new location contents will inherit the permissions of the parent folder - this should cut down on some possible permissions issues. Once you have copied all the data you should have a directory structure similar to the following:

c:\ProgramData\CouchPotato\cache
c:\ProgramData\CouchPotato\db_backup
c:\ProgramData\CouchPotato\logs

as well as your settings.conf and couchpotato.db in the root.

Once that is copied over you can optionally go ahead delete the original C:\Users\xxxxx\AppData\Roaming\CouchPotato directory.

4) Restart CP - Now that all our configuration information is moved out of the user's appdata and into a common folder, we need to tell CP to startup with the new config file. This is done with some special switches for the python file.

Start CP with the following:

CouchPotato.py --data_dir "path to your data directory" --config_file "path to your settings.conf" --pid_file "pid path"

for example...

--data_dir "c:\ProgramData\CouchPotato" --config_file "c:\ProgramData\CouchPotato\settings.conf" --pid_file "c:\ProgramData\CouchPotato\couchpotato.pid"

Once CP launches you can go into the Settings menu and look at the Startup Args again and it should be reflective of your new location.

5) Prevent CP from opening a browser - Since we are going to run as a service, you don't want a hidden browser window popping up. So, simply record the URL to access CP then go to Settings > General > Basic and uncheck the Launch Browser button.

If things aren't working at this point - STOP. Go back and re-read and try again. If you can't get CP running properly from an interactive prompt, we don't want to try and run it all hidden in the background as a service!

Now that all that is done, we can go ahead and actually configure CP to startup like a service. Technically, we won't be configuring it as a service, but as a scheduled task, to run on system startup

To create a startup task, open the Windows Task Scheduler (built into Windows...under Administrative Tools or Control Panel)

Inside the Task Scheduler do the following:
  • Right-click on Task Scheduler Library
  • Choose New Folder - give it a name of your choosing
  • Right-click on new folder and choose Create Task (not basic!)
  • On the GENERAL tab, give a name and description like "CouchPotato". Click on the "Change User or Group" and type in "SYSTEM" and click OK.*
  • On the TRIGGERS tab, click New and then choose to begin the task "At Startup". Click OK.
  • On the ACTIONS tab, click New and then choose "Start a Program". In the Program/Script, browse to and select your CouchPotatoServer.py file. In the Add Arguments field enter in --data_dir "c:\ProgramData\CouchPotato" --config_file "c:\ProgramData\CouchPotato\settings.conf" --pid_file "c:\ProgramData\CouchPotato\couchpotato.pid" as we did in step #4 above. Click OK.
  • On the CONDITIONS tab uncheck everything you can
  • On the SETTINGS tab check ONLY "Allow task to run on demand" and then from the drop down on the bottom select "Stop the Existing Instance".
  • Click OK to create the task

Now, find your newly created task in the folder you created in Task Scheduler. Right-click and select "Run". If you followed the instructions properly then it should run the python script as the system account in the background and within a few seconds you should be able to go to your CP URL and access your server. Next time you boot up your computer this task should automatically run even before login!

If you have any problems look at the "Last Run Result" column in Task Scheduler to see what errors are there. Review the steps posted here to be sure you have typed everything properly. You can also check out MS references on the Task Scheduler here and here.

Other things to check:
  • That the python.exe process is running when you start the task.
  • That you can run it via the command line (interactively) as we did in step #4
  • The CP log files (in the data directory)


Happy Sitting!

ADDITIONAL NOTES
* In the above example we configured the Scheduled Task to run as the SYSTEM account. The SYSTEM account generally has full access to everything running on the box and therefore is a good account to use to ensure that everything can be accessed. There are a few issues with this however that you should be aware of.

#1 - As SYSTEM, the service can do anything it wants. As mentioned, this can be good (no permissions problems), but it also means that the service (in this case CouchPotato) has full access to your box. Should the developer (wittingly or otherwise) write code that could compromise your computer, you would be allowing it to run unchecked.

#2 - SYSTEM does not have any network access (and can't normally be granted it). This might cause a problem if you are running CP across multiple systems. For instance you install CP on one machine but want it to download to, or pull data from another. If you are running in this configuration you will probably need to make a special "service" account. A service account is a user account that is used to only run a service. You can of course use a normal user account that is used for other things, although this isn't recommended. If you have a windows Domain you should use a windows account. Otherwise, you should create an account with the same name and password on all boxes. Ensure that the Scheduled Task is set to run as that user account instead of SYSTEM and also that it has permissions to all necessary files/folders.
You may also have to ensure that the user's profile environment is properly set up (e.g. mapped drives). For more info see the following threads:
http://couchpota.to/forum/showthread.php?tid=90
http://couchpota.to/forum/showthread.php?tid=255

sysrage
Junior Member
Posts: 12
Joined: 05 Aug 2012 18:21
Contact:
Status: Offline

RE: HOWTO: Run CouchPotatoServer as a Service on Windows

Postby sysrage » 12 Aug 2012 18:27

I used Windows Task Scheduler to start CP as described above. This works great, for the most part. However, is there any way to have the task automatically restart if it's no longer in the 'Running' state? CP crashed or something at some point last week and was no longer running. I'd like to have it automatically restart itself in such a case in the future. Thanks!

darksupernova
Junior Member
Posts: 28
Joined: 24 Oct 2013 09:30
Has thanked: 4 times
Status: Offline

Re: HOWTO: Run CouchPotatoServer as a Service on Windows

Postby darksupernova » 27 Mar 2014 18:14

Hi,

Thank you for this guide but I cannot seem to get it to work! I have followed everything correctly, to double check I created a shortcut with the following target:

C:\CouchPotato\CouchPotato.py --data_dir "c:\ProgramData\CouchPotato" --config_file "c:\ProgramData\CouchPotato\settings.conf" --pid_file "c:\ProgramData\CouchPotato\couchpotato.pid"

This pops up a python.exe command box and CP can be accessed. However when I run the task with the same argument (--data_dir "c:\ProgramData\CouchPotato" --config_file "c:\ProgramData\CouchPotato\settings.conf" --pid_file "c:\ProgramData\CouchPotato\couchpotato.pid") nothing happens. Python doesnt start and CP doesnt open. I have tried clicking to run it with the highest privs, I have also tried using my user account instead of system but I can't get it to do anything..?

The last run result is "terminated by user", that was me trying to stop it and start it again...

Any ideas?

User avatar
clinton.hall
Moderator
Posts: 9224
Joined: 28 Jun 2012 12:55
Has thanked: 94 times
Been thanked: 465 times
Contact:
Status: Offline

Re: HOWTO: Run CouchPotatoServer as a Service on Windows

Postby clinton.hall » 28 Mar 2014 05:17

how about getting the task to run a batch script that has the full command and argument?

Perhaps even

Code: Select all

C:\Python27\python.exe C:\CouchPotato\CouchPotato.py --data_dir "c:\ProgramData\CouchPotato" --config_file "c:\ProgramData\CouchPotato\settings.conf" --pid_file "c:\ProgramData\CouchPotato\couchpotato.pid"

darksupernova
Junior Member
Posts: 28
Joined: 24 Oct 2013 09:30
Has thanked: 4 times
Status: Offline

Re: HOWTO: Run CouchPotatoServer as a Service on Windows

Postby darksupernova » 28 Mar 2014 11:17

Thank you for your response - Ill try it now.

darksupernova
Junior Member
Posts: 28
Joined: 24 Oct 2013 09:30
Has thanked: 4 times
Status: Offline

Re: HOWTO: Run CouchPotatoServer as a Service on Windows

Postby darksupernova » 29 Mar 2014 12:17

If I run the batch file everything works, but if I ask the system to run it, nothing happens... Maybe there are some weird permission issues?

Well I've been playing around with it and it suddenly started working. Not sure which bit fixed it, but using the task scheduler and a batch file now.

Thank you for your help :)

PlzSABit
Junior Member
Posts: 13
Joined: 13 Mar 2015 20:31
Status: Offline

Re: HOWTO: Run CouchPotatoServer as a Service on Windows

Postby PlzSABit » 13 Mar 2015 20:47

The link in the original post is dead, but I'm assuming it's a method for installing CP using srvany. I've used srvany in the past, but ran into issues. Also, I don't believe srvany is supported/developed any longer. I also tried started CP via Task Scheduler, but that wasn't reliable. As a result, I just started using NSSM (the Non-Sucking Service Manager) to install Windows services and I really like it. From NSSM's own page:

"nssm is a service helper which doesn't suck. srvany and other service helper programs suck because they don't handle failure of the application running as a service. If you use such a program you may see a service listed as started when in fact the application has died. nssm monitors the running service and will restart it if it dies. With nssm you know that if a service says it's running, it really is. Alternatively, if your application is well-behaved you can configure nssm to absolve all responsibility for restarting it and let Windows take care of recovery actions.

nssm logs its progress to the system Event Log so you can get some idea of why an application isn't behaving as it should."

There's a GUI to install the services and the website is pretty thorough with explaining what everything does.

Note that I did not move the CP config data as suggested in this thread and everything seems to be working just fine so far. However, I will probably still move them at some point in the future.


Return to “FAQ”