Snippet: #g8jv

Written in Plain Text and posted on Nov 22, 2011 at 18:08
   1  daemon() {
   2      # Test syntax.
   3      local gotbase= force= nicelevel corelimit
   4      local pid base= user= nice= bg= pid_file=
   5      local cgroup=
   6      nicelevel=0
   7      while [ "$1" != "${1##[-+]}" ]; do
   8        case $1 in
   9          '')    echo $"$0: Usage: daemon [+/-nicelevel] {program}"
  10                 return 1;;
  11          --check)
  12             base=$2
  13             gotbase="yes"
  14             shift 2
  15             ;;
  16          --check=?*)
  17                 base=${1#--check=}
  18             gotbase="yes"
  19             shift
  20             ;;
  21          --user)
  22             user=$2
  23             shift 2
  24             ;;
  25          --user=?*)
  26                 user=${1#--user=}
  27             shift
  28             ;;
  29          --pidfile)
  30             pid_file=$2
  31             shift 2
  32             ;;
  33          --pidfile=?*)
  34             pid_file=${1#--pidfile=}
  35             shift
  36             ;;
  37          --force)
  38                 force="force"
  39             shift
  40             ;;
  41          [-+][0-9]*)
  42                 nice="nice -n $1"
  43                 shift
  44             ;;
  45          *)     echo $"$0: Usage: daemon [+/-nicelevel] {program}"
  46                 return 1;;
  47        esac
  48      done
  49  
  50          # Save basename.
  51          [ -z "$gotbase" ] && base=${1##*/}
  52  
  53          # See if it's already running. Look *only* at the pid file.
  54      __pids_var_run "$base" "$pid_file"
  55  
  56      [ -n "$pid" -a -z "$force" ] && return
  57  
  58      # make sure it doesn't core dump anywhere unless requested
  59      corelimit="ulimit -S -c ${DAEMON_COREFILE_LIMIT:-0}"
  60  
  61      # if they set NICELEVEL in /etc/sysconfig/foo, honor it
  62      [ -n "${NICELEVEL:-}" ] && nice="nice -n $NICELEVEL"
  63  
  64      # if they set CGROUP_DAEMON in /etc/sysconfig/foo, honor it
  65      if [ -n "${CGROUP_DAEMON}" ]; then
  66          if [ ! -x /bin/cgexec ]; then
  67              echo -n "Cgroups not installed"; warning
  68              echo
  69          else
  70              cgroup="/bin/cgexec";
  71              for i in $CGROUP_DAEMON; do
  72                  cgroup="$cgroup -g $i";
  73              done
  74          fi
  75      fi
  76  
  77      # Echo daemon
  78          [ "${BOOTUP:-}" = "verbose" -a -z "${LSB:-}" ] && echo -n " $base"
  79  
  80      # And start it up.
  81      if [ -z "$user" ]; then
  82         $cgroup $nice /bin/bash -c "$corelimit >/dev/null 2>&1 ; $*"
  83      else
  84         $cgroup $nice runuser -s /bin/bash $user -c "$corelimit >/dev/null 2>&1 ; $*"
  85      fi
  86  
  87      [ "$?" -eq 0 ] && success $"$base startup" || failure $"$base startup"
  88  }