caretakerd

Version: 1.0.9

caretakerd is a simple process supervisor. There are no external dependencies and it is optimized for containerization (as used in Docker) and simple configuration.

Topics

Features

Getting started

Installing

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

Linux 64-Bit example:

## Download directly from GitHub...
sudo curl -SL https://github.com/echocat/caretakerd/releases/download/v1.0.9/caretakerd-linux-amd64.tar.gz \
    | tar -xz --exclude caretakerd.html -C /usr/bin

## Download from caretakerd.echocat.org...   
sudo curl -SL https://caretakerd.echocat.org/v1.0.9/download/caretakerd-linux-amd64.tar.gz \
    | tar -xz --exclude caretakerd.html -C /usr/bin

## Download the latest version from caretakerd.echocat.org...   
sudo curl -SL https://caretakerd.echocat.org/latest/download/caretakerd-linux-amd64.tar.gz \
    | tar -xz --exclude caretakerd.html -C /usr/bin

Hint: You can use this command 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 the Configuration structure for all possibilities.

Run

caretakerd run

Control caretakerd with caretakerctl

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

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

## Retrieve the status of a service
$ caretakerctl status peasant

## Stop an already running service
$ caretakerctl stop peasant

See in action with Docker

You can find lots of working demos on github.com/echocat/caretakerd-docker-demos. Try it out!

Downloads

Commands

The way how the binary reacts depends on its command/file name. 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

usage: caretakerd [<flags>] <command> [<args> ...]

Simple control daemon for processes.

Flags:
  -c, --config=/etc/caretakerd.yaml  
    Configuration file for daemon. ($CTD_CONFIG)
  -a, --address=tcp://localhost:57955  
    Listen address of the daemon.

Commands:
help [<command>...]
    Show help.


version
    Print the actual version and other useful information.


run [<args>...]
    Run caretakerd in foreground.

caretakerctl

usage: caretakerctl [<flags>] <command> [<args> ...]

Remote control for caretakerd

Flags:
  -c, --config=/etc/caretakerd.yaml  
    Configuration file for control. ($CTCTL_CONFIG)
  -a, --address=tcp://localhost:57955  
    Listen address of the daemon.
  -p, --pem=/var/run/caretakerd.key  
    Location of PEM file which contains the private public key pair for access
    to the daemon.

Commands:
help [<command>...]
    Show help.


version
    Print the actual version and other useful information.


config <target>
    Returns configurations for defined service, '!daemon' or '!control'.


get [<target>]
    Query states for given service or if nothing specified for all services.


status <service>
    Query status of a service.


pid <service>
    Query pid of a service.


start <service>
    Starts a service.


restart <service>
    Restarts a service.


stop <service>
    Stops a service.


kill <service>
    Kills a service.


signal <service> <signal>
    Send a signal to service.

caretaker

usage: caretaker [<flags>] <command> [<args> ...]

Simple control daemon for processes including remote control for itself.

Flags:
  -c, --config=/etc/caretakerd.yaml  
    Configuration file for daemon and control. ($CT_CONFIG)
  -a, --address=tcp://localhost:57955  
    Listen address of the daemon.
  -p, --pem=/var/run/caretakerd.key  
    Location of PEM file which contains the private public key pair for access
    to the daemon.

Commands:
help [<command>...]
    Show help.


version
    Print the actual version and other useful information.


daemon [<args>...]
    Run caretakerd in foreground.


config <target>
    Returns configurations for defined service, '!daemon' or '!control'.


get [<target>]
    Query states for given service or if nothing specified for all services.


status <service>
    Query status of a service.


pid <service>
    Query pid of a service.


start <service>
    Starts a service.


restart <service>
    Restarts a service.


stop <service>
    Stops a service.


kill <service>
    Kills a service.


signal <service> <signal>
    Send a signal to service.

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
# immediately.

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 the instance keyStore.
type: generated
# (string) Defines the pemFile which contains the key and certificate to be used.
pemFile: ""
# (string) Defines some hints, for example to store in the 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 listened 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) If the property type = trusted, the certificates specified in this file are used to trust remote connections.
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 uses to log error messages.
level: info
# (logger.Level) If the service prints something to stdout, the instance will be logged with the corresponding instance level.
stdoutLevel: info
# (logger.Level) If the service prints something to stderr, the instance will be logged with the corresponding 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 be 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 code.
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 executed to stop the service.
stopCommand: []
# (int) Timeout to wait before killing 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) Additionally pass the environment variables started with caretakerd 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) If the property type = trusted, the certificates specified in this file are used to trust remote connections.
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 uses to log error messages.
level: info
# (logger.Level) If the service prints something to stdout, the instance will be logged with the corresponding instance level.
stdoutLevel: info
# (logger.Level) If the service prints something to stderr, the instance will be logged with the corresponding 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) Contains the source where this config comes from.
-:

Environment mapping

There are several environment variables mapped to configuration parameters. These environment variables are overwriting values from 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 to access caretakerd.

Properties

access.Permission enum

Permission represents the service’s/node’s permissions in caretakerd.

Elements

access.Type enum

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.

If the conversion pattern is “%d{YYYY-MM-DD HH:mm:ss} [%-5p]: %m%n” and the log4j environment has been set to use a PatternLayout. Then the statement will be:

LOG debug Message 1
LOG warn Message 2

and 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 log’s creation date. Possible patterns are:
    • 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 the instance is the name of the logger or the service. If you do not specify maximumNumberOfElements the full name is displayed. For example, if the instance is %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 the instance event. If you do not specify maximumNumberOfPathElements the full file name is displayed. For example, if the instance is %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. For example, if the instance is %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 the instance log message.
  • %r: Uptime of the logger.
  • %n: Prints out a new line character.
  • %%: Prints out a % character.

rpc.Rpc object

Description

Defines the remote access to caretakerd.

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 all values of the field; e.g., using an asterisk in the 5th field (month) would match every month.
  • Slash (/) Slashes are used to describe increments of ranges. For example 3-59/15 in the 1st field (minutes) would match 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 match Mondays, Wednesdays and Fridays.
  • Hyphen (-) Hyphens are used to define ranges. For example, 9-17 would match every hour between 9am and 5pm (inclusive).
  • Question mark (?) Question marks 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 be executed at fixed intervals. This is supported by formatting the cron spec as follows:

@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 different ways 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 the format “://“.

Protocols

  • “tcp“ This address connects or binds to a TCP socket. The “target“ should be of format “:“.
    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. 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.