initial commit
This commit is contained in:
commit
18b03c00f2
91
README.md
Normal file
91
README.md
Normal file
|
@ -0,0 +1,91 @@
|
|||
bes-service
|
||||
========
|
||||
|
||||
bes-service is a small bash bes library to easily create systemd service.
|
||||
|
||||
### Usage
|
||||
|
||||
add bes-service as dependency in bes.ini file of your project.
|
||||
|
||||
```
|
||||
[require]
|
||||
bes.service = 1.0
|
||||
```
|
||||
|
||||
create your service following 'src/main.sh.tpl' template.
|
||||
edit for example 'src/main.sh' in your bes project ('myservice' in this example) :
|
||||
|
||||
```
|
||||
#!/bin/bash
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
#
|
||||
# @author you - url repo
|
||||
# @license licence
|
||||
# @date 2017-07-07 02:21:51 CET
|
||||
#
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
BES_SERVICE_NAME="myservice"
|
||||
BES_SERVICE_LOGGER="myservice.type1"
|
||||
BES_SERVICE_DELAY=10
|
||||
BES_SERVICE_BOOT=1
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# optional - service is started, do before running
|
||||
function service.on.init()
|
||||
{
|
||||
service.log " >< service.on.init"
|
||||
}
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# optional - service is stopped, do before exiting
|
||||
function service.clean()
|
||||
{
|
||||
service.log " >< service.clean"
|
||||
}
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# your main service function
|
||||
# oneshot if $BES_SERVICE_DELAY equal 0
|
||||
# or repeat each $BES_SERVICE_DELAY seconds otherwise
|
||||
function service.on.run()
|
||||
{
|
||||
# do stuff
|
||||
service.log " >< do important things, every $BES_SERVICE_DELAY seconds"
|
||||
}
|
||||
|
||||
service.launch $*
|
||||
```
|
||||
|
||||
install bes dependencies :
|
||||
|
||||
```
|
||||
bes-build update
|
||||
```
|
||||
|
||||
build your project :
|
||||
|
||||
```
|
||||
bes-build
|
||||
```
|
||||
|
||||
then you call install your service with :
|
||||
|
||||
```
|
||||
./dist/myservice install
|
||||
```
|
||||
|
||||
and start/stop your service like others in systemd
|
||||
|
||||
```
|
||||
service myservice start # stop | restart
|
||||
# or
|
||||
systemctl start myservice
|
||||
```
|
||||
|
||||
inspect service log :
|
||||
|
||||
```
|
||||
tail -f /var/log/myservice.log
|
||||
```
|
||||
|
||||
|
10
bes.ini
Normal file
10
bes.ini
Normal file
|
@ -0,0 +1,10 @@
|
|||
[project]
|
||||
vendor = bes
|
||||
name = service
|
||||
version = 1.0
|
||||
license = "GNU GPL v3"
|
||||
author = a-Sansara
|
||||
type = library
|
||||
homepage = "https://git.pluie.org/meta-tech/bes-service"
|
||||
description = "bash bes systemd service utility library"
|
||||
keywords = "bash, bes, service, systemd"
|
39
src/main.sh.tpl
Normal file
39
src/main.sh.tpl
Normal file
|
@ -0,0 +1,39 @@
|
|||
#!/bin/bash
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
#
|
||||
# @author you - url repo
|
||||
# @license licence
|
||||
# @date 2017-07-07 02:21:51 CET
|
||||
#
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
BES_SERVICE_NAME="myservice"
|
||||
BES_SERVICE_LOGGER="myservice.type1"
|
||||
BES_SERVICE_DELAY=10
|
||||
BES_SERVICE_BOOT=1
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# optional - service is started, do before running
|
||||
function service.on.init()
|
||||
{
|
||||
service.log " >< service.on.init"
|
||||
}
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# optional - service is stopped, do before exiting
|
||||
function service.clean()
|
||||
{
|
||||
service.log " >< service.clean"
|
||||
}
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# your main service function
|
||||
# oneshot if $BES_SERVICE_DELAY equal 0
|
||||
# or repeat each $BES_SERVICE_DELAY seconds otherwise
|
||||
function service.on.run()
|
||||
{
|
||||
# do stuff
|
||||
service.log " >< do important things, every $BES_SERVICE_DELAY seconds"
|
||||
}
|
||||
|
||||
service.launch $*
|
||||
|
166
src/service.sh
Normal file
166
src/service.sh
Normal file
|
@ -0,0 +1,166 @@
|
|||
#!/bin/bash
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
#
|
||||
# @author a-Sansara - https://git.pluie.org/meta-tech/bes-service
|
||||
# @app bes-config
|
||||
# @license GNU GPL v3
|
||||
# @date 2019-02-26 13:24:55 CET
|
||||
#
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
function bes.service.boot ()
|
||||
{
|
||||
BES_SERVICE_NAME="yourservice"
|
||||
BES_SERVICE_LOGGER="channel1"
|
||||
BES_SERVICE_DELAY=0
|
||||
BES_SERVICE_BOOT=0
|
||||
}
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
function service.time()
|
||||
{
|
||||
date +'%Y-%m-%d %H:%M:%S'
|
||||
}
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
function service.log()
|
||||
{
|
||||
local dt=$(service.time)
|
||||
local msg="${1:-}"
|
||||
local display="${2:-0}"
|
||||
if [ "$display" = "1" ]; then
|
||||
echo "[$dt] $msg";
|
||||
fi
|
||||
echo "[$dt] $BES_SERVICE_LOGGER $msg" >> "$BES_SERVICE_LOG"
|
||||
}
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
function service.init()
|
||||
{
|
||||
service.log " > init()"
|
||||
if [ ! -d "$BES_SERVICE_RUN_DIR" ]; then
|
||||
mkdir -p "$BES_SERVICE_RUN_DIR"
|
||||
fi
|
||||
if bes.exists "service.on.init"; then
|
||||
service.on.init
|
||||
fi
|
||||
service.log " < init()"
|
||||
}
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
function service.isalive()
|
||||
{
|
||||
if [ -z "$1" ]; then
|
||||
return 1
|
||||
elif [ "$1" -gt 0 ]; then
|
||||
ps -p $1 &> /dev/null
|
||||
fi
|
||||
}
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
function service.pid()
|
||||
{
|
||||
if [ -z "$1" ]; then
|
||||
cat "$BES_SERVICE_RUN_DIR/$BES_SERVICE_NAME.pid"
|
||||
elif [ "$1" -gt 0 ]; then
|
||||
echo $1 > "$BES_SERVICE_RUN_DIR/$BES_SERVICE_NAME.pid"
|
||||
fi
|
||||
}
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
function service.start()
|
||||
{
|
||||
local pid=$(service.pid)
|
||||
if service.isalive $pid; then
|
||||
service.log "already started";
|
||||
else
|
||||
service.init
|
||||
service.log " > starting... ($$)"
|
||||
service.pid $$
|
||||
service.run
|
||||
fi
|
||||
}
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
function service.stop()
|
||||
{
|
||||
local pid=$(service.pid)
|
||||
if service.isalive $pid; then
|
||||
service.log "stopping... ($pid)";
|
||||
kill -9 $pid
|
||||
if bes.exists "service.clean"; then
|
||||
service.clean
|
||||
fi
|
||||
else
|
||||
service.log "already stopped";
|
||||
fi
|
||||
}
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
function service.run()
|
||||
{
|
||||
if bes.exists "service.on.run"; then
|
||||
if [ "$BES_SERVICE_DELAY" -eq 0 ]; then
|
||||
service.on.run
|
||||
elif [ "$BES_SERVICE_DELAY" -gt 0 ]; then
|
||||
while true; do
|
||||
sleep $BES_SERVICE_DELAY
|
||||
service.on.run
|
||||
done
|
||||
fi
|
||||
fi
|
||||
}
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
function service.install()
|
||||
{
|
||||
local dir=$(dirname "${BASH_SOURCE[0]}")
|
||||
if [ -f "$dir/$BES_SERVICE_NAME" ]; then
|
||||
sudo cp "$dir/$BES_SERVICE_NAME" /usr/sbin/
|
||||
if [ ! -f "$dir/$BES_SERVICE_NAME.service" ]; then
|
||||
echo "[Unit]
|
||||
Description=$1
|
||||
ConditionPathExists=/usr/sbin/$BES_SERVICE_NAME
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
ExecStart=/usr/sbin/$BES_SERVICE_NAME start
|
||||
ExecStop=/usr/sbin/$BES_SERVICE_NAME stop
|
||||
# Restart=always
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
" | sudo tee -a "$dir/$BES_SERVICE_NAME.service"
|
||||
fi
|
||||
sudo cp "$dir/$BES_SERVICE_NAME.service" "/etc/systemd/system/$BES_SERVICE_NAME.service"
|
||||
sudo systemctl --system daemon-reload
|
||||
if [ "$BES_SERVICE_BOOT" = 1 ]; then
|
||||
echo " enable service at boot time"
|
||||
sudo systemctl enable "$BES_SERVICE_NAME"
|
||||
fi
|
||||
echo "service $BES_SERVICE_NAME is installed"
|
||||
else
|
||||
echo " ! service file : '$dir/$BES_SERVICE_NAME' not found"
|
||||
echo " install aborted"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
function service.launch()
|
||||
{
|
||||
export BES_SERVICE_LOG="/var/log/$BES_SERVICE_NAME.log"
|
||||
export BES_SERVICE_RUN_DIR="/run/$BES_SERVICE_NAME"
|
||||
case "$1" in
|
||||
install)
|
||||
service.install "$2"
|
||||
;;
|
||||
start)
|
||||
service.start
|
||||
;;
|
||||
stop)
|
||||
service.stop
|
||||
;;
|
||||
restart)
|
||||
service.stop
|
||||
service.start
|
||||
;;
|
||||
*)
|
||||
echo "Usage: $0 {start|stop|restart}"
|
||||
esac
|
||||
}
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
declare -f bes.reg > /dev/null
|
||||
if [ $? -eq 0 ]; then
|
||||
bes.reg bes.service
|
||||
fi
|
||||
|
Loading…
Reference in New Issue
Block a user