Running application using cherryd

895 views
Skip to first unread message

Cherrypy-noob

unread,
Mar 16, 2017, 9:29:36 AM3/16/17
to cherrypy-users
Hi all,

so as my username says I'm new to cherrypy and also in general linux and linux administration.

I have created my simple cherrypy application which is is simple and consists of 1 python file. It needs to run in a specific virtualenv and is running behind apache using mod_proxy. This work but I have to start cherrypy app manually.

The goal is now automating the start/stop of cherrypy application. I wanted to play with cherryd but I'm not getting how I can tell it to load my specific application (eg. python file).

Is this the imports option? If yes how do I need to configure my app? Use quickstart or tree.mount? Do I put that at start of module or leave it in  if __name__ == '__main__':?




Cherrypy-noob

unread,
Mar 20, 2017, 8:00:24 AM3/20/17
to cherrypy-users
After a lot of trial and error and some research I came to following most simple solution.

I use Ubuntu 16.04 which by default uses systemd. So the solution is to create a systemd service. Advantage being that you do not need any daemonizing or forking.

In

/lib/systemd/system

create a file called cherrypy.service (or whatever you want to call it). Add to that file following info:

    [Unit]
    Description=Cherrypy Web Server

    [Service]
    Type=simple
    WorkingDirectory=/path/to/cwd
    ExecStart=/path/to/cherrpy/script.py

    [Install]
    WantedBy=multi-user.target

Run commands:

sudo systemctl daemon-reload
sudo systemctl enable cherrypy.service

If you now reboot your cherrypy application should start automatically at boot.

You can manually start/stop the service with

sudo systemctl start cherrypy
sudo systemctl stop cherrypy

You do not need to add any daemonzing code to your application.
An interesting "trick" is to add

#!/path/to/virtualenv/bin/python

to the top of your script/application file. This way your desired virtualenv is used for running the script

Hope this helps other people new to this to get this up and running quick and simple.

Michiel Overtoom

unread,
Mar 20, 2017, 12:34:03 PM3/20/17
to cherryp...@googlegroups.com
Hi Joos,

> On 2017-03-20, at 13:00, Cherrypy-noob <joos....@gmail.com> wrote:
>
> After a lot of trial and error and some research I came to following most simple solution. [...]

Thanks for sharing.

If you configure your webapp this way, is it run using test-develop or production environment settings?

Greetings,

Cherrypy-noob

unread,
Mar 27, 2017, 4:23:12 AM3/27/17
to cherrypy-users
Hi Michiel,

you got me there. I don't know. Whatever is default unless you specific differently in your app:

cherrypy.config.update({'environment' : 'production'}))
As far as I understood it, environment only changes some defaults for the config like no traceback on webpage in production. Setting it in the app is not ideal but the app/script could read in the config file and have it as a input parameter so you can define the config by config file and not within the app (makes more sense).

Another option is to use cherryd and then set that to autostart on boot with systemd or other tool. For systemd you for sure need to adjust settings (type=forking) and maybe more and adjust your script (quickstart won't work).
Reply all
Reply to author
Forward
0 new messages