-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.sh
More file actions
123 lines (104 loc) · 4.15 KB
/
Copy pathrun.sh
File metadata and controls
123 lines (104 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/bin/bash
# Definizione delle variabili di configurazione
LOG_DIR="database/log"
CONDA_ENV="myenv"
WORK_DIR="database"
# Imposta il comando Python - in Windows potrebbe essere 'python' invece di 'python3'
PYTHON_CMD="python"
# Funzione per la gestione degli errori
handle_error() {
echo "ERRORE: $1"
# Invia anche un messaggio di log, se possibile
if [ -d "$LOG_DIR" ]; then
echo "$(date '+%Y-%m-%d %H:%M:%S') - ERRORE: $1" >> "$LOG_DIR/run_script.log"
fi
exit "${2:-1}" # Codice di uscita predefinito 1 se non specificato
}
# Stampa informazioni di avvio
echo "==================================================="
echo "Avvio script $(date '+%Y-%m-%d %H:%M:%S')"
echo "Directory corrente: $(pwd)"
echo "==================================================="
# Verifica che la directory di log esista
if [ ! -d "$LOG_DIR" ]; then
mkdir -p "$LOG_DIR" || handle_error "Impossibile creare la directory di log: $LOG_DIR" 1
echo "Directory di log creata: $LOG_DIR"
else
echo "Directory di log esistente: $LOG_DIR"
fi
# Funzione per verificare se un comando esiste
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Verifica che conda sia disponibile
if ! command_exists conda; then
if [ -f "$HOME/anaconda3/etc/profile.d/conda.sh" ]; then
echo "Sourcing conda da anaconda3..."
source "$HOME/anaconda3/etc/profile.d/conda.sh"
elif [ -f "$HOME/miniconda3/etc/profile.d/conda.sh" ]; then
echo "Sourcing conda da miniconda3..."
source "$HOME/miniconda3/etc/profile.d/conda.sh"
else
handle_error "Conda non trovato. Assicurati che sia installato e disponibile nel PATH." 1
fi
fi
# Elimina i log più vecchi di 3 giorni
echo "Pulizia dei file di log vecchi..."
find "$LOG_DIR" -name "*.log" -type f -mtime +3 -exec rm -f {} \; ||
echo "Attenzione: problemi nella rimozione dei file di log vecchi. Continuo comunque."
# Verifica che l'ambiente conda esista
conda info --envs | grep -q "$CONDA_ENV" ||
handle_error "L'ambiente conda '$CONDA_ENV' non esiste" 1
# Attiva l'ambiente conda
echo "Attivazione dell'ambiente conda: $CONDA_ENV"
eval "$(conda shell.bash hook)"
conda activate "$CONDA_ENV" || handle_error "Errore nell'attivazione dell'ambiente Conda: $CONDA_ENV" 1
# Verifica quale comando Python è disponibile nell'ambiente
echo "Verifico il comando Python disponibile..."
if command_exists python; then
PYTHON_CMD="python"
echo "Trovato comando 'python'"
elif command_exists python3; then
PYTHON_CMD="python3"
echo "Trovato comando 'python3'"
else
handle_error "Nessun comando Python disponibile nell'ambiente $CONDA_ENV" 1
fi
# Verifica la versione di Python
$PYTHON_CMD --version || handle_error "Errore nell'esecuzione di $PYTHON_CMD" 1
echo "Utilizzerò il comando Python: $PYTHON_CMD"
# Verifica che la directory di destinazione esista
if [ ! -d "$WORK_DIR" ]; then
handle_error "La directory di destinazione non esiste: $WORK_DIR" 1
fi
# Cambia directory al percorso specificato
echo "Cambio directory a: $WORK_DIR"
cd "$WORK_DIR" || handle_error "Impossibile accedere alla directory: $WORK_DIR" 1
echo "Directory di lavoro attuale: $(pwd)"
# Verifica che main.py esista nella directory corrente
if [ ! -f "main.py" ]; then
handle_error "Il file main.py non esiste nella directory $(pwd)" 1
fi
# Esecuzione dello script Python
echo "Avvio dello script Python..."
$PYTHON_CMD main.py
PY_RESULT=$?
echo "Script Python terminato con codice: $PY_RESULT"
# Disattiva l'ambiente conda
echo "Disattivazione dell'ambiente conda..."
conda deactivate
# Gestione del risultato dello script Python
if [ $PY_RESULT -ne 0 ]; then
if [ $PY_RESULT -eq 1 ]; then
handle_error "Errore nell'esecuzione dello script Python (codice: $PY_RESULT)" $PY_RESULT
elif [ $PY_RESULT -eq 2 ]; then
echo "Script Python terminato con codice di uscita 2"
exit $PY_RESULT
else
handle_error "Script Python terminato con codice di uscita non previsto: $PY_RESULT" $PY_RESULT
fi
fi
echo "==================================================="
echo "Esecuzione completata con successo $(date '+%Y-%m-%d %H:%M:%S')"
echo "==================================================="
exit 0