How do I daemonize an arbitrary script in unix?

后端 未结 12 964
陌清茗
陌清茗 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条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-30 17:16

    I apologise for the long answer (please see comments about how my answer nails the spec). I'm trying to be comprehensive, so you have as good of a leg up as possible. :-)

    If you are able to install programs (have root access), and are willing to do one-time legwork to set up your script for daemon execution (i.e., more involved than simply specifying the command-line arguments to run on the command line, but only needing to be done once per service), I have a way that's more robust.

    It involves using daemontools. The rest of the post describes how to set up services using daemontools.

    Initial setup

    1. Follow the instructions in How to install daemontools. Some distributions (e.g., Debian, Ubuntu) already have packages for it, so just use that.
    2. Make a directory called /service. The installer should have already done this, but just verify, or if installing manually. If you dislike this location, you can change it in your svscanboot script, although most daemontools users are used to using /service and will get confused if you don't use it.
    3. If you're using Ubuntu or another distro that doesn't use standard init (i.e., doesn't use /etc/inittab), you will need to use the pre-installed inittab as a base for arranging svscanboot to be called by init. It's not hard, but you need to know how to configure the init that your OS uses. svscanboot is a script that calls svscan, which does the main work of looking for services; it's called from init so init will arrange to restart it if it dies for any reason.

    Per-service setup

    1. Each service needs a service directory, which stores housekeeping information about the service. You can also make a location to house these service directories so they're all in one place; usually I use /var/lib/svscan, but any new location will be fine.
    2. I usually use a script to set up the service directory, to save lots of manual repetitive work. e.g.,

      sudo mkservice -d /var/lib/svscan/some-service-name -l -u user -L loguser "command line here"
      

      where some-service-name is the name you want to give your service, user is the user to run that service as, and loguser is the user to run the logger as. (Logging is explained in just a little bit.)

    3. Your service has to run in the foreground. If your program backgrounds by default, but has an option to disable that, then do so. If your program backgrounds without a way to disable it, read up on fghack, although this comes at a trade-off: you can no longer control the program using svc.
    4. Edit the run script to ensure it's doing what you want it to. You may need to place a sleep call at the top, if you expect your service to exit frequently.
    5. When everything is set up right, create a symlink in /service pointing to your service directory. (Don't put service directories directly within /service; it makes it harder to remove the service from svscan's watch.)

    Logging

    1. The daemontools way of logging is to have the service write log messages to standard output (or standard error, if you're using scripts generated with mkservice); svscan takes care of sending log messages to the logging service.
    2. The logging service takes the log messages from standard input. The logging service script generated by mkservice will create auto-rotated, timestamped log files in the log/main directory. The current log file is called current.
    3. The logging service can be started and stopped independently of the main service.
    4. Piping the log files through tai64nlocal will translate the timestamps into a human-readable format. (TAI64N is a 64-bit atomic timestamp with a nanosecond count.)

    Controlling services

    1. Use svstat to get the status of a service. Note that the logging service is independent, and has its own status.
    2. You control your service (start, stop, restart, etc.) using svc. For example, to restart your service, use svc -t /service/some-service-name; -t means "send SIGTERM".
    3. Other signals available include -h (SIGHUP), -a (SIGALRM), -1 (SIGUSR1), -2 (SIGUSR2), and -k (SIGKILL).
    4. To down the service, use -d. You can also prevent a service from automatically starting at bootup by creating a file named down in the service directory.
    5. To start the service, use -u. This is not necessary unless you've downed it previously (or set it up not to auto-start).
    6. To ask the supervisor to exit, use -x; usually used with -d to terminate the service as well. This is the usual way to allow a service to be removed, but you have to unlink the service from /service first, or else svscan will restart the supervisor. Also, if you created your service with a logging service (mkservice -l), remember to also exit the logging supervisor (e.g., svc -dx /var/lib/svscan/some-service-name/log) before removing the service directory.

    Summary

    Pros:

    1. daemontools provides a bulletproof way to create and manage services. I use it for my servers, and I highly recommend it.
    2. Its logging system is very robust, as is the service auto-restart facility.
    3. Because it starts services with a shell script that you write/tune, you can tailor your service however you like.
    4. Powerful service control tools: you can send most any signal to a service, and can bring services up and down reliably.
    5. Your services are guaranteed a clean execution environment: they will execute with the same environment, process limits, etc., as what init provides.

    Cons:

    1. Each service takes a bit of setup. Thankfully, this only needs doing once per service.
    2. Services must be set up to run in the foreground. Also, for best results, they should be set up to log to standard output/standard error, rather than syslog or other files.
    3. Steep learning curve if you're new to the daemontools way of doing things. You have to restart services using svc, and cannot run the run scripts directly (since they would then not be under the control of the supervisor).
    4. Lots of housekeeping files, and lots of housekeeping processes. Each service needs its own service directory, and each service uses one supervisor process to auto-restart the service if it dies. (If you have many services, you will see lots of supervise processes in your process table.)

    In balance, I think daemontools is an excellent system for your needs. I welcome any questions about how to set it up and maintain it.

提交回复
热议问题