/bash-service-manager

Bash Script's for Service Manager

Primary LanguageShellMIT LicenseMIT

Bash Service Manager

Bash Script's for Service Manager

Create your custom service for development.

Usage

  1. Create your service script.
  2. Define the configuration variables: PID_FILE_PATH, LOG_FILE_PATH and LOG_ERROR_FILE_PATH.
  3. Copy services.sh content or import it.
  4. Call serviceMenu function with next format: serviceMenu ACTION SERVICE_NAME COMMAND [WORK_DIR]
  5. Make your new service script executable: chmod a+x my-service-script
  6. Use it!

Configuration 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.

servicesMenu function

Just call only this function to make everything work!

Arguments

1: 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.

2: SERVICE_NAME

This is the user friendly Service Name.

3: COMMAND

This is the command function that you must execute to start your service.

Parameters:

  1. action: Caller script action (start, stop, restart, status, run, debug, tail, tail-log or tail-error).

4: WORK_DIR

This is optional

The working directory is set, where it must be located to execute the COMMAND.

5: ON_START

This is optional

Function to execute before Service Funcion start.

If function exit code is not 0 (zero), the service will not started.

Parameters:

  1. action: Caller script action (start, stop, restart, status, run, debug, tail, tail-log or tail-error).

6: ON_FINISH

This is optional

Function to execute after Service Function finish/exit.

Parameters:

  1. action: Caller script action (start, stop, restart, status, run, debug, tail, tail-log or tail-error).
  2. serviceExitCode: The COMMAND Exit Code.

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

MongoDB Service

mongo file:

#!/usr/bin/env bash

export PID_FILE_PATH="/tmp/my-service.pid"
export LOG_FILE_PATH="/tmp/my-service.log"
export LOG_ERROR_FILE_PATH="/tmp/my-service.error.log"

# Import or paste "services.sh"
. ./services.sh

run-mongo() {
  sudo rm -f /data/db/mongod.lock >/dev/null 2>&1
  sudo mongod
}

action="$1"
serviceName="mongodb"
command="run-mongo"

serviceMenu "$action" "$serviceName" "$command"

In console:

$ mongo status
$ mongo restart

Custom Service

my-service file:

#!/usr/bin/env bash

export PID_FILE_PATH="/tmp/my-service.pid"
export LOG_FILE_PATH="/tmp/my-service.log"
export LOG_ERROR_FILE_PATH="/tmp/my-service.error.log"

# Import or paste "services.sh"
. ./services.sh

run-custom() {
  bash my-service-script.sh
}

action="$1"
serviceName="my-service"
command="run-custom"
workDir="/opt/my-service"

serviceMenu "$action" "$serviceName" "$command" "$workDir"

Multiple Services

my-services file:

#!/usr/bin/env bash

export PID_FILE_PATH="/tmp/my-service.pid"
export LOG_FILE_PATH="/tmp/my-service.log"
export LOG_ERROR_FILE_PATH="/tmp/my-service.error.log"

# Import or paste "services.sh"
. ./services.sh

# Mong
run-mongo() {
  sudo rm -f /data/db/mongod.lock >/dev/null 2>&1
  sudo mongod
}

mongo() {
  serviceMenu "$1" "mongodb" "run-mongo"
}

run-custom() {
  bash my-script.sh
}

# Custom Service
custom() {
  serviceMenu "$1" "my-script" "run-custom" "/home/user/my-project"
}

$1 $2

In console:

$ my-services mongo status
$ my-services custom restart