Move handling of lockfiles back to the script. Use trap instead.
This commit is contained in:
@@ -6,6 +6,7 @@ REPO_LOCAL_PATH="/opt/src"
|
|||||||
BRANCH_NAME="develop"
|
BRANCH_NAME="develop"
|
||||||
POLL_INTERVAL=60 # Seconds between checks
|
POLL_INTERVAL=60 # Seconds between checks
|
||||||
#LOG_FILE="/var/log/git-monitor.log" # Log file for recording actions
|
#LOG_FILE="/var/log/git-monitor.log" # Log file for recording actions
|
||||||
|
LOCK_FILE="/run/lock/git-monitor.lock" # File to prevent multiple instances
|
||||||
IFS="|"
|
IFS="|"
|
||||||
|
|
||||||
# Functions
|
# Functions
|
||||||
@@ -17,6 +18,21 @@ log_message() {
|
|||||||
echo "$timestamp [$level] $message" # Also print to console (optional)
|
echo "$timestamp [$level] $message" # Also print to console (optional)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
check_lock() {
|
||||||
|
if [ -f "$LOCK_FILE" ]; then
|
||||||
|
log_message "WARN" "Another instance is already running. Exiting."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
create_lock() {
|
||||||
|
touch "$LOCK_FILE"
|
||||||
|
}
|
||||||
|
|
||||||
|
remove_lock() {
|
||||||
|
rm -f "$LOCK_FILE"
|
||||||
|
}
|
||||||
|
|
||||||
run_after() {
|
run_after() {
|
||||||
local after_pull_script=$1
|
local after_pull_script=$1
|
||||||
source $after_pull_script
|
source $after_pull_script
|
||||||
@@ -68,6 +84,12 @@ check_for_updates() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
# Main Execution
|
# Main Execution
|
||||||
|
|
||||||
|
trap 'remove_lock' EXIT
|
||||||
|
|
||||||
|
check_lock
|
||||||
|
create_lock
|
||||||
|
|
||||||
log_message "INFO" "Git monitor started."
|
log_message "INFO" "Git monitor started."
|
||||||
|
|
||||||
initialize_repos
|
initialize_repos
|
||||||
@@ -81,3 +103,5 @@ while true; do
|
|||||||
done < $REPO_URLS
|
done < $REPO_URLS
|
||||||
sleep "$POLL_INTERVAL"
|
sleep "$POLL_INTERVAL"
|
||||||
done
|
done
|
||||||
|
|
||||||
|
remove_lock # This won't be reached in this infinite loop. Useful if exiting gracefully.
|
||||||
|
|||||||
@@ -4,9 +4,7 @@ After=network.target
|
|||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
ExecStart=/usr/local/bin/git-monitor
|
ExecStart=/usr/local/bin/git-monitor
|
||||||
LockFile=/run/lock/git-monitor-lock
|
|
||||||
Restart=on-failure
|
Restart=on-failure
|
||||||
RemovalPolicy=onFailure
|
|
||||||
User=monitor
|
User=monitor
|
||||||
WorkingDirectory=/opt/src
|
WorkingDirectory=/opt/src
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user