|
| 1 | +# Linux systemd setup |
| 2 | + |
| 3 | +## 1. Install and configure the bot |
| 4 | + |
| 5 | +```bash |
| 6 | +npm install -g @grinev/opencode-telegram-bot@latest |
| 7 | +opencode-telegram config |
| 8 | +``` |
| 9 | + |
| 10 | +## 2. Get the required paths |
| 11 | + |
| 12 | +```bash |
| 13 | +which node |
| 14 | +which opencode-telegram |
| 15 | +dirname "$(which node)" |
| 16 | +``` |
| 17 | + |
| 18 | +Use these values in the service file: |
| 19 | + |
| 20 | +- `<USER>`: your Linux user |
| 21 | +- `<NODE_PATH>`: output of `which node` |
| 22 | +- `<OPENCODE_TELEGRAM_PATH>`: output of `which opencode-telegram` |
| 23 | +- `<NODE_BIN_DIR>`: output of `dirname "$(which node)"` |
| 24 | + |
| 25 | +## 3. Create the service file |
| 26 | + |
| 27 | +Create `/etc/systemd/system/opencode-telegram-bot.service`: |
| 28 | + |
| 29 | +```ini |
| 30 | +[Unit] |
| 31 | +Description=OpenCode Telegram Bot |
| 32 | +After=network.target |
| 33 | + |
| 34 | +[Service] |
| 35 | +Type=simple |
| 36 | +User=<USER> |
| 37 | +Environment=PATH=<NODE_BIN_DIR>:/usr/local/bin:/usr/bin:/bin |
| 38 | +ExecStart=<NODE_PATH> <OPENCODE_TELEGRAM_PATH> start |
| 39 | +Restart=on-failure |
| 40 | +RestartSec=5 |
| 41 | + |
| 42 | +[Install] |
| 43 | +WantedBy=multi-user.target |
| 44 | +``` |
| 45 | + |
| 46 | +Run the bot in foreground mode. Do not use `--daemon` under `systemd`. |
| 47 | + |
| 48 | +## 4. Enable and start the service |
| 49 | + |
| 50 | +```bash |
| 51 | +sudo systemctl daemon-reload |
| 52 | +sudo systemctl enable opencode-telegram-bot |
| 53 | +sudo systemctl start opencode-telegram-bot |
| 54 | +sudo systemctl status opencode-telegram-bot |
| 55 | +``` |
| 56 | + |
| 57 | +## 5. View logs |
| 58 | + |
| 59 | +```bash |
| 60 | +sudo journalctl -u opencode-telegram-bot -f |
| 61 | +``` |
| 62 | + |
| 63 | +## Example |
| 64 | + |
| 65 | +This is a working example for an `nvm`-based setup: |
| 66 | + |
| 67 | +`ExecStart` does not include `start` here because `start` is the default CLI command. |
| 68 | + |
| 69 | +```ini |
| 70 | +[Unit] |
| 71 | +Description=OpenCode Telegram Bot |
| 72 | +After=network.target |
| 73 | + |
| 74 | +[Service] |
| 75 | +Type=simple |
| 76 | +User=admin |
| 77 | +Environment=PATH=/home/admin/.nvm/versions/node/v20.20.2/bin:/usr/local/bin:/usr/bin:/bin |
| 78 | +ExecStart=/home/admin/.nvm/versions/node/v20.20.2/bin/node /home/admin/.nvm/versions/node/v20.20.2/bin/opencode-telegram |
| 79 | +Restart=on-failure |
| 80 | +RestartSec=5 |
| 81 | + |
| 82 | +[Install] |
| 83 | +WantedBy=default.target |
| 84 | +``` |
0 commit comments