-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
discordian.sh
executable file
·95 lines (83 loc) · 2.98 KB
/
discordian.sh
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
#!/bin/bash
# Define PATH if running from crontab. Adjust as necessary.
# PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
# Script for launching a DiscordianAI bot with customizable configurations.
# Suitable for both manual execution and running via crontab.
# Enable strict error checking: Exit on any error and recognize failures in pipelines.
set -e
set -o pipefail
# Logging function with timestamp for better tracking
log() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1"
}
# Find and log the path to the python3 executable.
python3="$(which python3)"
log "Using python3 binary: $python3"
# Default configuration and output settings
config_file="bot.ini" # Default configuration file.
output="" # No output redirection initially.
base_folder="" # Use current directory by default.
# Process command line arguments to adjust script settings
while [[ "$#" -gt 0 ]]; do
case "$1" in
-d|--daemon)
# Daemon mode: No output to terminal
output="/dev/null"
log "Running in daemon mode. Output redirected to $output"
;;
-c|--config)
# Custom configuration file: Ensure argument is provided
if [[ -n "$2" && ! "$2" =~ ^- ]]; then
config_file="$2"
shift # Skip next argument
log "Using custom config file: $config_file"
else
log "Error: Configuration file argument required."
exit 1
fi
;;
-f|--folder)
# Base folder: Ensure argument is a valid path
if [[ -n "$2" && ! "$2" =~ ^- ]]; then
base_folder="$(realpath "$2")"
shift # Skip next argument
log "Using base folder: $base_folder"
else
log "Error: Folder path argument required."
exit 1
fi
;;
*)
# Ignore unrecognized options
;;
esac
shift # Move to next argument
done
# Construct the command with the base folder and config file.
# If base_folder is set, prepend it to both bot.py and config file paths.
if [[ -n $base_folder ]]; then
config_file="$base_folder/$config_file"
command="$python3 $base_folder/bot.py --conf $config_file"
else
command="$python3 bot.py --conf $config_file"
fi
log "Command to execute: $command"
# Enhanced error handling
trap 'log "An error occurred. Exiting..."; exit 1' ERR
# Terminate existing instances of the bot
log "Killing existing instances of the bot..."
pkill -f "bot.py --conf" || true
# Decide execution mode based on output setting
if [[ -z $output ]]; then
# Normal execution
eval "$command"
else
# Daemon execution
log "Running command in background: $command > $output 2>&1 &"
if ! ($command > "$output" 2>&1 &); then
log "Error: Failed to execute command."
exit 1
fi
fi
# Log successful execution completion
log "Script execution completed successfully."