KAOSv 2.15.3
About
KAOSv is bash lib for SysV init scripts.
This lib can be used as replacement for default /etc/init.d/functions lib.
Constants
Global Variables
Methods
Constants
Code for reset prev color codes
Code for light grey color
Status code for working service
Status code for stopped service
Status code for broken service
Status code for a situation when service is stopped, but PID file is exist
Status code for a situation when service is stopped, but lock file is exist
Status code for a situation when service is running, but PID file doesn't exist
Status code for a situation when service is running, but lock file doesn't exist
Status for an action performed without errors
Status for an action with some errors
Status for an action which was forced
This constant can be used as argument in kv.setOOMScoreAdj for disabling OOM Killer
Global Variables
Program name used as the base for help/paths/files generation
Automatically create PID file while service demonization
Automatically create lock file while service demonization
Automatically generate help command content
Automatically log all actions (start/stop/restart/etc...)
Automatically search PID if PID file doesn't exist
Time in seconds for service start
Time in seconds for service stop
Delay between start and stop
Processes soft/hard limit
Real user name (search real user name over sudo)
Temporary file for service output (can be only set to /dev/null)
cmd_info = ($(pgrep -o -l -f "$pkg_file")) STRING
shellcheck disable=SC2207
Methods
kv.go — Start command processing
*. All arguments passed to service script
Example:
kv.require "2.7"
1. Command name STRING
2. Command description STRING
3. Handler function name STRING
4. Do not require root for execute BOOLEAN OPTIONAL
Example:
kv.AddCommand "check" "Check config value" "myCheckFunc" true
kv.AddCommand "kill" "Kill connections" "myKillFunc"
kv.addHandler — Add handler for start/stop/restart/status command
1. Command name STRING
2. Handler STRING
3. Type of handler (pre/post/default) STRING
1. Command name STRING
2. Alias name STRING
Example:
kv.addCommandAlias "test" "check"
1. Command STRING
*. Vars STRING
Example:
kv.addCommandArgs "stop" "force"
1. Handler name STRING
2. Type of handler (pre/post/default) STRING
1. Name of sysconfig file STRING OPTIONAL
Code: 0 - ok, 1 - not ok
Example:
local status=$(kv.run "/bin/echo" "1234" "ABCD")
if [[ $status -eq 0 ]] ; then
return $ACTION_OK
else
return $ACTION_ERROR
fi
1. User STRING
*. Command
Code: 0 - ok, 1 - not ok
Example:
kv.runAs "someuser" "/bin/echo" "1234" "ABCD"
if [[ $? -eq 0 ]] ; then
return $ACTION_OK
else
return $ACTION_ERROR
fi
1. Binary STRING
*. Command arguments
Code: 0 - ok, 1 - not ok
Example:
kv.daemonize "/usr/bin/daemon" "-d" "-p 80" "-v"
if [[ $? -eq 0 ]] ; then
return $ACTION_OK
else
return $ACTION_ERROR
fi
kv.start — Execute default start handler
kv.stop — Execute default stop handler
kv.usage — Execute default usage handler
kv.requireRoot — Require root privileges, if user is not a root, exit with error status
1. Status NUMBER OPTIONAL
Code: 0 - ok, 1 - not ok
Echo: PID NUMBER
Example:
local pid=$(kv.getPID)
kv.sendSignal $pid
Echo: PID NUMBER
Example:
local pid=$(kv.findPID)
Example:
kv.showProcessMessage "Generating data"
kv.showStatusMessage $ACTION_OK
Example:
kv.showProcessMessage "Generating data"
kv.showStatusMessage $ACTION_OK
1. Path to PID file STRING OPTIONAL
2. Don't check process status BOOLEAN OPTIONAL
Code: 0 - ok, 1 - not ok
Example:
if ! kv.getStartStatus ; then
kv.error "Can't start service"
fi
Code: 0 - ok, 1 - not ok
Example:
if kv.getStopStatus $pid ; then
return $ACTION_OK
else
kv.killProcess $pid
return $ACTION_FORCED
fi
Code: 0 - ok, 1 - not ok
Example:
if kv.isProcExist 12345 ; then
kv.error "Process with PID 12345 doesn't exist"
fi
kv.hasUser — Return 0 if user exists on the system
Code: 0 - ok, 1 - not ok
Example:
if ! kv.hasUser "serviceuser" ; then
kv.error "User serviceuser doesn't exist!"
fi
kv.isBoundPort — Check that port is bound (occupied by another process)
Code: 0 - ok, 1 - not ok
Example:
if kv.isBoundPort 8080 ; then
kv.error "Port 8080 is bound"
fi
Code: 0 - ok, 1 - not ok
Example:
if kv.isServiceWorks "nginx" ; then
kv.error "Service nginx is works"
fi
Code: 0 - ok, 1 - not ok
Example:
if ! kv.isInstalled "java" "nc" ; then
kv.error "Java and netcat is not installed"
fi
1. Signal STRING
2. PID NUMBER OPTIONAL
Code: 0 - ok, 1 - not ok
Example:
if ! kv.sendSignal $SIGNAL_QUIT ; then
kv.error "Can't send signal"
fi
1. Adjustment level NUMBER OPTIONAL
2. PID NUMBER OPTIONAL
Example:
kv.setOOMScoreAdj "-1000" "12345"
kv.setOOMScoreAdj "$OOM_DISABLE"
Code: 0 - ok, 1 - not ok
Example:
if ! kv.isRoot ; then
kv.error "This action require root privileges!"
fi
Code: 0 - ok, 1 - not ok
Example:
if kv.hasLock ; then
echo "Lock file exist!"
fi
Example:
kv.createPIDFile 12345
1. Error code NUMBER OPTIONAL
Example:
kv.exit
kv.exit 1
Example:
kv.error "My error message"
Example:
kv.warn "My warning message"
1. Message STRING
2. Color STRING OPTIONAL
Example:
show "Signal sended"
show "Can't do some action" $RED
kv.showm — Show message without new line symbol
1. Message STRING
2. Color STRING OPTIONAL
Example:
kv.restoreSEContext "/path/to/file"
1. File STRING
2. Property name STRING
3. Delimiter STRING OPTIONAL
Echo: Property value STRING
Example:
local port=$(kv.readProperty "/path/to/my.conf" "port")
local host=$(kv.readProperty "/path/to/my.conf" "host" ":")
kv.log — Add message to logfile or logger
Example:
kv.log "My very important message"