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
712FLASK_APP_PATH=" ${FLASK_APP_PATH:- $SCRIPT_DIR / app.py} "
813REQUIREMENTS_FILE=" ${REQUIREMENTS_FILE:- $SCRIPT_DIR / requirements.txt} "
914FLASK_PORT=" ${FLASK_PORT:- 5050} "
1015LOG_FILE=" ${SCRIPT_DIR} /my_script.log"
1116USERNAME=" $( 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
1832fi
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
2438if [ ! -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
2741fi
2842
@@ -34,34 +48,34 @@ CONDA_ENV_NAME="systemdashboard"
3448
3549# Check if the Conda environment exists and create it if not
3650if ! 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 "
4357else
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 "
4660fi
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
5266export FLASK_APP=" $FLASK_APP_PATH "
5367export FLASK_ENV=development # or production
5468
5569# Check if Flask app is running
5670if ! 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 " &
6579else
66- echo " Flask app is already running." >> " $LOG_FILE "
80+ log_message " Flask app is already running."
6781fi
0 commit comments