2023-03-31 07:55:38 +02:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
PIDFILE="/var/run/agent.pid"
|
2023-04-17 19:56:31 +02:00
|
|
|
SCRIPT="/usr/local/bin/agent.sh"
|
2023-03-31 07:55:38 +02:00
|
|
|
|
|
|
|
status() {
|
2023-04-19 00:43:53 +02:00
|
|
|
|
2023-04-19 00:47:44 +02:00
|
|
|
if [ -f "$PIDFILE" ] && kill -0 "$(cat "$PIDFILE")"; then
|
2023-04-17 22:41:53 +02:00
|
|
|
echo 'Service running'
|
2023-04-17 23:01:32 +02:00
|
|
|
return 1
|
2023-03-31 07:55:38 +02:00
|
|
|
fi
|
2023-04-19 00:43:53 +02:00
|
|
|
|
2023-04-18 04:33:02 +02:00
|
|
|
return 0
|
2023-03-31 07:55:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
start() {
|
2023-04-19 00:43:53 +02:00
|
|
|
|
2023-03-31 08:12:00 +02:00
|
|
|
if [ -f "$PIDFILE" ] && kill -0 "$(cat "$PIDFILE")"; then
|
2023-04-17 22:41:53 +02:00
|
|
|
echo 'Service already running'
|
2023-04-17 23:01:32 +02:00
|
|
|
return 1
|
2023-03-31 07:55:38 +02:00
|
|
|
fi
|
2023-04-19 01:18:55 +02:00
|
|
|
|
2023-04-17 22:41:53 +02:00
|
|
|
echo 'Starting agent service...'
|
2023-04-17 15:19:26 +02:00
|
|
|
chmod 666 /dev/ttyS0
|
2023-04-19 00:43:53 +02:00
|
|
|
|
2023-04-19 01:16:35 +02:00
|
|
|
if [ ! -f "$SCRIPT" ]; then
|
2023-04-19 00:43:53 +02:00
|
|
|
|
|
|
|
echo 'ERROR: Agent script not found!' > /dev/ttyS0
|
|
|
|
|
|
|
|
TMP="/tmp/agent.sh"
|
|
|
|
URL="https://raw.githubusercontent.com/kroese/virtual-dsm/master/agent/agent.sh"
|
|
|
|
|
|
|
|
rm -f "${TMP}"
|
|
|
|
|
|
|
|
if ! curl -sfk -m 10 -o "${TMP}" "${URL}"; then
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
mv -f "${TMP}" "${SCRIPT}"
|
2023-04-19 00:47:44 +02:00
|
|
|
chmod 755 "${SCRIPT}"
|
2023-04-19 00:43:53 +02:00
|
|
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "-" > /var/lock/subsys/agent.sh
|
2023-04-17 15:20:18 +02:00
|
|
|
"$SCRIPT" &> /dev/ttyS0 & echo $! > "$PIDFILE"
|
2023-04-19 00:43:53 +02:00
|
|
|
|
2023-04-18 04:33:02 +02:00
|
|
|
return 0
|
2023-03-31 07:55:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
stop() {
|
2023-04-19 00:43:53 +02:00
|
|
|
|
2023-03-31 08:12:00 +02:00
|
|
|
if [ ! -f "$PIDFILE" ] || ! kill -0 "$(cat "$PIDFILE")"; then
|
2023-04-17 22:41:53 +02:00
|
|
|
echo 'Service not running'
|
2023-04-17 23:01:32 +02:00
|
|
|
return 1
|
2023-03-31 07:55:38 +02:00
|
|
|
fi
|
2023-04-19 00:43:53 +02:00
|
|
|
|
2023-04-17 22:41:53 +02:00
|
|
|
echo 'Stopping agent service...'
|
2023-04-19 00:43:53 +02:00
|
|
|
|
2023-04-17 22:41:53 +02:00
|
|
|
chmod 666 /dev/ttyS0
|
|
|
|
echo 'Stopping agent service...' > /dev/ttyS0
|
2023-04-19 00:43:53 +02:00
|
|
|
|
2023-03-31 08:12:00 +02:00
|
|
|
kill -15 "$(cat "$PIDFILE")" && rm -f "$PIDFILE"
|
2023-04-19 00:47:44 +02:00
|
|
|
rm -f /var/lock/subsys/agent.sh
|
2023-04-19 00:43:53 +02:00
|
|
|
|
2023-04-17 22:41:53 +02:00
|
|
|
echo 'Service stopped'
|
2023-04-18 04:33:02 +02:00
|
|
|
return 0
|
2023-03-31 07:55:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
case "$1" in
|
|
|
|
start)
|
2023-04-18 05:14:38 +02:00
|
|
|
start
|
2023-03-31 07:55:38 +02:00
|
|
|
;;
|
|
|
|
stop)
|
2023-04-18 05:14:38 +02:00
|
|
|
stop
|
2023-03-31 07:55:38 +02:00
|
|
|
;;
|
|
|
|
status)
|
2023-04-18 05:14:38 +02:00
|
|
|
status
|
2023-03-31 07:55:38 +02:00
|
|
|
;;
|
|
|
|
restart)
|
|
|
|
stop
|
2023-04-18 05:14:38 +02:00
|
|
|
start
|
2023-03-31 07:55:38 +02:00
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "Usage: $0 {start|stop|restart}"
|
2023-04-18 05:14:38 +02:00
|
|
|
exit 1
|
2023-03-31 07:55:38 +02:00
|
|
|
esac
|
2023-04-18 04:33:02 +02:00
|
|
|
|