Create service.sh

This commit is contained in:
Kroese 2023-03-31 07:55:38 +02:00 committed by GitHub
parent 477f673e17
commit 5d44582755

49
agent/service.sh Normal file
View File

@ -0,0 +1,49 @@
#!/bin/bash
PIDFILE="/var/run/agent.pid"
LOGFILE="/var/log/agent.log"
SCRIPT="/usr/local/bin/agent.sh"
status() {
if [ -f $PIDFILE ]; then
echo 'Service running' >&2
return 1
fi
}
start() {
if [ -f $PIDFILE ] && kill -0 $(cat $PIDFILE); then
echo 'Service already running' >&2
return 1
fi
printf 'Starting agent service...' >&2
"$SCRIPT" &> "$LOGFILE" & echo $! > "$PIDFILE"
}
stop() {
if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then
echo 'Service not running' >&2
return 1
fi
echo 'Stopping agent service' >&2
kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE"
echo 'Service stopped' >&2
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
stop
start
;;
*)
echo "Usage: $0 {start|stop|restart}"
esac