← back to Dear Bubbe Nextjs
lib/instagram-cron.sh
54 lines
#!/bin/bash
# Instagram Automated Posting Script
# Runs 3 times per day at optimal posting times
LOG_DIR="/root/Projects/dear-bubbe-admin/logs"
SCRIPT_PATH="/root/Projects/dear-bubbe-nextjs/lib/instagram-poster.js"
LOCK_FILE="/tmp/instagram-poster.lock"
# Create log directory if it doesn't exist
mkdir -p "$LOG_DIR"
# Function to log messages
log_message() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> "$LOG_DIR/instagram-cron.log"
}
# Check if another instance is running
if [ -f "$LOCK_FILE" ]; then
PID=$(cat "$LOCK_FILE")
if ps -p "$PID" > /dev/null 2>&1; then
log_message "Another instance is already running (PID: $PID)"
exit 0
else
log_message "Removing stale lock file"
rm "$LOCK_FILE"
fi
fi
# Create lock file
echo $$ > "$LOCK_FILE"
# Run the Instagram poster
log_message "Starting Instagram post attempt"
cd /root/Projects/dear-bubbe-nextjs
# Set timeout to 5 minutes to prevent hanging
timeout 300 node "$SCRIPT_PATH" >> "$LOG_DIR/instagram-cron.log" 2>&1
EXIT_CODE=$?
if [ $EXIT_CODE -eq 0 ]; then
log_message "Instagram post completed successfully"
elif [ $EXIT_CODE -eq 124 ]; then
log_message "Instagram post timed out after 5 minutes"
else
log_message "Instagram post failed with exit code: $EXIT_CODE"
fi
# Remove lock file
rm -f "$LOCK_FILE"
log_message "Instagram cron job completed"