Skip to content
This repository

mon(1) - Simple single-process process monitoring program written in C

tree: 4b461012b5

Fetching latest commit…


Cannot retrieve the latest commit at this time

Octocat-spinner-32 deps
Octocat-spinner-32 example
Octocat-spinner-32 src
Octocat-spinner-32 .gitignore Initial commit
Octocat-spinner-32 Makefile


Super-simple monitoring program.

Effectively all it does is daemonize some programs, re-executing the program on failure, write pidfiles, and provide status checks.


$ make install


Usage: mon [options] <command>


  -V, --version                 output program version
  -h, --help                    output help information
  -l, --log <path>              specify logfile [mon.log]
  -s, --sleep <sec>             sleep seconds before re-executing [1]
  -S, --status                  check status of --pidfile
  -p, --pidfile <path>          write pid to <path>
  -m, --mon-pidfile <path>      write mon(1) pid to <path>
  -P, --prefix <str>            add a log prefix
  -d, --daemonize               daemonize the program
  -a, --attempts <n>            retry attempts within 60 seconds [10]
  -R, --on-restart <cmd>        execute <cmd> on restarts
  -E, --on-error <cmd>          execute <cmd> on error


The most simple use of mon(1) is to simply keep a command running:

$ mon ./myprogram
mon : pid 50395
mon : child 50396
mon : sh -c "./example/"

You may daemonize mon and disassociate from the term with -d:

$ mon ./myprogram -d
mon : pid 50413

Failure alerts

mon(1) will continue to attempt restarting your program unless the maximum number of --attempts has been exceeded within 60 seconds. Each time a restart is performed the --on-restart command is executed, and when mon(1) finally bails the --on-error command is then executed before mon itself exits and gives up.

For example the following will echo "hey" three times before mon realizes that the program is unstable, since it's exiting immediately, thus finally invoking ./, or any other script you like.

mon "echo hey" --attempts 3 --on-error ./
mon : child 48386
mon : sh -c "echo hey"
mon : last restart less than one second ago
mon : 3 attempts remaining
mon : child 48387
mon : sh -c "echo hey"
mon : last restart less than one second ago
mon : 2 attempts remaining
mon : child 48388
mon : sh -c "echo hey"
mon : last restart less than one second ago
mon : 1 attempts remaining
mon : 3 restarts within less than one second, bailing
mon : on error `sh`
emailed failure notice to
mon : bye :)

Groups of mon

mon(1) is designed to monitor a single program only, this means a few things, firstly that a single mon(1) may crash and it will not influence other programs, secondly that the "configuration" for mon(1) is simply a shell script, no need for funky weird inflexible DSLs.

#!/usr/bin/env bash


mon -d redis-server -p $pids/
mon -d "node $app/app" -p $pids/
mon -d "node $app/jobs" -p $pids/
mon -d "node $app/jobs" -p $pids/
mon -d "node $app/jobs" -p $pids/
mon -d "node $app/image" -p $pids/
mon -d "node $app/image" -p $pids/
mon -d "node $app/image-broker" -p $pids/

Restarting processes

Restarting a process is simple, kill(1) it. For example if your app is written to gracefully exit on SIGQUIT you might want to do:

$ kill -s SIGQUIT $(cat /var/run/

Or if you just want things done quick and dirty:

$ kill -s SIGKILL $(cat /var/run/

mon(1) will see that the child died, and re-execute the initial start command.


Tools built with mon(1):

Something went wrong with that request. Please try again.