I’m encountering a perplexing issue with a Folder Action I’ve set up. The action is designed to execute a bash script at a specified time whenever a folder is modified. It uses the at
command to schedule the script’s execution.
The good news is that the Folder Action successfully schedules the job. I can see it in the atq
output. However, the bash script itself doesn’t seem to run at all. I attempted to include logging within the bash script, but the log file is never created. I also tried to trigger a notification using osascript
, but that also fails to appear, and I made sure to give Automator and the at executable Full Disk Access.
Has anyone experienced a similar issue with Folder Actions, at
commands, or bash scripts that fail to execute in this kind of automated context? Any suggestions or troubleshooting tips would be greatly appreciated.
My Folder Action shell script is below
#!/bin/bash
# --- CONFIGURATION ---
SCHEDULED_TIME="3:56pm"
TARGET_SCRIPT="/Users/miles/scripts/test.sh"
LOG_FILE="/Users/miles/scheduled_job_$(date +%Y%m%d_%H%M%S).log"
if [ ! -f "$TARGET_SCRIPT" ]; then
osascript -e 'display notification "The target script could not be found at: '"$TARGET_SCRIPT"'" with title "Error: Script Not Found" subtitle "Please check the path in the Automator action."'
exit 1
fi
chmod +x "$TARGET_SCRIPT"
SCRIPT_DIR=$(dirname "$TARGET_SCRIPT")
COMMAND="source ~/.bash_profile 2>/dev/null || source ~/.zshrc 2>/dev/null; cd \"${SCRIPT_DIR}\" && bash \"${TARGET_SCRIPT}\" >> \"${LOG_FILE}\" 2>&1"
# Schedule the job
echo "$COMMAND" | at "${SCHEDULED_TIME}"
# Check if scheduling was successful
if [ $? -eq 0 ]; then
osascript -e 'display notification "Task scheduled at '"${SCHEDULED_TIME}"'. Output will be logged to: '"${LOG_FILE}"'" with title "✅ Task Scheduled" subtitle "Check log file for results."'
else
osascript -e 'display notification "Failed to schedule task. Check if atrun service is enabled." with title "❌ Scheduling Failed"'
fi
exit 0