From dcbc4d9408850e3bd13cdcdad75ec6244c4ff6a0 Mon Sep 17 00:00:00 2001 From: randyjc Date: Sun, 9 Jul 2023 19:20:29 +0000 Subject: [PATCH] adding logging --- lychee-sync.sh | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/lychee-sync.sh b/lychee-sync.sh index ced6e89..231ac5e 100755 --- a/lychee-sync.sh +++ b/lychee-sync.sh @@ -1,10 +1,36 @@ #!/bin/bash +# Define log file path +LOG_FILE="/media/scripts/lychee-sync.log" + +# Function to log messages +log_message() { + local timestamp=$(date +"%Y-%m-%d %H:%M:%S") + echo "[$timestamp] $1" >> "$LOG_FILE" +} + +# Function to log errors +log_error() { + local timestamp=$(date +"%Y-%m-%d %H:%M:%S") + echo "[$timestamp] [ERROR] $1" >> "$LOG_FILE" +} + # Run the PHP command -php /app/code/artisan lychee:sync /app/code/public/uploads/import/ +log_message "Running PHP command..." +php /app/code/artisan lychee:sync /app/code/public/uploads/import/ >> "$LOG_FILE" 2>&1 +php_exit_code=$? # Check if the PHP command succeeded -if [ $? -eq 0 ]; then +if [ $php_exit_code -eq 0 ]; then + log_message "PHP command executed successfully. Deleting files and folders..." # Delete all files and folders inside the directory - rm -rf /app/code/public/uploads/import/* + rm -rf /app/code/public/uploads/import/* >> "$LOG_FILE" 2>&1 + rm_exit_code=$? + if [ $rm_exit_code -eq 0 ]; then + log_message "Files and folders deleted successfully." + else + log_error "Error deleting files and folders. Exit code: $rm_exit_code" + fi +else + log_error "Error executing PHP command. Exit code: $php_exit_code" fi