How do I daemonize an arbitrary script in unix?

后端 未结 12 955
陌清茗
陌清茗 2020-11-30 16:33

I\'d like a daemonizer that can turn an arbitrary, generic script or command into a daemon.

There are two common cases I\'d like to deal with:

  1. I hav

12条回答
  •  萌比男神i
    2020-11-30 17:13

    You should have a look at daemonize. It allows to detect second copy (but it uses file locking mechanism). Also it works on different UNIX and Linux distributions.

    If you need to automatically start your application as daemon, then you need to create appropriate init-script.

    You can use the following template:

    #!/bin/sh
    #
    # mydaemon     This shell script takes care of starting and stopping
    #               the 
    #
    
    # Source function library
    . /etc/rc.d/init.d/functions
    
    
    # Do preliminary checks here, if any
    #### START of preliminary checks #########
    
    
    ##### END of preliminary checks #######
    
    
    # Handle manual control parameters like start, stop, status, restart, etc.
    
    case "$1" in
      start)
        # Start daemons.
    
        echo -n $"Starting  daemon: "
        echo
        daemon 
        echo
        ;;
    
      stop)
        # Stop daemons.
        echo -n $"Shutting down : "
        killproc 
        echo
    
        # Do clean-up works here like removing pid files from /var/run, etc.
        ;;
      status)
        status 
    
        ;;
      restart)
        $0 stop
        $0 start
        ;;
    
      *)
        echo $"Usage: $0 {start|stop|status|restart}"
        exit 1
    esac
    
    exit 0
    

提交回复
热议问题