Skip to content

Commit 915faf2

Browse files
chore: Update Conda setup script path and log messages
1 parent e742c57 commit 915faf2

2 files changed

Lines changed: 52 additions & 17 deletions

File tree

cronjob.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
3+
# Define the cron job command
4+
username=$(whoami)
5+
log_dir="/home/$username/logs"
6+
mkdir -p "$log_dir"
7+
CRON_JOB="* * * * * /bin/bash $(pwd)/dashboard.sh >> $log_dir/systemdashboard_cron.log 2>&1"
8+
9+
echo "Total cron jobs before: $(crontab -l | grep -v '^#' | wc -l)"
10+
11+
# Add the cron job to the crontab
12+
(crontab -l 2>/dev/null; echo "$CRON_JOB") | crontab -
13+
14+
echo "Cron job added: $CRON_JOB"
15+
16+
# show all cron jobs ignoring comments and empty lines
17+
18+
echo "Total cron jobs after: $(crontab -l | grep -v '^#' | wc -l)"
19+
20+
21+

dashboard.sh

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,42 @@
11
#!/bin/bash
22

3+
# Function to log messages with timestamps
4+
log_message() {
5+
echo "$(date +'%Y-%m-%d %H:%M:%S') - $1" >> "$LOG_FILE"
6+
}
7+
38
# Determine the directory where this script is located
4-
SCRIPT_DIR="$(dirname "$(realpath "$0")")"
9+
SCRIPT_DIR="$(dirname "$(realpath "$0" 2>/dev/null || readlink -f "$0")")"
510

611
# Define variables for paths relative to the script's directory
712
FLASK_APP_PATH="${FLASK_APP_PATH:-$SCRIPT_DIR/app.py}"
813
REQUIREMENTS_FILE="${REQUIREMENTS_FILE:-$SCRIPT_DIR/requirements.txt}"
914
FLASK_PORT="${FLASK_PORT:-5050}"
1015
LOG_FILE="${SCRIPT_DIR}/my_script.log"
1116
USERNAME="$(whoami)"
12-
CONDA_PATH="/home/$USERNAME/miniconda3"
1317

14-
# Find the Conda setup script dynamically
15-
if [ -z "$CONDA_PATH" ]; then
16-
echo "Conda not found in PATH. Ensure Conda is installed and added to PATH." >> "$LOG_FILE"
18+
# Check for Miniconda3 and Anaconda3
19+
CONDA_PATHS=("/home/$USERNAME/miniconda3" "/home/$USERNAME/anaconda3")
20+
CONDA_FOUND=false
21+
22+
for CONDA_PATH in "${CONDA_PATHS[@]}"; do
23+
if [ -d "$CONDA_PATH" ]; then
24+
CONDA_FOUND=true
25+
break
26+
fi
27+
done
28+
29+
if [ "$CONDA_FOUND" = false ]; then
30+
log_message "Neither Miniconda3 nor Anaconda3 found. Ensure Conda is installed."
1731
exit 1
1832
fi
1933

2034
# Derive the path to the Conda setup script
21-
CONDA_SETUP_SCRIPT="/home/$USERNAME/miniconda3/etc/profile.d/conda.sh"
35+
CONDA_SETUP_SCRIPT="$CONDA_PATH/etc/profile.d/conda.sh"
2236

2337
# Check if Conda setup script exists
2438
if [ ! -f "$CONDA_SETUP_SCRIPT" ]; then
25-
echo "Conda setup script not found at $CONDA_SETUP_SCRIPT" >> "$LOG_FILE"
39+
log_message "Conda setup script not found at $CONDA_SETUP_SCRIPT."
2640
exit 1
2741
fi
2842

@@ -34,34 +48,34 @@ CONDA_ENV_NAME="systemdashboard"
3448

3549
# Check if the Conda environment exists and create it if not
3650
if ! conda env list | grep -q "$CONDA_ENV_NAME"; then
37-
echo "Conda environment '$CONDA_ENV_NAME' not found. Creating it..." >> "$LOG_FILE"
51+
log_message "Conda environment '$CONDA_ENV_NAME' not found. Creating it..."
3852
conda create -n "$CONDA_ENV_NAME" python=3.10 -y
39-
echo "Activating Conda environment '$CONDA_ENV_NAME'" >> "$LOG_FILE"
53+
log_message "Activating Conda environment '$CONDA_ENV_NAME'."
4054
conda activate "$CONDA_ENV_NAME"
41-
echo "Installing required packages" >> "$LOG_FILE"
55+
log_message "Installing required packages."
4256
pip install -r "$REQUIREMENTS_FILE"
4357
else
44-
echo "Activating existing Conda environment '$CONDA_ENV_NAME'" >> "$LOG_FILE"
58+
log_message "Activating existing Conda environment '$CONDA_ENV_NAME'."
4559
conda activate "$CONDA_ENV_NAME"
4660
fi
4761

4862
# Continue with the rest of your script
49-
echo "Conda environment '$CONDA_ENV_NAME' is active." >> "$LOG_FILE"
63+
log_message "Conda environment '$CONDA_ENV_NAME' is active."
5064

5165
# Export Flask environment variables
5266
export FLASK_APP="$FLASK_APP_PATH"
5367
export FLASK_ENV=development # or production
5468

5569
# Check if Flask app is running
5670
if ! pgrep -f "flask run --host=0.0.0.0 --port=$FLASK_PORT" > /dev/null; then
57-
# git pull on FLASK_APP_PATH directory
71+
# git pull in FLASK_APP_PATH directory
5872
current_dir=$(pwd)
59-
cd $SCRIPT_DIR
73+
cd "$SCRIPT_DIR"
6074
git stash && git pull
61-
cd $current_dir
75+
cd "$current_dir"
6276

63-
echo "Starting Flask app..." >> "$LOG_FILE"
77+
log_message "Starting Flask app..."
6478
flask run --host=0.0.0.0 --port="$FLASK_PORT" &
6579
else
66-
echo "Flask app is already running." >> "$LOG_FILE"
80+
log_message "Flask app is already running."
6781
fi

0 commit comments

Comments
 (0)