Month: July 2019

Execution a command as detached process

Posted on Updated on

run_deamon() {
  local dir=$1
  shift
  local cmd=$*
  (
    cd $dir
    setsid $cmd &
    disown
  ) >/dev/null 2>/dev/null </dev/null &
  disown
}

Usage:

run_deamon ~/bin xx.sh

The command will be detached :

  • sdtin/sdtout/stderror are redirected, so process is detach of any tty
  • processus will be child if init (leader session, by setsid)
  • His default directory is in argument (should be ‘/’ for a real daemon)
  • signal to/from parent script are disable ( disown )

All this properties are only dependency of bash, non need of external resources