From 00e46ed40164230fe3f0f745913e091d653ff6c2 Mon Sep 17 00:00:00 2001 From: Ezra Taimuty-Loomis Date: Fri, 21 Aug 2020 14:53:59 -0400 Subject: [PATCH] Scripts to run server as service --- README.md | 4 +- src/CMakeLists.txt | 8 ++- src/{piScan_backend.cpp => piscan_server.cpp} | 0 src/service/piscan_server | 67 +++++++++++++++++++ src/service/piscan_server.service | 9 +++ 5 files changed, 84 insertions(+), 4 deletions(-) rename src/{piScan_backend.cpp => piscan_server.cpp} (100%) create mode 100644 src/service/piscan_server create mode 100644 src/service/piscan_server.service diff --git a/README.md b/README.md index 06c22a0..53795ee 100644 --- a/README.md +++ b/README.md @@ -83,14 +83,14 @@ Now you can build PiScan: make all Alternatively, you can build the binaries individually if you don't need all of them - make piScan_backend # main program + make piscan_server # main program make piscan_hpdconv # tool to convert Uniden Sentinel files to PiScan files ### Running For now, you have to be in the `build` directory to run the program. In the future there will be an install command to eliminate the need for this. You can now run PiScan from the command line: - ./piScan_backend + ./src/piscan_server See **Usage** for more information on command arguments and setting up the data files diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 55a9503..855a48a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,6 +1,6 @@ project (piscan_server) -add_executable(piscan_server piScan_backend.cpp) +add_executable(piscan_server piscan_server.cpp) MACRO(use_included_rtaudio) SET (cubicsdr_sources @@ -149,6 +149,7 @@ set(APPLICATION_LIBS common external loguru proto scan server sigproc util ${APP target_link_libraries(piscan_server ${APPLICATION_LIBS}) +set(CPACK_SET_DESTDIR ON) set(CPACK_GENERATOR "DEB") set(CPACK_DEBIAN_PACKAGE_MAINTAINER "etaimutyloomis@gmail.com") set(CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON) @@ -159,4 +160,7 @@ install( DESTINATION /usr/local/bin LIBRARY DESTINATION /usr/local/bin ) - +install( + FILES ${CMAKE_CURRENT_SOURCE_DIR}/service/piscan_server DESTINATION /etc/init.d + FILES ${CMAKE_CURRENT_SOURCE_DIR}/service/piscan_server.service DESTINATION /etc/systemd/system +) diff --git a/src/piScan_backend.cpp b/src/piscan_server.cpp similarity index 100% rename from src/piScan_backend.cpp rename to src/piscan_server.cpp diff --git a/src/service/piscan_server b/src/service/piscan_server new file mode 100644 index 0000000..e8ffc31 --- /dev/null +++ b/src/service/piscan_server @@ -0,0 +1,67 @@ +#!/bin/sh +### BEGIN INIT INFO +# Provides: piscan-server +# Required-Start: $local_fs $network $named $time $syslog +# Required-Stop: $local_fs $network $named $time $syslog +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Description: +### END INIT INFO + +SCRIPT=/usr/local/bin/piscan_server +RUNAS=ezra + +PIDFILE=/var/run/piscan-server.pid +LOGFILE=/var/log/piscan-server.log + +start() { + if [ -f /var/run/$PIDNAME ] && kill -0 $(cat /var/run/$PIDNAME); then + echo 'Service already running' >&2 + return 1 + fi + echo 'Starting service…' >&2 + local CMD="$SCRIPT &> /dev/null & echo \$!" + su -c "$CMD" -l $RUNAS > "$PIDFILE" + echo 'Service started' >&2 +} + +stop() { + if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then + echo 'Service not running' >&2 + return 1 + fi + echo 'Stopping service…' >&2 + kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE" + echo 'Service stopped' >&2 +} + +uninstall() { + echo -n "Are you really sure you want to uninstall this service? That cannot be undone. [yes|No] " + local SURE + read SURE + if [ "$SURE" = "yes" ]; then + stop + rm -f "$PIDFILE" + echo "Notice: log file is not be removed: '$LOGFILE'" >&2 + update-rc.d -f piscan-server remove + rm -fv "$0" + fi +} + +case "$1" in + start) + start + ;; + stop) + stop + ;; + uninstall) + uninstall + ;; + restart) + stop + start + ;; + *) + echo "Usage: $0 {start|stop|restart|uninstall}" +esac diff --git a/src/service/piscan_server.service b/src/service/piscan_server.service new file mode 100644 index 0000000..582bd20 --- /dev/null +++ b/src/service/piscan_server.service @@ -0,0 +1,9 @@ +[Unit] +Description=PiScan Server + +[Service] +Type=simple +ExecStart=/usr/local/bin/piscan_server -v 0 + +[Install] +WantedBy=multi-user.target