Find file
Fetching contributors…
Cannot retrieve contributors at this time
165 lines (118 sloc) 5.82 KB

Managing external daemons/services

uWSGI can easily monitor external processes, allowing you to increase reliability and usability of your multi-tier apps. For example you can manage services like Memcached, Redis, Celery, Ruby delayed_job or even dedicated PostgreSQL instances.

Kinds of services

Currently uWSGI supports 3 categories of processes:

  • --attach-daemon -- directly attached non daemonized processes
  • --smart-attach-daemon -- pidfile governed (both foreground and daemonized)
  • --smart-attach-daemon2 -- pidfile governed with daemonization management

The first category allows you to directly attach processes to the uWSGI master. When the master dies or is reloaded these processes are destroyed. This is the best choice for services that must be flushed whenever the app is restarted.

Pidfile governed processes can survive death or reload of the master so long as their pidfiles are available and the pid contained therein matches a running pid. This is the best choice for processes requiring longer persistence, and for which a brutal kill could mean loss of data such as a database.

The last category is a superset of the second one. If your process does not support daemonization or writing to pidfile, you can let the master do the management. Very few daemons/applications require this feature, but it could be useful for tiny prototype applications or simply poorly designed ones.

Since uWSGI 2.0 a fourth option, --attach-daemon2 has been added for advanced configurations (see below).


Managing a memcached instance in 'dumb' mode. Whenever uWSGI is stopped or reloaded, memcached is destroyed.

master = true
socket = :3031
attach-daemon = memcached -p 11311 -u roberto

Managing a memcached instance in 'smart' mode. Memcached survives uWSGI stop and reload.

master = true
socket = :3031
smart-attach-daemon = /tmp/ memcached -p 11311 -d -P /tmp/ -u roberto

Managing 2 mongodb instances in smart mode:

master = true
socket = :3031
smart-attach-daemon = /tmp/ mongod --pidfilepath /tmp/ --dbpath foo1 --port 50001
smart-attach-daemon = /tmp/ mongod --pidfilepath /tmp/ --dbpath foo2 --port 50002

Managing PostgreSQL dedicated-instance (cluster in /db/foobar1):

master = true
socket = :3031
smart-attach-daemon = /db/foobar1/ /usr/lib/postgresql/9.1/bin/postgres -D /db/foobar1

Managing celery:

master = true
socket = :3031
smart-attach-daemon = /tmp/ celery -A tasks worker --pidfile=/tmp/

Managing delayed_job:

master = true
socket = :3031
env = RAILS_ENV=production
rbrequire = bundler/setup
rack =
chdir = /var/apps/foobar
smart-attach-daemon = %(chdir)/tmp/pids/ %(chdir)/script/delayed_job start

Managing dropbear:

namespace = /ns/001/:testns
namespace-keep-mount = /dev/pts
socket = :3031
exec-as-root = chown -R www-data /etc/dropbear
attach-daemon = /usr/sbin/dropbear -j -k -p 1022 -E -F -I 300

When using the namespace option you can attach a dropbear daemon to allow direct access to the system inside the specified namespace. This requires the /dev/pts filesystem to be mounted inside the namespace, and the user your workers will be running as have access to the /etc/dropbear directory inside the namespace.

Legion support

Starting with uWSGI 1.9.9 it's possible to use the :doc:`Legion` subsystem for daemon management. Legion daemons will be executed only on the legion lord node, so there will always be a single daemon instance running in each legion. Once the lord dies a daemon will be spawned on another node. To add a legion daemon use --legion-attach-daemon, --legion-smart-attach-daemon and --legion-smart-attach-daemon2 options, they have the same syntax as normal daemon options. The difference is the need to add legion name as first argument.


Managing celery beat:

master = true
socket = :3031
legion-mcast = mylegion 90 bf-cbc:mysecret
legion-smart-attach-daemon = mylegion /tmp/ celery beat --pidfile=/tmp/


This option has been added in uWSGI 2.0 and allows advanced configurations. It is a keyval option, and it accepts the following keys:

  • command/cmd/exec: the command line to execute
  • freq: maximum attempts before considering a daemon "broken"
  • pidfile: the pidfile to check (enable smart mode)
  • control: if set, the daemon becomes a 'control' one: if it dies the whole uWSGI instance dies
  • daemonize/daemon: daemonize the process (enable smart2 mode)
  • touch semicolon separated list of files to check: whenever they are 'touched', the daemon is restarted
  • stopsignal/stop_signal: the signal number to send to the daemon when uWSGI is stopped
  • reloadsignal/reload_signal: the signal to send to the daemon when uWSGI is reloaded
  • stdin: if set the file descriptor zero is not remapped to /dev/null
  • uid: drop privileges to the specified uid (requires master running as root)
  • gid: drop privileges to the specified gid (requires master running as root)
  • ns_pid: spawn the process in a new pid namespace (requires master running as root, Linux only)
  • chdir: chdir() to the specified directory before running the command (added in uWSGI 2.0.6)


attach-daemon2 =,pidfile=/tmp/,uid=33,gid=33,stopsignal=3