Bash Script's for Service Manager
Create your custom service for development.
docker compose run --rm test
- Create your service script.
- Define the configuration environment variables:
PID_FILE_PATH
LOG_FILE_PATH
- Define the configuration variables:
- Mandatory configuration variables:
$SERVICE_NAME
$SERVICE_CMD
(array)
- Optional configuration variables:
$SERVICE_WORK_DIR
$SERVICE_ON_START
(array)$SERVICE_ON_FINISH
(array)
- Mandatory configuration variables:
- Copy
services.sh
content or import it. - Call
serviceMenu
function and pass the action as first parameter (ex:serviceMenu "$1"
). - Make your new service script executable:
chmod a+x my-service-script
. - Use it!
Configure the PID_FILE_PATH
variable before import service.sh
script, and define the PID file path.
Configure the LOG_FILE_PATH
variable before import service.sh
script, and define the LOG file path.
This is the user friendly Service Name.
This is the commands that you must execute to start your service.
The working directory is set, where it must be located to execute the command.
Commands to execute before Service start.
If function exit code is not 0
(zero), the service will not started.
Commands to execute after Service finish/exit.
Just call only this function to make everything work!
If it is an invalid action or empty 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.run
: Execute service command and exit (this action does not stop the service).debug
: Stop service (if running) and run service command and exit.
telegraf.sh file:
#!/usr/bin/env bash
appDir=$(cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd)
. "$appDir/services.sh"
# Friendly service name (mandatory)
SERVICE_NAME="telegraf"
# Command to run (mandatory, array variable)
SERVICE_CMD=(./telegraf --config telegraf.conf)
# Working Directory (optional)
SERVICE_WORK_DIR="$appDir"
# On start (optional, array variable)
#SERVICE_ON_START=()
# On finish (optional, array variable)
#SERVICE_ON_FINISH=()
export LOG_FILE_PATH="$SERVICE_NAME.log"
export PID_FILE_PATH="$SERVICE_NAME.pid"
serviceMenu "$1"
In console:
$ telegraf.sh status
$ telegraf.sh restart
my-service file:
#!/usr/bin/env bash
export PID_FILE_PATH="my-service.pid"
export LOG_FILE_PATH="my-service.log"
. ./services.sh
# Friendly service name (mandatory)
SERVICE_NAME="Example Service"
# Command to run (mandatory, array variable)
SERVICE_CMD=(ping 1.1.1.1)
# Working Directory (optional)
#SERVICE_WORK_DIR=
# On start (optional, array variable)
#SERVICE_ON_START=()
# On finish (optional, array variable)
#SERVICE_ON_FINISH=()
serviceMenu "$1"