Bash Script's for Service Manager
Create your custom service for development.
- Create your service script.
- Define the configuration variables:
PID_FILE_PATH
,LOG_FILE_PATH
andLOG_ERROR_FILE_PATH
. - Copy
services.sh
content or import it. - Call
serviceMenu
function with next format:serviceMenu ACTION SERVICE_NAME COMMAND [WORK_DIR]
- 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.
Configure the LOG_ERROR_FILE_PATH
variable before import service.sh
script, and define the ERROR file path.
Just call only this function to make everything work!
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.
This is the user friendly Service Name.
This is the command function that you must execute to start your service.
Parameters:
action
: Caller script action (start
,stop
,restart
,status
,run
,debug
,tail
,tail-log
ortail-error
).
This is optional
The working directory is set, where it must be located to execute the COMMAND.
This is optional
Function to execute before Service Funcion start.
If function exit code is not 0
(zero), the service will not started.
Parameters:
action
: Caller script action (start
,stop
,restart
,status
,run
,debug
,tail
,tail-log
ortail-error
).
This is optional
Function to execute after Service Function finish/exit.
Parameters:
action
: Caller script action (start
,stop
,restart
,status
,run
,debug
,tail
,tail-log
ortail-error
).serviceExitCode
: The COMMAND Exit Code.
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.
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
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"
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