caretakerd

Version: 0.1.7

caretakerd is build for to be a minimal process supervisor, with no dependencies, optimized for containerization (such as Docker) and simple to configure.

Topics

Features

Getting started

Installing

Download your matching distribution files and extract it where you want.

Linux 64-Bit example:

sudo curl -SL https://github.com/echocat/caretakerd/releases/download/v0.1.7/caretakerd-linux-amd64.tar.gz \
    | tar -xz --exclude caretakerd.html -C /usr/bin

Hint: You can use exact these command to in a Dockerfile to create caretakerd in a docker container.

See Downloads for all possible download locations.

Configuring

Place your configuration file at your favorite location. Default location is:

See Configuration examples for a quick start or consult Configuration structure for all possibilities.

Run

caretakerd run

Control caretakerd with caretakerctl

Precondition: RPC is enabled. See RPC enabled configuration example how to done this.

## Start a not already running service
$ caretakerctl start peasant

## Retrieve status of a service
$ caretakerctl status peasant

## Stop a not already running service
$ caretakerctl stop peasant

Downloads

Commands

The way how the binary reacts depends on the command/file name it was called. A simple rename or symlink will change the behavior of the binary.

Pattern Target command Examples
*/caretakerd* caretakerd /usr/bin/caretakerd
/usr/bin/caretakerd-linux-amd64
C:\Program Files\caretakerd\caretakerd-windows-amd64.exe
*/caretakerctl* caretakerctl /usr/bin/caretakerctl
/usr/bin/caretakerctl-linux-amd64
C:\Program Files\caretakerd\caretakerctl-windows-amd64.exe
Everything else that does
not matches one of above.
caretaker /usr/bin/caretaker
/usr/bin/caretaker-linux-amd64
C:\Program Files\caretakerd\caretaker-windows-amd64.exe

caretakerd

NAME:
   caretakerd - Simple control daemon for processes.

USAGE:
   caretakerd [global options] command ...

COMMANDS:
   run	Run caretakerd in forground.
   
GLOBAL OPTIONS:
   --config, -c "/etc/caretakerd.yaml"		Configuration file for daemon. [$CTD_CONFIG]
   --address, -a "tcp://localhost:57955"	Listen address of the daemon.
   

caretakerctl

NAME:
   caretakerctl - Remote control for caretakerd

USAGE:
   caretakerctl [global options] command ...

COMMANDS:
   config			Query whole daemon configuration.
   controlConfig		Query control configuration.
   services			Query whole daemon configuration with all its actual service stats.
   service			Query service configuration and its actual stats.
   serviceConfig		Query service configuration.
   serviceStatus, status	Query service status.
   servicePid, pid		Query service pid.
   serviceStart, start		Start a service.
   serviceRestart, restart	Restart a service.
   serviceStop, stop		Stop a service.
   serviceKill, kill		Kill a service.
   serviceSignal, signal	Send a signal to service.
   
GLOBAL OPTIONS:
   --config, -c "/etc/caretakerd.yaml"		Configuration file for control. [$CTCTL_CONFIG]
   --address, -a "tcp://localhost:57955"	Listen address of the daemon.
   --pem, -p "/var/run/caretakerd.key"		Location of PEM file which contains the private public key pair for access to the daemon.
   

caretaker

NAME:
   caretaker - Simple control daemon for processes including remote control for itself.

USAGE:
   caretaker [global options] command ...

COMMANDS:
   daemon			Run caretakerd in forground.
   config			Query whole daemon configuration.
   controlConfig		Query control configuration.
   services			Query whole daemon configuration with all its actual service stats.
   service			Query service configuration and its actual stats.
   serviceConfig		Query service configuration.
   serviceStatus, status	Query service status.
   servicePid, pid		Query service pid.
   serviceStart, start		Start a service.
   serviceRestart, restart	Restart a service.
   serviceStop, stop		Stop a service.
   serviceKill, kill		Kill a service.
   serviceSignal, signal	Send a signal to service.
   
GLOBAL OPTIONS:
   --config, -c "/etc/caretakerd.yaml"		Configuration file for daemon and control. [$CT_CONFIG]
   --address, -a "tcp://localhost:57955"	Listen address of the daemon.
   --pem, -p "/var/run/caretakerd.key"		Location of PEM file which contains the private public key pair for access to the daemon.
   

Configuration

Examples

RPC enabled

# Run the service king at startup and enable rpc.
# peasant will only be started of "caretakerctl start peasant" is called.
# king will run for 120 seconds. If king is finished the whole careteakerd
# will go down.

rpc:
    enabled: true

services:
    king:
        type: master
        command: ["sleep", "120"]

    peasant:
        type: onDemand
        command: ["sleep","10"]

Simple

# Run the service king, queen and wonderland at startup.
# king will run for 120 seconds. If king is finished the whole careteakerd will
# go down and also terminate queen.
# wonderland will only be print "Follow the white rabbit." to console and exit
# imidately.

services:
    king:
        type: master
        command: ["sleep","120"]

    queen:
        command: ["sleep", "240"]

    wonderland:
        command: ["echo", "Follow the white rabbit."]

With cron expression

# Run the service king and queen at startup.
# queen will run every 5th second until king is finished.
# king will run for 120 seconds. If king is finished the whole careteakerd will
# go down.

services:
    king:
        type: master
        command: ["sleep","120"]

    queen:
        command: ["echo","Hello world from the queen!"]
        cronExpression: "0,5,10,15,20,25,30,35,40,45,50,55 * * * * *"

Structure

# (keyStore.KeyStore) Defines how the encryption of caretakerd works.
keyStore:
# (keyStore.Type) Defines the type of instance keyStore.
type: generated
# (string) Defines the pemFile which contains the key and certificate to use.
pemFile: ""
# (string) Defines some hints for instance store in format [<key:`value`>...].
hints: "algorithm:`rsa` bits:`1024`"
# (string) File where trusted certificates are stored in.
caFile: ""
# (rpc.Rpc) Defines how caretaker can controlled remotely.
rpc:
# (bool) If this is set to true it is possible to control caretakerd remotely.
enabled: false
# (values.SocketAddress) Address where caretakerd RPC interface is listen to.
listen: "tcp://localhost:57955"
# (control.Control) Defines the access rights of caretakerctl to caretakerd.
control:
# (access.Access) Configures the permission of caretakerctl to control caretakerd remotely and how to obtain the credentials for it.
access:
# (access.Type) Defines how this access will be ensured.
type: "generateToFile" (for control/caretakerctl) "none" (for services)
# (access.Permission) Defines what the control/service can do with caretakerd.
permission: "readWrite" (for control/caretakerctl) "forbidden" (for services)
# (string) In case of type = trusted from this file will the certificate read which should be trust and identifies the remote connection.
pemFile: ""
# (uint32) Permission in filesystem of the generated pem file.
pemFilePermission: 0600
# (string) If set this user owns the generated pem file.
pemFileUser: ""
# (logger.Logger) Configures the logger for caretakerd itself.
logger:
# (logger.Level) Minimal log level the logger will output its messages.
level: info
# (logger.Level) If the service prints something to stdout instance will logged with instance level.
stdoutLevel: info
# (logger.Level) If the service prints something to stderr instance will logged with instance level.
stderrLevel: error
# (string) Target file of the logger.
filename: "console"
# (int) Maximum size in megabytes of the log file before it gets rotated.
maxSizeInMb: 500
# (int) Maximum number of old log files to retain.
maxBackups: 500
# (int) Maximum number of days to retain old log files based on the timestamp encoded in their filename.
maxAgeInDays: 1
# (logger.Pattern) Pattern how to format the log messages to output with.
pattern: "%d{YYYY-MM-DD HH:mm:ss} [%-5.5p] [%c] %m%n%P{%m}"
# ([string]service.Service) Services configuration to run with caretakerd.
services:
<service name>:
# (service.Type) Defines how this service will run by caretakerd.
type: autoStart
# ([]string) The command the service process has to start with.
command: []
# ([][]string) Commands to be executed before execution of the actual command.
preCommands: []
# ([][]string) Commands to be executed after execution of the actual command.
postCommands: []
# (service.CronExpression) If configured this will trigger the service at this specific times.
cronExpression: ""
# (int) Wait before the service process will start the first time.
startDelayInSeconds: 0
# ([]int) Every of these values represents an expected success exit codes.
successExitCodes: [0]
# (int) Signal which will be send to the service when a stop is requested.
stopSignal: "TERM"
# (values.SignalTarget) Defines who have to receive the stop signal.
stopSignalTarget: "processGroup"
# ([]string) Command to be execute to stop the service.
stopCommand: []
# (int) Timeout to wait before kill the service process after a stop is requested.
stopWaitInSeconds: 30
# (string) User under which the service process will be started.
user: ""
# ([string]string) Environment variables to pass to the process.
environment: []
<environment name>:
# (bool) Pass the environment variables started with caretakerd also to the service process.
inheritEnvironment: true
# (string) Working directory to start the service process in.
directory: ""
# (values.RestartType) Configure how caretakerd will handle the end of a process.
autoRestart: onFailures
# (int) Seconds to wait before restart of a process.
restartDelayInSeconds: 5
# (access.Access) Configures the permission of this service to control caretakerd remotely and how to obtain the credentials for it.
access:
# (access.Type) Defines how this access will be ensured.
type: "generateToFile" (for control/caretakerctl) "none" (for services)
# (access.Permission) Defines what the control/service can do with caretakerd.
permission: "readWrite" (for control/caretakerctl) "forbidden" (for services)
# (string) In case of type = trusted from this file will the certificate read which should be trust and identifies the remote connection.
pemFile: ""
# (uint32) Permission in filesystem of the generated pem file.
pemFilePermission: 0600
# (string) If set this user owns the generated pem file.
pemFileUser: ""
# (logger.Logger) Configures the logger for this specific service.
logger:
# (logger.Level) Minimal log level the logger will output its messages.
level: info
# (logger.Level) If the service prints something to stdout instance will logged with instance level.
stdoutLevel: info
# (logger.Level) If the service prints something to stderr instance will logged with instance level.
stderrLevel: error
# (string) Target file of the logger.
filename: "console"
# (int) Maximum size in megabytes of the log file before it gets rotated.
maxSizeInMb: 500
# (int) Maximum number of old log files to retain.
maxBackups: 500
# (int) Maximum number of days to retain old log files based on the timestamp encoded in their filename.
maxAgeInDays: 1
# (logger.Pattern) Pattern how to format the log messages to output with.
pattern: "%d{YYYY-MM-DD HH:mm:ss} [%-5.5p] [%c] %m%n%P{%m}"

Environment mapping

There are several environment variables mapped to configuration parameters. There environment variables goes over configuration files.

Hint: Every caretakerd environment variable starts with CTD = CareTakerD

Examples

caretakerd.yaml

services:
    king:
        type: master
        command: ["echo", "Hello world!"]
        user: king

Executions

$ caretakerd run
Hello world!

$ export CTD.king.COMMAND=whoami
$ caretakerd run
king

$ export CTD.king.USER=root
$ caretakerd run
root

Services

Environment variable Configuration property
CTD.<service>.TYPE service.Service.type
CTD.<service>.COMMAND service.Service.command
CTD.<service>.START_DELAY_IN_SECONDS service.Service.startDelayInSeconds
CTD.<service>.RESTART_DELAY_IN_SECONDS service.Service.restartDelayInSeconds
CTD.<service>.SUCCESS_EXIT_CODES service.Service.successExitCodes
CTD.<service>.STOP_WAIT_IN_SECONDS service.Service.stopWaitInSeconds
CTD.<service>.USER service.Service.user
CTD.<service>.DIRECORY service.Service.directory
CTD.<service>.AUTO_RESTART service.Service.autoRestart
CTD.<service>.INHERIT_ENVIRONMENT service.Service.inheritEnvironment
CTD.<service>.LOG_LEVEL service.Service.logger: logger.Logger.level
CTD.<service>.LOG_STDOUT_LEVEL service.Service.logger: logger.Logger.stdoutLevel
CTD.<service>.LOG_STDERR_LEVEL service.Service.logger: logger.Logger.stderrLevel
CTD.<service>.LOG_FILE_NAME service.Service.logger: logger.Logger.filename
CTD.<service>.LOG_MAX_SIZE_IN_MB service.Service.logger: logger.Logger.maxSizeInMb
CTD.<service>.LOG_MAX_BACKUPS service.Service.logger: logger.Logger.maxBackups
CTD.<service>.LOG_MAX_AGE_IN_DAYS service.Service.logger: logger.Logger.maxAgeInDays
CTD.<service>.ENVIRONMENT.<environmentName> service.Service.environment[<environmentName>]

Global

Environment variable Configuration property
CTD.LOG_LEVEL Caretakerd.logger: logger.Logger.level
CTD.LOG_STDOUT_LEVEL Caretakerd.logger: logger.Logger.stdoutLevel
CTD.LOG_STDERR_LEVEL Caretakerd.logger: logger.Logger.stderrLevel
CTD.LOG_FILE_NAME Caretakerd.logger: logger.Logger.filename
CTD.LOG_MAX_SIZE_IN_MB Caretakerd.logger: logger.Logger.maxSizeInMb
CTD.LOG_MAX_BACKUPS Caretakerd.logger: logger.Logger.maxBackups
CTD.LOG_MAX_AGE_IN_DAYS Caretakerd.logger: logger.Logger.maxAgeInDays

Data Types

Caretakerd object

Root configuration of caretakerd.

Properties

access.Access object

Config for access to caretakerd.

Properties

access.Permission enum

Permission represents the permission a service/node have to caretakerd.

Elements

access.Type enum

Type represents the type how the access of a service/node to caretakerd will be trusted/validated.

Elements

control.Control object

Description

Defines the access rights of caretakerctl to caretakerd.

Properties

keyStore.KeyStore object

Description

Defines the keyStore of caretakerd.

Properties

keyStore.Type enum

Description

Represents the type of the keyStore.

Elements

logger.Logger object

Description

A logger handles every output generated by the daemon itself, the process or other parts controlled by the daemon.

Properties

logger.Level enum

Description

Represents a level for logging with a Logger

Elements

logger.Pattern string

Description

A flexible pattern string.

The conversion pattern is closely related to the conversion pattern of the printf function in C. A conversion pattern is composed of literal text and format control expressions called conversion specifiers.

You are free to insert any literal text within the conversion pattern.

Each conversion specifier starts with a percent sign (%) and is followed by optional format modifiers and a conversion character. The conversion character specifies the type of data, e.g. category, priority, date, thread name. The format modifiers control such things as field width, padding, left and right justification. The following is a simple example.

Let the conversion pattern be “%d{YYYY-MM-DD HH:mm:ss} [%-5p]: %m%n” and assume that the log4j environment was set to use a PatternLayout. Then the statements:

LOG debug Message 1
LOG warn Message 2

would yield the output

2016-01-09 14:59:30 [DEBUG] Message 1
2016-01-09 14:59:31 [WARN ] Message 2

Note that there is no explicit separator between text and conversion specifiers. The pattern parser knows when it has reached the end of a conversion specifier when it reads a conversion character. In the example above the conversion specifier %-5p means the priority of the logging event should be left justified to a width of five characters. The recognized conversion characters are

Conversion patterns

  • %d[{<dateFormat>}]: Prints out the date of when the log event was created. Possible patterns:
    • Month
      • M: 1 2 … 12
      • MM: 01 01 … 12
      • Mo: 1st 2nd … 12th
      • MMM: Jan Feb … Dec
      • MMMM: January February … December
    • Day of Month
      • D: 1 2 … 31
      • DD: 01 02 … 31
      • Do: 1st 2nd … 31st
    • Day of Week
      • ddd: Sun Mon … Sat
      • dddd: Sunday Monday … Saturday
    • Year
      • YY: 70 71 … 12
      • YYYY: 1970 1971 … 2012
    • Hour
      • H: 0 1 2 … 23
      • HH: 00 01 02 .. 23
      • h: 1 2 … 12
      • hh: 01 02 … 12
    • Minute
      • m: 0 1 2 … 59
      • mm: 00 01 02 … 59
    • Second
      • s: 0 1 2 … 59
      • ss: 00 01 02 … 59
    • AM / PM
      • A: AM PM
      • a: am pm
    • Timezone
      • Z: -07:00 -06:00 … +07:00
      • ZZ: -0700 -0600 … +0700
  • %m: The log message.
  • %c[{<maximumNumberOfElements>}]: Holds the logging category. Normally instance is the name of the logger or the service. If you do not specify maximumNumberOfElements the full name is displayed. If instance is for example %c{2} and the name of the category is a.b.c then the output result is b.c.
  • %F[{<maximumNumberOfPathElements>}]: Holds the source file that logs instance event. If you do not specify maximumNumberOfPathElements the full file name is displayed. If instance is for example %F{2} and the file name is /a/b/c.go then the output result is b/c.go.
  • %l: Holds the source location of the log event.
  • %L: Holds the line number where the log event was created.
  • %C[{<maximumNumberOfElements>}]: Holds the source code package. If you do not specify maximumNumberOfElements the full name is displayed. If instance is for example %C{2} and the name of the package is a.b.c then the output result is b.c.
  • %M: Holds the method name where the log event was created.
  • %p: Holds the priority or better called log level.
  • %P[{<subFormatPattern>}]: Stacktrace of the location where a problem was raised that caused instance log message.
  • %r: Uptime of the logger.
  • %n: Prints out a new line character.
  • %%: Prints out a % character.

rpc.Rpc object

Description

Defines how caretakerd could be accessed remotely.

Properties

service.Service object

Represents the configuration of a service in caretakerd.

Properties

service.CronExpression string

Description

A cron expression represents a set of times, using 6 space-separated fields.

Field name Mandatory Allowed values Allowed special characters
Seconds No 0-59 * / , -
Minutes Yes 0-59 * / , -
Hours Yes 0-23 * / , -
Day of month Yes 1-31 * / , - ?
Month Yes 1-12 or JAN-DEC * / , -
Day of week Yes 0-6 or SUN-SAT * / , - ?

Note: Month and Day-of-week field values are case insensitive. SUN, Sun, and sun are equally accepted.

Special Characters

  • Asterisk (*) The asterisk indicates that the cron expression will match for all values of the field; e.g., using an asterisk in the 5th field (month) would indicate every month.
  • Slash (/) Slashes are used to describe increments of ranges. For example 3-59/15 in the 1st field (minutes) would indicate the 3rd minute of the hour and every 15 minutes thereafter. The form *\/... is equivalent to the form first-last/..., that is, an increment over the largest possible range of the field. The form N/... is accepted as meaning N-MAX/..., that is, starting at N, use the increment until the end of that specific range. It does not wrap around.
  • Comma (,) Commas are used to separate items of a list. For example, using MON,WED,FRI in the 5th field (day of week) would mean Mondays, Wednesdays and Fridays.
  • Hyphen (-) Hyphens are used to define ranges. For example, 9-17 would indicate every hour between 9am and 5pm inclusive.
  • Question mark (?) Question mark may be used instead of * for leaving either day-of-month or day-of-week blank.

Predefined schedules

You may use one of several pre-defined schedules in place of a cron expression.

Entry Description Equivalent To
@yearly (or @annually) Run once a year, midnight, Jan. 1st 0 0 0 1 1 *
@monthly Run once a month, midnight, first of month 0 0 0 1 * *
@weekly Run once a week, midnight on Sunday 0 0 0 * * 0
@daily (or @midnight) Run once a day, midnight 0 0 0 * * *
@hourly Run once an hour, beginning of hour 0 0 * * * *

Intervals

You may also schedule a job to execute at fixed intervals. This is supported by formatting the cron spec like this:

@every <duration>

where duration is a string accepted by time.ParseDuration.

For example, @every 1h30m10s would indicate a schedule that activates every 1 hour, 30 minutes, 10 seconds.

Hint: The interval does not take the job runtime into account. For example, if a job takes 3 minutes to run, and it is scheduled to run every 5 minutes, it will have only 2 minutes of idle time between each run.

service.Type enum

Description

Identifies the differed ways how caretakerd handles services.

Elements

values.RestartType enum

Description

RestartType tells caretakerd what to do if a process ends.

Elements

values.SignalTarget enum

SignalTarget defines who have to receives a signal.

Elements

values.SocketAddress string

Description

SocketAddress represents a socket address in format <protocol>://<target>.

Protocols

  • tcp This address connects or binds to a TCP socket. The target should be of format <host>:<port>.
    Examples:
    • tcp://localhost:57955: Listen on IPv4 and IPv6 local addresses
    • tcp://[::1]:57955: Listen on IPv6 local address
    • tcp://0.0.0.0:57955: Listen on all addresses - this includes IPv4 and IPv6
    • tcp://192.168.0.1:57955: Listen on specific IPv4 address
  • unix This address connects or binds to a UNIX file socket. The target should be the location of the socket file.
    Example:
    • unix:///var/run/caretakerd.sock

Support

If you need support you can file a ticket at our issue tracker or join our chat at gitter.im/echocat/caretakerd.

Contributing

caretakerd is an open source project of echocat. So if you want to make this project even better, you can contribute to this project on Github by fork us.

License

caretakerd
The MIT License (MIT)

Copyright (c) echocat

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.