Files
hzdyjs/退学、休学、复学、毕业/control_scripts.sh
T
2025-12-25 18:01:27 +08:00

305 lines
7.9 KiB
Bash
Executable File

#!/bin/bash
# ===========================================
# 学生状态变更处理脚本控制程序
# 用于启动、停止、重启处理脚本
# ===========================================
set -euo pipefail
# Load configuration
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CONFIG_FILE="${SCRIPT_DIR}/data.conf"
if [[ ! -f "$CONFIG_FILE" ]]; then
echo "Error: Configuration file $CONFIG_FILE not found!"
exit 1
fi
source "$CONFIG_FILE"
# Script definitions
declare -A SCRIPTS=(
["changes"]="process_student_changes.sh"
["resume"]="process_student_resume.sh"
)
# PID files
PID_DIR="${SCRIPT_DIR}/pids"
mkdir -p "$PID_DIR"
# Functions
log() {
local level="$1"
local message="$2"
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
echo "[$timestamp] [$level] $message"
}
get_pid_file() {
local script_name="$1"
echo "${PID_DIR}/${script_name}.pid"
}
get_lock_file() {
local script_name="$1"
echo "${SCRIPT_DIR}/${script_name}.lock"
}
is_running() {
local script_name="$1"
local pid_file=$(get_pid_file "$script_name")
local lock_file=$(get_lock_file "$script_name")
# Check PID file
if [[ -f "$pid_file" ]]; then
local pid=$(cat "$pid_file")
if kill -0 "$pid" 2>/dev/null; then
return 0 # Running
else
log "WARN" "Removing stale PID file for $script_name"
rm -f "$pid_file"
fi
fi
# Check lock file
if [[ -f "$lock_file" ]]; then
local lock_pid=$(cat "$lock_file")
if kill -0 "$lock_pid" 2>/dev/null; then
return 0 # Running
else
log "WARN" "Removing stale lock file for $script_name"
rm -f "$lock_file"
fi
fi
return 1 # Not running
}
start_script() {
local script_name="$1"
local script_file="${SCRIPT_DIR}/${SCRIPTS[$script_name]}"
local pid_file=$(get_pid_file "$script_name")
if [[ ! -f "$script_file" ]]; then
log "ERROR" "Script file not found: $script_file"
return 1
fi
if is_running "$script_name"; then
log "INFO" "$script_name is already running"
return 0
fi
log "INFO" "Starting $script_name..."
chmod +x "$script_file"
# Start script in background
nohup "$script_file" > /dev/null 2>&1 &
local pid=$!
# Wait a moment for script to start
sleep 2
if kill -0 "$pid" 2>/dev/null; then
echo $pid > "$pid_file"
log "INFO" "$script_name started successfully (PID: $pid)"
return 0
else
log "ERROR" "Failed to start $script_name"
return 1
fi
}
stop_script() {
local script_name="$1"
local pid_file=$(get_pid_file "$script_name")
local lock_file=$(get_lock_file "$script_name")
local stopped=false
log "INFO" "Stopping $script_name..."
# Try to stop by PID file
if [[ -f "$pid_file" ]]; then
local pid=$(cat "$pid_file")
if kill -0 "$pid" 2>/dev/null; then
kill "$pid"
sleep 2
if ! kill -0 "$pid" 2>/dev/null; then
log "INFO" "$script_name stopped successfully (PID: $pid)"
stopped=true
fi
fi
rm -f "$pid_file"
fi
# Try to stop by lock file
if [[ -f "$lock_file" ]]; then
local lock_pid=$(cat "$lock_file")
if kill -0 "$lock_pid" 2>/dev/null; then
kill "$lock_pid"
sleep 2
if ! kill -0 "$lock_pid" 2>/dev/null; then
log "INFO" "$script_name stopped successfully (PID: $lock_pid)"
stopped=true
fi
fi
rm -f "$lock_file"
fi
if [[ "$stopped" == false ]]; then
log "WARN" "$script_name may not have been running or failed to stop gracefully"
fi
}
restart_script() {
local script_name="$1"
log "INFO" "Restarting $script_name..."
stop_script "$script_name"
sleep 3
start_script "$script_name"
}
status_script() {
local script_name="$1"
if is_running "$script_name"; then
local pid_file=$(get_pid_file "$script_name")
local lock_file=$(get_lock_file "$script_name")
if [[ -f "$pid_file" ]]; then
local pid=$(cat "$pid_file")
log "INFO" "$script_name is running (PID: $pid)"
elif [[ -f "$lock_file" ]]; then
local lock_pid=$(cat "$lock_file")
log "INFO" "$script_name is running (PID: $lock_pid)"
else
log "INFO" "$script_name status unknown"
fi
else
log "INFO" "$script_name is not running"
fi
}
show_usage() {
cat << EOF
Usage: $0 <command> <script_name>
Commands:
start Start the specified script
stop Stop the specified script
restart Restart the specified script
status Show status of the specified script
startall Start all scripts
stopall Stop all scripts
statusall Show status of all scripts
Script names:
changes Student status changes script (休学/退学/毕业)
resume Student resume script (复学)
all All scripts
Examples:
$0 start changes # Start changes processing script
$0 stop resume # Stop resume processing script
$0 restart changes # Restart changes processing script
$0 status all # Show status of all scripts
$0 startall # Start all scripts
$0 stopall # Stop all scripts
EOF
}
# Main function
main() {
local command="${1:-}"
local script_name="${2:-}"
case "$command" in
start)
if [[ "$script_name" == "all" ]]; then
for script in "${!SCRIPTS[@]}"; do
start_script "$script"
done
elif [[ -n "${SCRIPTS[$script_name]}" ]]; then
start_script "$script_name"
else
log "ERROR" "Unknown script name: $script_name"
show_usage
exit 1
fi
;;
stop)
if [[ "$script_name" == "all" ]]; then
for script in "${!SCRIPTS[@]}"; do
stop_script "$script"
done
elif [[ -n "${SCRIPTS[$script_name]}" ]]; then
stop_script "$script_name"
else
log "ERROR" "Unknown script name: $script_name"
show_usage
exit 1
fi
;;
restart)
if [[ "$script_name" == "all" ]]; then
for script in "${!SCRIPTS[@]}"; do
restart_script "$script"
done
elif [[ -n "${SCRIPTS[$script_name]}" ]]; then
restart_script "$script_name"
else
log "ERROR" "Unknown script name: $script_name"
show_usage
exit 1
fi
;;
status)
if [[ "$script_name" == "all" ]]; then
for script in "${!SCRIPTS[@]}"; do
status_script "$script"
done
elif [[ -n "${SCRIPTS[$script_name]}" ]]; then
status_script "$script_name"
else
log "ERROR" "Unknown script name: $script_name"
show_usage
exit 1
fi
;;
startall)
log "INFO" "Starting all scripts..."
for script in "${!SCRIPTS[@]}"; do
start_script "$script"
done
;;
stopall)
log "INFO" "Stopping all scripts..."
for script in "${!SCRIPTS[@]}"; do
stop_script "$script"
done
;;
statusall)
log "INFO" "Checking status of all scripts..."
for script in "${!SCRIPTS[@]}"; do
status_script "$script"
done
;;
*)
log "ERROR" "Unknown command: $command"
show_usage
exit 1
;;
esac
}
# Run main function
main "$@"