Skip to content

Commit

Permalink
Improved to create PID folder and set permissions to run daemon as at…
Browse files Browse the repository at this point in the history
… startup. Most systems clear out /var/run at boot so it must be initialized at boot every time.
  • Loading branch information
f34rdotcom committed Nov 28, 2017
1 parent 09f5058 commit 53cd066
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions init/ser2sock
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@ PROGRAM_NAME="ser2sock"
# Use the name of this script as the SCRIPT_NAME
SCRIPT_NAME=${0##*/}

# Uncoment next line to set user to run daemon as. Best to be a real user like "pi:pi".
#RUN_AS="root:root"

CONF_FILE="/etc/ser2sock/${SCRIPT_NAME}.conf"
EXECUTABLE="/usr/local/bin/${PROGRAM_NAME}"
PID_FILE="/var/run/${SCRIPT_NAME}.pid"
PID_FILE="/var/run/${PROGRAM_NAME}/${SCRIPT_NAME}.pid"

#EXTRA_START_ARGS="--chuid pi:pi"
EXTRA_START_ARGS=""

DAEMON_ARGS="-d -f ${CONF_FILE} -P ${PID_FILE}"
RETVAL=0

Expand All @@ -45,6 +48,26 @@ GREEN=$(tput setaf 2)
RED=$(tput setaf 1)
NORMAL=$(tput sgr0)

# PID folder sanity check. /var/run may be a tmp file system so we need to check.
if [ ! -d /var/run/${PROGRAM_NAME} ]; then
mkdir /var/run/${PROGRAM_NAME} 2>/dev/null
if [ $? -ne 0 ]; then
echo $"${BOLD}${RED}Failed to create PID folder '/var/run/${PROGRAM_NAME}/'"
exit 1
fi
fi

# Are we running as a specific user? If so then we need the correct permissions.
if [ -n "${RUN_AS}" ]; then
EXTRA_START_ARGS+=" --chuid ${RUN_AS}"
# Set permissions to pid folder for the user
chown ${RUN_AS} /var/run/${PROGRAM_NAME} 2>/dev/null
if [ $? -ne 0 ]; then
echo $"${BOLD}${RED}Failed to set PID folder '/var/run/${PROGRAM_NAME}/' owner to '${RUN_AS}'"
exit 1
fi
fi

start() {
echo
echo $"Starting instantiation ${BOLD}${SCRIPT_NAME}${NORMAL} of ${BOLD}${PROGRAM_NAME}${NORMAL}..."
Expand Down

0 comments on commit 53cd066

Please sign in to comment.