Coder Social home page Coder Social logo

procdog's Introduction

procdog

Lightweight command-line process control

Procdog (as in "process watchdog"... get it?) is a simple command-line tool to start, stop, and check the health of processes. It works with any kind of process you can invoke from the command line (be it native, Java, Node, Python, or anything) on MacOS or Linux.

Why would you want another tool for this?

  • For basic interactive situations, you can just run a process in a terminal or use job management in your shell (jobs, kill, etc.).
  • But you rapidly realize this won't work well once you have longer-lived processes or are sshing to remote servers. Then you could use nohup and manually checking with ps, or using screen. But these don't script easily.
  • You also might want to script starting and stopping. With a little effort you might write a custom Bash script (writing a PID file, using pgrep and pkill, etc.), but it's a hassle and gets messy quickly.
  • Of course, you can just "do it right." Traditionally, in the Unix world, the way to control services are System V service scripts, start-stop-daemon, Upstart, or systemd. Using these is essential for production deployment, but they tend to be a bit arcane and highly OS-dependent, so aren't as convenient for casual use, and you can't easily develop and test on both MacOS and Linux (as many of us try to do).

Procdog is an alternative for developers that attempts to be easy to install, simple and obvious to use, and cross-platform. Processes are independent of the shell used to invoke them (i.e., detatched as with nohup) and you can also check status or kill them at any time (as with a Unix service).

Probably the most simlar tool to Procdog is Supervisor. Although it has similar goals, Supervisor is a more complex, production-oriented tool with a long-lived, centrailized XML-RPC server that monitors processes. Procdog is intended to be a single, simple, easy-to-use command that needn't be set up and managed itself or run as root. Each use of Procdog is independent and monitors just one process. It does not require a configuration file unless you want to save typing. You can even check the Procdog script into your own project so developers can immediately use it locally, since it has no dependencies (besides Python 2.7).

You'll find it useful if you have servers, databases, or other processes you want to manage on your personal machine when developing, in build systems and test harnesses, test deployments, etc. Currently, it doesn't have restart logic, log file rotation, or some other features you may want for a production environments; for this consider Supervisor, Upstart, systemd, & co. Procdog is also way less mature than these alternatives.

Installation

No dependencies except Python 2.7+. It's easiest to install with pip:

sudo pip install procdog

Or, since it's just one file, you can copy the single procdog script somewhere (perhaps within your own project) and make it executable.

Quick start

Now you can start and monitor any process (here let's pick "sleep" -- not all that useful, but you already have it):

$ procdog start myprocess --command "sleep 100"
running, pid=14969
$ procdog status myprocess
running, pid=14969
$ procdog stop myprocess
stopped
$

Note you have to give your process an arbitrary name (myprocess here), like a Unix service name, so you can refer to it. Once the process is done, the monitor daemon also exits.

Usage

Run procdog -h for help on all options.

A better example

Now, with a real server we'd like to know if it's actually up and doing something, like listening on a port. Procdog supports an arbitrary command to test health (e.g. running curl to see if it returns a result) and can wait until a server is running and tell you.

$ procdog start backend --command="java -classpath my-backend.jar com.example.BackendServer server config.yml" \
  --health-command="curl http://localhost:8080/ping" \
  --health-count=10 --health-delay=2 \
  --dir=$HOME/backend \
  --stdout=backend.log --stderr=backend.log --append \
  --ensure-healthy --strict
running, health=0, pid=15240
$ procdog status backend
running, health=0, pid=15240
$ procdog stop backend --strict
stopped
$ procdog stop backend --strict
procdog: error: process 'backend' is not running
$

Some notes on this:

  • We can specify where to write stdout and stderr. They can be the same or different. Existing log files are appended to if you use --append.
  • The health command simply calls a shell command to see if the server is healthy. The return code of the health check command must be 0 for the server to be considered healthy. In this case, we're callin curl on a known health-check URL, which will have return code 0 on an HTTP 200
  • The --ensure-healthy option means the command will block until the process is healthy, or until the daemon gives up and kills the process (if necessary). In this example, it will try 10 times, sleeping 2 seconds each time, before giving up.
  • The --dir option means process will run from that directory.
  • We ask the client to be --strict, so that it returns non-zero status code when we try to start a process that's already running or or stop one that is already stopped.

Configuration files

It's possible to avoid typing by putting most options in a configuration file:

# Procdog config file. Each section is a process name.
[backend]
command=java -classpath my-backend.jar com.example.BackendServer server config.yml
health_command=curl http://localhost:8080/ping
health_count=10
health_delay=2.
dir=$HOME/backend
stdout=backend.log
stderr=backend.log
append=False
ensure_healthy=True
strict=True

You have any number of sections, one section per process. Procdog reads options from ~/.procdog.cfg or procdog.cfg (in the same directory the procdog script resides). Any options given on the command line override those in the configuration file. Note that environment variables (like $HOME) are allowed and expanded. Once you have the above section in your config file, you can run:

$ procdog start backend
running, health=0, pid=15396
$ procdog stop backend
stopped
$

How it works

Procdog starts a small daemon that popen()s and monitors the process. The daemon listens and accepts commands on a local Unix domain socket, making it possible to check the process is running or terminate it, and to do simple health checks. You can see these sockets at /var/tmp/procdog.*.sock (where the * is the id of the process). For simplicity, there is a single Procdog daemon for each monitored process, so each process is handled compeltely separately.

We use Unix domain sockets so that we don't have the headaches of pid files or choosing and binding to TCP ports. They're also available on most platforms.

Daemon logs are sent to /var/tmp/procdog.*.log. Usually you'll want to redirect your process stdout and stderr using the --stdout and --stderr options.

Procdog is quite new so probably not stable. Bug reports and contributions are welcome!

Tests

All basic features are covered with a simple Bash-based regression test.

To run it, clone this repo, invoke tests/run.sh then follow the directions to diff the output using git.

License

Apache 2.

procdog's People

Contributors

jlevy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

procdog's Issues

issuse installing procdog on osx using python 3

$ pip -V
pip 7.1.2 from /Users/t.hensel-extern/.pyenv/versions/3.4.3/lib/python3.4/site-packages (python 3.4)
$ pip install procdog
Collecting procdog
  Using cached procdog-0.1.13.tar.gz
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 20, in <module>
      File "/private/var/folders/79/mskk04vd3wn4bglf0c7h1s7rx6l6_8/T/pip-build-ienmj8nt/procdog/setup.py", line 6, in <module>
        procdog = imp.load_source("procdog", "procdog")
      File "/Users/t.hensel-extern/.pyenv/versions/3.4.3/lib/python3.4/imp.py", line 171, in load_source
        module = methods.load()
      File "<frozen importlib._bootstrap>", line 1220, in load
      File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
      File "<frozen importlib._bootstrap>", line 1129, in _exec
      File "<frozen importlib._bootstrap>", line 1467, in exec_module
      File "<frozen importlib._bootstrap>", line 1572, in get_code
      File "<frozen importlib._bootstrap>", line 1532, in source_to_code
      File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
      File "procdog", line 400
        def daemonize(home_dir="/", umask=077, do_redirect=True, stdout=None, stderr=None):
                                            ^
    SyntaxError: invalid token

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/79/mskk04vd3wn4bglf0c7h1s7rx6l6_8/T/pip-build-ienmj8nt/procdog

guess good'ol daemonize went incompatible or something... regards ❤️

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.