Setup crontab work with root

This commit is contained in:
Alexandru-Victor Macocian
2025-10-30 17:36:40 +01:00
parent b7cd8076c7
commit c7ba79c856
+65 -22
View File
@@ -12,7 +12,16 @@ SCRIPTS=(
) )
OS_TYPE=$(DetectOS) OS_TYPE=$(DetectOS)
LAUNCHD_DIR="$HOME/Library/LaunchAgents"
# Determine LaunchAgent vs LaunchDaemon directory
# If running as root on macOS, use system-wide LaunchDaemons for headless operation
if [[ "$OS_TYPE" == "macos" && $EUID -eq 0 ]]; then
LAUNCHD_DIR="/Library/LaunchDaemons"
LAUNCHD_TYPE="daemon"
else
LAUNCHD_DIR="$HOME/Library/LaunchAgents"
LAUNCHD_TYPE="agent"
fi
CheckScriptExists() { CheckScriptExists() {
local script="$1" local script="$1"
@@ -77,7 +86,7 @@ ManageLaunchAgent() {
local plist_name="com.servermetrics.${script_name}.plist" local plist_name="com.servermetrics.${script_name}.plist"
local plist_path="${LAUNCHD_DIR}/${plist_name}" local plist_path="${LAUNCHD_DIR}/${plist_name}"
# Ensure LaunchAgents directory exists # Ensure LaunchAgents/LaunchDaemons directory exists
if [[ ! -d "$LAUNCHD_DIR" ]]; then if [[ ! -d "$LAUNCHD_DIR" ]]; then
mkdir -p "$LAUNCHD_DIR" mkdir -p "$LAUNCHD_DIR"
fi fi
@@ -107,18 +116,39 @@ ManageLaunchAgent() {
EOF EOF
if [[ $? -eq 0 ]]; then if [[ $? -eq 0 ]]; then
LogInformation "Created LaunchAgent plist: $plist_path" # Set proper ownership for LaunchDaemons
if [[ "$LAUNCHD_TYPE" == "daemon" ]]; then
chown root:wheel "$plist_path"
chmod 644 "$plist_path"
fi
# Unload if already loaded (ignore errors) LogInformation "Created Launch${LAUNCHD_TYPE^} plist: $plist_path"
launchctl unload "$plist_path" 2>/dev/null || true
# Load the plist # Use appropriate launchctl commands based on type
if launchctl load "$plist_path" 2>/dev/null; then if [[ "$LAUNCHD_TYPE" == "daemon" ]]; then
LogInformation "Loaded LaunchAgent for $script" # Unload if already loaded (ignore errors)
return 0 launchctl bootout system "$plist_path" 2>/dev/null || true
# Load the plist
if launchctl bootstrap system "$plist_path" 2>/dev/null; then
LogInformation "Loaded LaunchDaemon for $script"
return 0
else
LogError "Failed to load LaunchDaemon for $script"
return 1
fi
else else
LogError "Failed to load LaunchAgent for $script" # Unload if already loaded (ignore errors)
return 1 launchctl unload "$plist_path" 2>/dev/null || true
# Load the plist
if launchctl load "$plist_path" 2>/dev/null; then
LogInformation "Loaded LaunchAgent for $script"
return 0
else
LogError "Failed to load LaunchAgent for $script"
return 1
fi
fi fi
else else
LogError "Failed to create plist for $script" LogError "Failed to create plist for $script"
@@ -170,7 +200,11 @@ ShowStatus() {
LogInformation "Scheduled task status:" LogInformation "Scheduled task status:"
LogInformation "OS: $OS_TYPE" LogInformation "OS: $OS_TYPE"
if [[ "$OS_TYPE" == "macos" ]]; then if [[ "$OS_TYPE" == "macos" ]]; then
LogInformation "Method: LaunchAgents" if [[ "$LAUNCHD_TYPE" == "daemon" ]]; then
LogInformation "Method: LaunchDaemons (system-wide, runs at boot)"
else
LogInformation "Method: LaunchAgents (user-level, requires login)"
fi
else else
LogInformation "Method: Crontab" LogInformation "Method: Crontab"
fi fi
@@ -213,9 +247,14 @@ ShowStatus() {
if [[ $found_entries -gt 0 ]]; then if [[ $found_entries -gt 0 ]]; then
LogInformation "Commands:" LogInformation "Commands:"
if [[ "$OS_TYPE" == "macos" ]]; then if [[ "$OS_TYPE" == "macos" ]]; then
LogInformation " List agents: launchctl list | grep servermetrics" LogInformation " List ${LAUNCHD_TYPE}s: launchctl list | grep servermetrics"
LogInformation " View logs: tail -f /tmp/*.out" if [[ "$LAUNCHD_TYPE" == "daemon" ]]; then
LogInformation " Unload agent: launchctl unload ~/Library/LaunchAgents/com.servermetrics.*.plist" LogInformation " View logs: tail -f /tmp/*.out"
LogInformation " Bootout ${LAUNCHD_TYPE}: sudo launchctl bootout system $LAUNCHD_DIR/com.servermetrics.*.plist"
else
LogInformation " View logs: tail -f /tmp/*.out"
LogInformation " Unload agent: launchctl unload ~/Library/LaunchAgents/com.servermetrics.*.plist"
fi
else else
LogInformation " View crontab: crontab -l" LogInformation " View crontab: crontab -l"
LogInformation " Edit crontab: crontab -e" LogInformation " Edit crontab: crontab -e"
@@ -230,30 +269,34 @@ RemoveCrontabEntries() {
local removed_count=0 local removed_count=0
if [[ "$OS_TYPE" == "macos" ]]; then if [[ "$OS_TYPE" == "macos" ]]; then
# Remove LaunchAgents # Remove LaunchAgents/LaunchDaemons
for script in "${SCRIPTS[@]}"; do for script in "${SCRIPTS[@]}"; do
local script_name=$(basename "$script" .sh) local script_name=$(basename "$script" .sh)
local plist_name="com.servermetrics.${script_name}.plist" local plist_name="com.servermetrics.${script_name}.plist"
local plist_path="${LAUNCHD_DIR}/${plist_name}" local plist_path="${LAUNCHD_DIR}/${plist_name}"
if [[ -f "$plist_path" ]]; then if [[ -f "$plist_path" ]]; then
LogInformation "Removing LaunchAgent for $script_name..." LogInformation "Removing Launch${LAUNCHD_TYPE^} for $script_name..."
# Unload the agent # Unload the agent/daemon
launchctl unload "$plist_path" 2>/dev/null || true if [[ "$LAUNCHD_TYPE" == "daemon" ]]; then
launchctl bootout system "$plist_path" 2>/dev/null || true
else
launchctl unload "$plist_path" 2>/dev/null || true
fi
# Remove the plist file # Remove the plist file
rm -f "$plist_path" rm -f "$plist_path"
((removed_count++)) ((removed_count++))
else else
LogDebug "No LaunchAgent found for $script_name" LogDebug "No Launch${LAUNCHD_TYPE^} found for $script_name"
fi fi
done done
if [[ $removed_count -gt 0 ]]; then if [[ $removed_count -gt 0 ]]; then
LogInformation "Removed $removed_count LaunchAgents" LogInformation "Removed $removed_count Launch${LAUNCHD_TYPE^}s"
else else
LogWarning "No LaunchAgents were found to remove" LogWarning "No Launch${LAUNCHD_TYPE^}s were found to remove"
fi fi
else else
# Remove crontab entries # Remove crontab entries