Giter Site home page Giter Site logo

bash-service-manager's Introduction

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

bash-service-manager's People

Contributors

reduardo7 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.