Home

Awesome

unitscript

This program can be used as an interpreter for unitscripts, which are yaml based init scripts. Unitscripts could be used as easier alternative to shell scripts as init scripts or to standardise init scripts across many different sysv-rc compatible distributions. This project was created to show how to create something similar to systemd units which could be used to standardise init scripts across distributions but using existing technologies and without imposing a system which has since grown way bayond any reasonable scope on everybody.

This project does only provide some basic functionality to start and stop programs at the moment and may be extendet by additional features in the future. It will, however, never become a daemon in the classical sense and unitscripts will never do anything else than managing services.

Currently supported actions

actiondescription
startStarts the program. The script won't exit until the 'start check' condition is met.
stopStops the program by sending it a SIGTERM signal. Won't exit until the process whose pid is written in the pid file doesn't exist anymore or an error occurs.
restartshorthand for start and stop actions.
statusReturns if the process is running and on which pid or not. Exit code will be either 0 for yes, 1 fo no, or anything else to indicate an error.
checkDoes nothing, but will still exit with an error message if the unitscript contains an error
zapRemoves the pid file

Format

A unitscript consists of the shebang line #!/usr/bin/env unitscript, followed by an LSB Header, followed by the unitscript options. The wholeunit script is a valid yaml file.

The following unitscript options are currently supported:

optiontypedescription
userstringA valid user name from /etc/passwd
groupstringA valid group name from /etc/group
start checkstringCan be either exit, start, or notification. <br/> exit: The process forks itself, wait until it exits and check the return code. <br/> start: The moment the executable is executed is considered a successful start. <br/> notification: The process writes a newline to a file descriptor to indicate the start succeeded. You can specify the file descriptor using the notifyfd property, default is 3.
programstringA shell script. The default interpreter is sh -l, but can be overwritten using a shebang line
uidintegerA user id of a valid user from /etc/passwd
gidintegerA group id of a valid group from /etc/group
notifyfdintegerUsed if start check is set to notification. Default is 3.
loggingstringIf and how stdout and stdin should be redirected. Can be either: default, syslog, stdio or none. <br/> default: equivalent to syslog if 'start check' is 'start', stdout if 'start check' is 'exit', invalid otherwise. <br/> syslog: redirect stdout and stderr to syslog before executing 'program'. <br/> stdio: keep stdout and stderr unchanged <br/> none: close stdout and stderr before execution.
pidfilestringDefaults to /var/run/unitscript/<script name>.pid
manage pidfilebooleanEither yes or no. Specifies if the program or the unit script should create the pid file. <br/> Default for 'start check' 'exit' is 'no'. <br/> Default for 'start check' 'start' is 'yes'.
env        map    Environment variables to set before program execution
env scriptslistA list of posix shell scripts. The variables those scripts define will be exported as environment variables before executing the program
env fileslistA list of files similar in syntax to /etc/environment. The variables those scripts define will be exported as environment variables before executing the program
working directorystringSets the working directory. Defaults to the user home directory.
umaskintegerThe umask using which the program is started, defaults to 0022
rlimits   map    All posix rlimits and some system specific ones can be set. The name of the rlimit should be in lower case and shouldn't be prefixed with RLIMIT_. The value for an rlimit can either be an integer, which will set the cur and max rlimit values, or max:cur, where max and cur are integers used to set the max and cur values of the rlimit.

Environment variables

All environment variables will be cleared before execution, but per default a sh login shell is used to start the program, which should source /etc/profile and .profile, which may set some basic environment variables like the PATH variable. In addition to this, all variables specified using the 'env', 'env files' and 'env scripts' option and the following ones will be set:

variabledescription
SHELLThe shell specified for the user in /etc/passwd
HOMEThe user home directory as specified in /etc/passwd
PIDFILEThe location of the pid file
NOTIFICATION_FDThe file descriptor number using which the program can indicate that it finished starting up

Build requirements

This project requires my dpaparser project for building. You can find it at https://github.com/Daniel-Abrecht/dpaparser https://gitlab.com/DanielAbrecht/dpaparser Or wherever else I'll put a copy of my projects in addition to that in the future.

You'll also need libbsd and libyaml.