telegram bot: launchd install/uninstall scripts for macOS

This commit is contained in:
Azreen Jamal
2026-03-03 04:16:27 +08:00
parent 5f8f7a1591
commit dd2ce7e81f
5 changed files with 87 additions and 0 deletions
+1
View File
@@ -32,3 +32,4 @@ report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
# Finder (MacOS) folder config # Finder (MacOS) folder config
.DS_Store .DS_Store
bot.log\nbot.err
+24
View File
@@ -0,0 +1,24 @@
9 | const SSH_USER = process.env.SSH_USER || "root";
10 | const SSH_HOST = process.env.SSH_HOST!;
11 | const SSH_PORT = process.env.SSH_PORT || "22";
12 | const ALLOWED_USERS = process.env.TELEGRAM_ALLOWED_USERS?.split(",").map(Number) || [];
13 |
14 | if (!BOT_TOKEN) throw new Error("TELEGRAM_BOT_TOKEN missing from .env");
^
error: TELEGRAM_BOT_TOKEN missing from .env
at /Users/azreenjamal/pi-vs-claude-code/telegram-bot/index.ts:14:27
at loadAndEvaluateModule (2:1)
Bun v1.3.10 (macOS arm64)
9 | const SSH_USER = process.env.SSH_USER || "root";
10 | const SSH_HOST = process.env.SSH_HOST!;
11 | const SSH_PORT = process.env.SSH_PORT || "22";
12 | const ALLOWED_USERS = process.env.TELEGRAM_ALLOWED_USERS?.split(",").map(Number) || [];
13 |
14 | if (!BOT_TOKEN) throw new Error("TELEGRAM_BOT_TOKEN missing from .env");
^
error: TELEGRAM_BOT_TOKEN missing from .env
at /Users/azreenjamal/pi-vs-claude-code/telegram-bot/index.ts:14:27
at loadAndEvaluateModule (2:1)
Bun v1.3.10 (macOS arm64)
+3
View File
@@ -0,0 +1,3 @@
🚀 Bot starting...
✅ @cr_management_smart_bot is live
🔧 [775071081] run_command: free -h
+54
View File
@@ -0,0 +1,54 @@
#!/bin/bash
# Install telegram bot as a macOS launchd service (auto-start on boot)
set -e
BOT_DIR="$(cd "$(dirname "$0")" && pwd)"
PROJECT_DIR="$(dirname "$BOT_DIR")"
BUN_PATH="$(which bun)"
PLIST_NAME="com.cr.telegram-bot"
PLIST_PATH="$HOME/Library/LaunchAgents/${PLIST_NAME}.plist"
if [ -z "$BUN_PATH" ]; then
echo "❌ bun not found in PATH"
exit 1
fi
if [ ! -f "$PROJECT_DIR/.env" ]; then
echo "❌ .env not found at $PROJECT_DIR/.env"
exit 1
fi
# Unload if already installed
launchctl unload "$PLIST_PATH" 2>/dev/null || true
cat > "$PLIST_PATH" <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>${PLIST_NAME}</string>
<key>ProgramArguments</key>
<array>
<string>/bin/bash</string>
<string>-c</string>
<string>source ${PROJECT_DIR}/.env && exec ${BUN_PATH} run ${BOT_DIR}/index.ts</string>
</array>
<key>WorkingDirectory</key>
<string>${BOT_DIR}</string>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>StandardOutPath</key>
<string>${BOT_DIR}/bot.log</string>
<key>StandardErrorPath</key>
<string>${BOT_DIR}/bot.err</string>
</dict>
</plist>
EOF
launchctl load "$PLIST_PATH"
echo "✅ Installed and started: ${PLIST_NAME}"
echo " Logs: ${BOT_DIR}/bot.log"
echo " Stop: launchctl unload ${PLIST_PATH}"
+5
View File
@@ -0,0 +1,5 @@
#!/bin/bash
# Remove telegram bot launchd service
PLIST_PATH="$HOME/Library/LaunchAgents/com.cr.telegram-bot.plist"
launchctl unload "$PLIST_PATH" 2>/dev/null && echo "✅ Stopped and unloaded" || echo "⚠️ Not loaded"
rm -f "$PLIST_PATH" && echo "✅ Removed plist" || true