How to daemonize bottle app

796 views
Skip to first unread message

Lee Connell

unread,
Jun 10, 2011, 11:25:24 PM6/10/11
to bottlepy
I am running a bottle app and want to know how to properly daemonize
it and log stderr and stdout to a log file. I've tried a couple
things and so far it doesn't work, mainly my issue is it doesn't log
properly. I've tried the daemonize.py from djangoproject utils, i've
tried writing an init script using start-stop-daemon as well as
executing it directly and redirecting stderr and stdout. I've tried
redirecting stdout and stderror within my bottle app as well. This
code below works great for starting and stopping, but i can't find a
way to properly redirect my output, any ideas?

#! /bin/sh
APP_CWD=/app/python
DAEMON=$APP_CWD/pyrest.py
APP_OPTS=''
APP_PID=$APP_CWD/pyrest.pid
APP_PROC=pyrest
APP_LOG=$APP_CWD/server.log

case "$1" in
start)
echo "Starting $APP_PROC"
#cd $APP_CWD
#nohup $DAEMON > $APP_LOG 2>&1 &
#exec > $APP_LOG 2>&1
#$DAEMON $APP_OPTS
start-stop-daemon --start --background --chdir $APP_CWD --pidfile
$APP_PID --make-pidfile --exec $DAEMON -- $APP_OPTS 1>>$APP_LOG
;;
stop)
echo "Stopping $APP_PROC"
start-stop-daemon --stop --oknodo --quiet --pidfile $APP_PID
#pkill -f < pgrep $APP_PROC
;;
*)

Branko Vukelic

unread,
Jun 11, 2011, 7:46:38 AM6/11/11
to bott...@googlegroups.com
Looks like an Arch Linux system.

I don't have the code on my right now, but I remember it involved
using Python's standard libraries to set the UID and GID of the
running app (so it doesn't run as root), writing out the .pid file so
I can use kill to stop the app, and maybe a few more things.

http://docs.python.org/library/os.html#os.setuid
http://docs.python.org/library/os.html#os.setgid
http://docs.python.org/library/os.html#os.getpid

The .pid is written out to a known location, so the daemon script can
use it to kill the process.

You also want to redirect sys.stdout to a file. To background the
process, I used nohup. It's much easier than using a python library.

> --
> You are member of the "bottlepy" group at google groups.
> See http://groups.google.de/group/bottlepy for mailing list options.
> See http://bottlepy.org/ for news and documentation.
>

Branko Vukelic

unread,
Jun 11, 2011, 7:47:50 AM6/11/11
to bott...@googlegroups.com
I'll try to dig up the files later, and give you some pastes.

Branko Vukelic

unread,
Jun 11, 2011, 7:50:05 AM6/11/11
to bott...@googlegroups.com
Can't find the code, but I remember this blog post:

http://www.brankovukelic.com/post/2743988069/running-web-apps-as-linux-system-service

On Sat, Jun 11, 2011 at 1:47 PM, Branko Vukelic

Lee Connell

unread,
Jun 11, 2011, 10:16:43 AM6/11/11
to bottlepy
Thanks for the response, no matter what I do I still can't get it to
redirect output to a log. I've tried: 'nohup python pyrest.py >
server.log &' and it doesn't redirect, if I do that command without
backgrounding it, it works fine. I read your article, I know running
as root is obviously a security issue, I'm just testing this locally
for now, but running as root would cause it not to redirect? I'm
missing something here!

On Jun 11, 7:50 am, Branko Vukelic <stu...@brankovukelic.com> wrote:
> Can't find the code, but I remember this blog post:
>
> http://www.brankovukelic.com/post/2743988069/running-web-apps-as-linu...
>
> On Sat, Jun 11, 2011 at 1:47 PM, Branko Vukelic
>
>
>
>
>
>
>
> <stu...@brankovukelic.com> wrote:
> > I'll try to dig up the files later, and give you some pastes.
>
> > On Sat, Jun 11, 2011 at 1:46 PM, Branko Vukelic
> > <stu...@brankovukelic.com> wrote:
> >> Looks like an Arch Linux system.
>
> >> I don't have the code on my right now, but I remember it involved
> >> using Python's standard libraries to set the UID and GID of the
> >> running app (so it doesn't run as root), writing out the .pid file so
> >> I can use kill to stop the app, and maybe a few more things.
>
> >>http://docs.python.org/library/os.html#os.setuid
> >>http://docs.python.org/library/os.html#os.setgid
> >>http://docs.python.org/library/os.html#os.getpid
>
> >> The .pid is written out to a known location, so the daemon script can
> >> use it to kill the process.
>
> >> You also want to redirect sys.stdout to a file. To background the
> >> process, I used nohup. It's much easier than using a python library.
>

Branko Vukelic

unread,
Jun 11, 2011, 10:31:37 AM6/11/11
to bott...@googlegroups.com
For me, nohup has been reliable on Arch Linux on which I used to
deploy. Nowadays I use GAE so it doesn't matter. Another way to
redirect stdout is something like:

branko@megafox:~$ python
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> f = open('test.log', 'w')
>>> sys.stdout = f
>>> print "bogus"
>>> f.close()
>>>
branko@megafox:~$ cat test.log
bogus

In production, you'd handle the SIGTERM to close the file.

smallfish

unread,
Jun 11, 2011, 10:37:03 AM6/11/11
to bott...@googlegroups.com
hello, you can try to use supervisor tools. http://supervisord.org

use it to manage serivce start/stop/status etc..

Lee Connell

unread,
Jun 11, 2011, 10:42:16 AM6/11/11
to bottlepy
Figured it out! I knew it had to be something easy. Python buffers
its output when running as a background process and requires it to be
flushed. So to achieve the results I am looking for, I have to run
the command 'python -u myscript.py >> server.log &', using the -u
switch for unbuffered

On Jun 11, 7:50 am, Branko Vukelic <stu...@brankovukelic.com> wrote:
> Can't find the code, but I remember this blog post:
>
> http://www.brankovukelic.com/post/2743988069/running-web-apps-as-linu...
>
> On Sat, Jun 11, 2011 at 1:47 PM, Branko Vukelic
>
>
>
>
>
>
>
> <stu...@brankovukelic.com> wrote:
> > I'll try to dig up the files later, and give you some pastes.
>
> > On Sat, Jun 11, 2011 at 1:46 PM, Branko Vukelic
> > <stu...@brankovukelic.com> wrote:
> >> Looks like an Arch Linux system.
>
> >> I don't have the code on my right now, but I remember it involved
> >> using Python's standard libraries to set the UID and GID of the
> >> running app (so it doesn't run as root), writing out the .pid file so
> >> I can use kill to stop the app, and maybe a few more things.
>
> >>http://docs.python.org/library/os.html#os.setuid
> >>http://docs.python.org/library/os.html#os.setgid
> >>http://docs.python.org/library/os.html#os.getpid
>
> >> The .pid is written out to a known location, so the daemon script can
> >> use it to kill the process.
>
> >> You also want to redirect sys.stdout to a file. To background the
> >> process, I used nohup. It's much easier than using a python library.
>

Branko Vukelic

unread,
Jun 11, 2011, 1:26:30 PM6/11/11
to bott...@googlegroups.com
Nice find. Wasn't aware of that.

Lee Connell

unread,
Jun 11, 2011, 2:41:51 PM6/11/11
to bottlepy
@smallfish: I seen supervisor too, I was going to end up using that if
I couldn't figure this out, may look into it in the future. thanks.

@branko: I haven't used GAE yet, how do you like it? I'm going to look
at it and see what it offers. I'm assuming a specific bandwidth and
disk space is provided? When I was going to look into it in the past,
one problem arose. What if you wanted to send files securely to the
client? i.e: You can authenticate your users in your python app and
then use x-sendfile header to have the web server handle the sending
of files instead of your python app. How do you accomplish this in
GAE? If you can't accomplish this in this way, how do you go about
this?
> >> >>> Seehttp://bottlepy.org/fornews and documentation.

Branko Vukelic

unread,
Jun 11, 2011, 2:54:58 PM6/11/11
to bott...@googlegroups.com
On Sat, Jun 11, 2011 at 8:41 PM, Lee Connell <lee.a....@gmail.com> wrote:
> @branko: I haven't used GAE yet, how do you like it? I'm going to look
> at it and see what it offers.  I'm assuming a specific bandwidth and
> disk space is provided? When I was going to look into it in the past,
> one problem arose. What if you wanted to send files securely to the
> client? i.e: You can authenticate your users in your python app and
> then use x-sendfile header to have the web server handle the sending
> of files instead of your python app. How do you accomplish this in
> GAE? If you can't accomplish this in this way, how do you go about
> this?

I'm not sure how x-sendfile works. At any rate, GAE doesn't have
static file storage other than for your application files. It has a
blobstore which is a datastore-based file storage. I haven't used
those yet.

For normal web apps that do not deal with files, it's ok. datastore is
very difficult to work with, and requires much more work in the DB
schema design phase, as opposed to SQL (no joins, etc). Main advantage
of GAE, though, is it's sheer power and ease of deployment. The
platform also offers good tools, and so far, I haven't met much
difficulty.

I didn't choose to use GAE, though. It was more or less requested by
the employer. I kinda thought it might be fun, so I said ok. I guess
it's not bad, overall. At least I don't have to fiddle with the kind
of stuff discussed in this thread. :D

--
Branko

Reply all
Reply to author
Forward
0 new messages