Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent the possible multiple recording instances from starting #407

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 60 additions & 17 deletions package/prudynt-t/files/S96record
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,74 @@ WEBUI_CONF=/etc/webui/record.conf

. /etc/init.d/rc.common

LOCK_FILE="/tmp/record.lock"

read_config() {
sed -nE "s/^.*$1:\s*\"?([^\"]+)\"?;.*$/\1/p" /etc/prudynt.cfg | head -1
}

start() {
starting
pidof $DAEMON > /dev/null && quit "$DAEMON is already running"

is_streamer_disabled && quit "Streamer disabled"
is_streamer_running || quit "Streamer is not running"
[ -f $WEBUI_CONF ] || quit "Not configured"
. $WEBUI_CONF
[ "true" = "$record_enabled" ] || quit "Disabled in $WEBUI_CONF"
[ -d "$record_mount" ] || quit "Storage is not mounted"
record_path=$(dirname "$record_mount/$(date +"$record_filename")")
[ -d "$record_path" ] || mkdir -p $record_path
[ -w "$record_path" ] || quit "No access to $record_path"
start_daemon_with_pid
starting

# Check if the lock file exists and the process is running
if [ -f "$LOCK_FILE" ]; then
existing_pid=$(cat "$LOCK_FILE")
if [ -n "$existing_pid" ] && kill -0 "$existing_pid" 2>/dev/null; then
quit "Another instance of the record script is already running (PID: $existing_pid)."
else
echo "Stale lock file found. Cleaning up."
rm -f /tmp/record.*
fi
fi

# Write current PID to the lock file
echo $$ > "$LOCK_FILE"

pidof $DAEMON > /dev/null && quit "$DAEMON is already running"

is_streamer_disabled && quit "Streamer disabled"
is_streamer_running || quit "Streamer is not running"
[ -f $WEBUI_CONF ] || quit "Not configured"
. $WEBUI_CONF
[ "true" = "$record_enabled" ] || quit "Disabled in $WEBUI_CONF"
[ -d "$record_mount" ] || quit "Storage is not mounted"
record_path=$(dirname "$record_mount/$(date +"$record_filename")")
[ -d "$record_path" ] || mkdir -p $record_path
[ -w "$record_path" ] || quit "No access to $record_path"

# Start the daemon and capture its PID
start_daemon_with_pid
daemon_pid=$(cat "$PIDFILE")

# Verify if the PID was successfully captured
if [ -n "$daemon_pid" ] && kill -0 "$daemon_pid" 2>/dev/null; then
echo "$daemon_pid" > "$LOCK_FILE"
else
echo "Failed to start daemon or capture PID."
exit 1
fi
}


stop() {
stopping
is_streamer_disabled && quit "Streamer disabled"
find /tmp/ -name "record.*" -maxdepth 0 -exec rm {} \;
stop_daemon_with_pid
stopping

# Check if the lock file exists
if [ ! -f "$LOCK_FILE" ]; then
echo "No lock file found. The process may not be running."
return 0
fi

# Retrieve the PID from the lock file
pid=$(cat "$LOCK_FILE")

# Check if the PID is valid and belongs to the running script
if kill -0 "$pid" 2>/dev/null; then
echo "Stopping process with PID $pid. Process will end after next recording interval concludes."
kill "$pid"
else
echo "No valid process found for PID $pid. Cleaning up."
fi
}

case "$1" in
Expand Down
2 changes: 2 additions & 0 deletions package/prudynt-t/files/record
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

RECORD_FLAG="/tmp/record.$$"

trap 'rm -f /tmp/record.*; exit' INT TERM EXIT

get_free_space() {
available_space=$(df -k "$record_mount" | sed 1d | tr -d '\n' | awk 'END{print $4}') # in KiB
echo "Space available: $available_space KiB"
Expand Down