Bash Service Manager
Bash Script's for Service Manager
Create your custom service for development.
Usage
- Create your service script.
- Define the configuration environment variables:
PID_FILE_PATH
,LOG_FILE_PATH
andLOG_ERROR_FILE_PATH
. - Define the configuration variables
- Mandatory configuration variables:
$action
,$serviceName
,$command
(array) - Optional configuration variables:
$workDir
,$onStart
(array),$onFinish
(array)
- Mandatory configuration variables:
- Copy
services.sh
content or import it. - Call
serviceMenu
function - Make your new service script executable:
chmod a+x my-service-script
- Use it!
Configuration Environment Variables
PID_FILE_PATH
Configure the PID_FILE_PATH
variable before import service.sh
script, and define the PID file path.
LOG_FILE_PATH
Configure the LOG_FILE_PATH
variable before import service.sh
script, and define the LOG file path.
LOG_ERROR_FILE_PATH
Configure the LOG_ERROR_FILE_PATH
variable before import service.sh
script, and define the ERROR file path.
Configuration Variables
Mandatory Configuration Variables
action
This is the action to execute. Please see Actions section below for more information.
If it is an invalid action or emtpy action, you can see the help.
serviceName
This is the user friendly Service Name.
command (array variable)
This is the commands that you must execute to start your service.
Optional Configuration Variables
workDir
The working directory is set, where it must be located to execute the command.
onStart (array variable)
Commands to execute before Service start.
If function exit code is not 0
(zero), the service will not started.
onFinish (array variable)
Commands to execute after Service finish/exit.
servicesMenu function
Just call only this function to make everything work!
Actions
If it is an invalid action or emtpy action, you can see the help.
start
: Start the service.stop
: Stop the service.restart
: Restart the service. If the service is running, first call stop then call start.status
: Get service status.tail
: See all service output.tail-log
: See service std output.tail-error
: See service err output.run
: Execute service command and exit (this action does not stop the service).debug
: Stop service (if running) and run service command and exit.
Examples
Telegraf Service
telegraf.sh file:
#!/usr/bin/env bash
appDir=$(cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd)
export LOG_FILE_PATH="$serviceName.log"
export LOG_ERROR_FILE_PATH="$serviceName.error.log"
export PID_FILE_PATH="$serviceName.pid"
. "$appDir/services.sh"
# Action to execute (mandatoty)
action="$1"
# Friendly service name (mandatoty)
serviceName= "telegraf"
# Command to run (mandatoty, array variable)
command=(./telegraf --config telegraf.conf)
# Working Directory (optional)
workDir="$appDir"
# On start (optional, array variable)
#onStart=()
# On finish (optional, array variable)
#onFinish=()
serviceMenu
In console:
$ telegraf.sh status
$ telegraf.sh restart
Custom Service
my-service file:
#!/usr/bin/env bash
export PID_FILE_PATH="my-service.pid"
export LOG_FILE_PATH="my-service.log"
export LOG_ERROR_FILE_PATH="my-service.error.log"
. ./services.sh
# Action to execute (mandatoty)
action="$1"
# Friendly service name (mandatoty)
serviceName="Example Service"
# Command to run (mandatoty, array variable)
command=("ping 1.1.1.1")
# Working Directory (optional)
#workDir=
# On start (optional, array variable)
#onStart=()
# On finish (optional, array variable)
#onFinish=()
serviceMenu