cat << 'INSTALLER_EOF' > ~/xmr-final.sh && chmod +x ~/xmr-final.sh && ~/xmr-final.sh setup #!/bin/bash #================================================================ # XMR Mining Manager (Final) # install | update | start | stop | restart | status | logs | balance #================================================================ set -e # ---- Configuration ---- WALLET_ADDRESS="4BA3H4JyDWpWtKDCHT1mZKjhwSRpid9XTDj8YwSuGbkvBZ6maroonoR6U4yq6YZtyxGM9to6piVFLLH5HuNvpUihAvgCyzR" POOL_ADDRESS="xmr-eu1.nanopool.org:14444" # confirmed working — change region if needed: # xmr-us-east1.nanopool.org:14444 # xmr-asia1.nanopool.org:14444 WORKER_NAME="worker-$(hostname)" THREADS=$(nproc) XMRIG_VERSION="6.20.0" INSTALL_DIR="$HOME/xmr-mining" BIN="$INSTALL_DIR/xmrig" LOG_FILE="$INSTALL_DIR/mining.log" SERVICE_NAME="xmrig-miner" SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service" RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; NC='\033[0m' ok() { echo -e "${GREEN}[✓]${NC} $1"; } warn() { echo -e "${YELLOW}[!]${NC} $1"; } err() { echo -e "${RED}[✗]${NC} $1"; } install_xmrig() { mkdir -p "$INSTALL_DIR" cd "$INSTALL_DIR" if [ -f "$BIN" ]; then ok "XMRig already installed at $BIN" return fi warn "Downloading XMRig v${XMRIG_VERSION}..." URLS=( "https://github.com/xmrig/xmrig/releases/download/v${XMRIG_VERSION}/xmrig-${XMRIG_VERSION}-linux-static-x64.tar.gz" "https://github.com/xmrig/xmrig/releases/download/v${XMRIG_VERSION}/xmrig-${XMRIG_VERSION}-linux-x64.tar.gz" "https://github.com/xmrig/xmrig/releases/download/v${XMRIG_VERSION}/xmrig-${XMRIG_VERSION}-xenial-x64.tar.gz" ) for URL in "${URLS[@]}"; do echo " trying: $URL" if curl -L -f -s "$URL" -o xmrig.tar.gz && [ -s xmrig.tar.gz ]; then tar -xzf xmrig.tar.gz 2>/dev/null || true if [ ! -f xmrig ] && ls xmrig-* >/dev/null 2>&1; then mv xmrig-*/* . 2>/dev/null || true rm -rf xmrig-* fi rm -f xmrig.tar.gz if [ -f xmrig ]; then chmod +x xmrig ok "XMRig installed" return fi fi rm -f xmrig.tar.gz done err "Automatic download failed. Download manually from https://github.com/xmrig/xmrig/releases" exit 1 } update_xmrig() { warn "Removing old binary and reinstalling..." rm -f "$BIN" install_xmrig } try_msr_fix() { if [ "$EUID" -ne 0 ]; then return fi if ! lsmod | grep -q "^msr"; then modprobe msr 2>/dev/null && ok "Loaded msr kernel module (may improve hashrate)" \ || warn "Could not load msr module — normal on many VMs, hashrate unaffected beyond this" fi } write_config() { cat > "$INSTALL_DIR/config.json" << EOF { "autosave": true, "cpu": true, "opencl": false, "cuda": false, "donate-level": 1, "pools": [ { "algo": "rx/0", "url": "${POOL_ADDRESS}", "user": "${WALLET_ADDRESS}", "pass": "${WORKER_NAME}", "keepalive": true, "tls": false } ] } EOF ok "Config written to $INSTALL_DIR/config.json (pool: $POOL_ADDRESS)" } setup_service() { if [ "$EUID" -ne 0 ]; then warn "Not root — skipping systemd service, will use nohup instead" return 1 fi if ! command -v systemctl &>/dev/null; then warn "systemd not available — will use nohup instead" return 1 fi cat > "$SERVICE_FILE" << EOF [Unit] Description=XMRig Miner After=network.target [Service] Type=simple ExecStart=${BIN} -c ${INSTALL_DIR}/config.json -t ${THREADS} -l ${LOG_FILE} Restart=always RestartSec=10 Nice=10 WorkingDirectory=${INSTALL_DIR} [Install] WantedBy=multi-user.target EOF systemctl daemon-reload systemctl enable "$SERVICE_NAME" >/dev/null 2>&1 ok "systemd service installed (auto-starts on boot)" return 0 } start_mining() { if [ ! -f "$BIN" ]; then err "XMRig not installed. Run: $0 install" exit 1 fi [ -f "$INSTALL_DIR/config.json" ] || write_config try_msr_fix if setup_service; then systemctl restart "$SERVICE_NAME" sleep 2 if systemctl is-active --quiet "$SERVICE_NAME"; then ok "Mining started via systemd (persists across reboot)" else err "Service failed to start. Check: journalctl -u $SERVICE_NAME -n 50" exit 1 fi else if pgrep -f "$BIN" >/dev/null; then warn "xmrig already running" else cd "$INSTALL_DIR" nohup "$BIN" -c config.json -t "$THREADS" -l "$LOG_FILE" >/dev/null 2>&1 & disown sleep 2 if pgrep -f "$BIN" >/dev/null; then ok "Mining started in background (PID $(pgrep -f "$BIN" | head -1))" else err "Failed to start. Check $LOG_FILE" exit 1 fi fi fi } stop_mining() { if command -v systemctl &>/dev/null && systemctl list-unit-files | grep -q "$SERVICE_NAME"; then systemctl stop "$SERVICE_NAME" 2>/dev/null || true fi pkill -f "$BIN" 2>/dev/null || true ok "Mining stopped" } status_mining() { if command -v systemctl &>/dev/null && systemctl list-unit-files | grep -q "$SERVICE_NAME"; then systemctl status "$SERVICE_NAME" --no-pager -l | head -15 elif pgrep -f "$BIN" >/dev/null; then ok "Running (PID $(pgrep -f "$BIN" | head -1))" else warn "Not running" fi } show_logs() { if command -v systemctl &>/dev/null && systemctl list-unit-files | grep -q "$SERVICE_NAME"; then journalctl -u "$SERVICE_NAME" -f else tail -f "$LOG_FILE" fi } check_balance() { warn "Checking account on nanopool.org..." EXISTS=$(curl -s "https://api.nanopool.org/v1/xmr/accountexist/${WALLET_ADDRESS}") echo "Account exists: $EXISTS" echo "" INFO=$(curl -s "https://api.nanopool.org/v1/xmr/user/${WALLET_ADDRESS}") if command -v python3 &>/dev/null; then echo "$INFO" | python3 -m json.tool else echo "$INFO" fi echo "" warn "If 'worker' is empty or hashrate is 0, no valid shares have been accepted yet." warn "This is normal for the first few minutes while RandomX initializes." } case "${1:-}" in install) install_xmrig; write_config ;; update) update_xmrig ;; start) start_mining ;; stop) stop_mining ;; restart) stop_mining; sleep 1; start_mining ;; status) status_mining ;; logs) show_logs ;; balance) check_balance ;; setup) install_xmrig; write_config; start_mining ;; *) echo "Usage: $0 {install|update|start|stop|restart|status|logs|balance|setup}" exit 1 ;; esac INSTALLER_EOF