Start-stop-daemon is a tiny but useful stock Linux util by Debian and Ubuntu. You can turn almost every executable into background daemon.

Usage: start-stop-daemon [<option> ...] <command>

Commands:
  -S|--start -- <argument> ...  start a program and pass <arguments> to it
  -K|--stop                     stop a program
  -T|--status                   get the program status
  -H|--help                     print help information
  -V|--version                  print version

Matching options (at least one is required):
  -p|--pidfile <pid-file>       pid file to check
  -x|--exec <executable>        program to start/check if it is running
  -n|--name <process-name>      process name to check
  -u|--user <username|uid>      process owner to check

Options:
  -g|--group <group|gid>        run process as this group
  -c|--chuid <name|uid[:group|gid]>
                                change to this user/group before starting
                                  process
  -s|--signal <signal>          signal to send (default TERM)
  -a|--startas <pathname>       program to start (default is <executable>)
  -r|--chroot <directory>       chroot to <directory> before starting
  -d|--chdir <directory>        change to <directory> (default is /)
  -N|--nicelevel <incr>         add incr to the process' nice level
  -P|--procsched <policy[:prio]>
                                use <policy> with <prio> for the kernel
                                  process scheduler (default prio is 0)
  -I|--iosched <class[:prio]>   use <class> with <prio> to set the IO
                                  scheduler (default prio is 4)
  -k|--umask <mask>             change the umask to <mask> before starting
  -b|--background               force the process to detach
  -C|--no-close                 do not close any file descriptor
  -m|--make-pidfile             create the pidfile before starting
  -R|--retry <schedule>         check whether processes die, and retry
  -t|--test                     test mode, don't do anything
  -o|--oknodo                   exit status 0 (not 1) if nothing done
  -q|--quiet                    be more quiet
  -v|--verbose                  be more verbose

Retry <schedule> is <item>|/<item>/... where <item> is one of
 -<signal-num>|[-]<signal-name>  send that signal
 <timeout>                       wait that many seconds
 forever                         repeat remainder forever
or <schedule> may be just <timeout>, meaning <signal>/<timeout>/KILL/<timeout>

The process scheduler <policy> can be one of:
  other, fifo or rr

The IO scheduler <class> can be one of:
  real-time, best-effort or idle

Exit status:
  0 = done
  1 = nothing done (=> 0 if --oknodo)
  2 = with --retry, processes would not die
  3 = trouble
Exit status with --status:
  0 = program is running
  1 = program is not running and the pid file exists
  3 = program is not running
  4 = unable to determine status

Here are some important parameters:

  • -b --background, it is especially useful for those processes that not designed to run as a daemon, it will force the program detach and work in the background.
  • -m together with -p, will create a pid file for the target process. Useful if the target process doesn't support writing its pid to a file.
  • -u, run the process as a specific user.

Demo time:

#!/bin/sh

# Quick start-stop-daemon example, derived from Debian /etc/init.d/ssh
set -e

# Must be a valid filename
NAME=foo
PIDFILE=/var/run/$NAME.pid
#This is the command to be run, give the full pathname
DAEMON=/usr/local/bin/bar
DAEMON_OPTS="--baz=quux"

export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"

case "$1" in
  start)
        echo -n "Starting daemon: "$NAME
    start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_OPTS
        echo "."
    ;;
  stop)
        echo -n "Stopping daemon: "$NAME
    start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE
        echo "."
    ;;
  status)
        if ps -p `cat $PIDFILE` > /dev/null
        then
           echo "$NAMEis running"
           # Do something knowing the pid exists, i.e. the process with $PID is running
        fi
  restart)
        echo -n "Restarting daemon: "$NAME
    start-stop-daemon --stop --quiet --oknodo --retry 30 --pidfile $PIDFILE
    start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_OPTS
    echo "."
    ;;

  *)
    echo "Usage: "$1" {start|stop|status|restart}"
    exit 1
esac

exit 0

Here I introduced a way to determine if the PID is running suggested by Stackoverflow, you can replace it with better ways you know.