Skip to content

Commit f6e44d6

Browse files
authored
Merge pull request #12 from samuelrb/feature/add_ephemeral_run_to_doc
Ephemeral run, remove -x option in script and json logs
2 parents e220170 + 043298e commit f6e44d6

4 files changed

Lines changed: 67 additions & 16 deletions

File tree

README.md

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,50 @@ A Docker container that monitors your internet connection speed (download and up
1616

1717
## 🐳 Usage
1818

19+
### 🚀 Quickstart (Docker run)
20+
21+
#### 🔹 Using Docker Run
22+
23+
Run the container directly from Docker Hub with scheduled checks and environment configuration:
24+
25+
```bash
26+
docker run -d \
27+
--name speedtest-monitor \
28+
-e CRON_SCHEDULE="30 * * * *" \
29+
-e MIN_DOWNLOAD=270 \
30+
-e MIN_UPLOAD=260 \
31+
-e TELEGRAM_BOT_TOKEN="" \
32+
-e TELEGRAM_CHAT_ID="" \
33+
--restart unless-stopped \
34+
samuelrb/speedtest-monitor:latest
35+
```
36+
37+
#### 🔹 Using Docker Compose
38+
```bash
39+
docker compose up -d
40+
```
41+
1942
### Build
2043
```bash
21-
docker build -t speedtest-monitor .
44+
docker build -t speedtest-monitor docker
2245
```
2346
### Run simple (without telegram notifications)
2447
```bash
25-
docker run -d --restart=always --name speedtest-monitor speedtest-monitor
48+
docker run -d --restart=always --name speedtest-monitor samuelrb/speedtest-monitor
2649
```
2750
### Run with options all options (remove unused options)
2851

2952
```bash
3053
docker run -d \
31-
-e MIN_DOWNLOAD=1000 \
32-
-e MIN_UPLOAD=1000 \
54+
-e MIN_DOWNLOAD=1000 \
55+
-e MIN_UPLOAD=1000 \
3356
-e SERVER_ID=12345 \
3457
-e TELEGRAM_BOT_TOKEN="1234567:AABCC123_asdfjnnASDF" \
3558
-e TELEGRAM_CHAT_ID="12345667" \
3659
-e CRON_SCHEDULE="* * * * *" \
3760
--restart=always \
3861
--name speedtest-monitor \
39-
speedtest-monitor
62+
samuelrb/speedtest-monitor
4063
```
4164

4265
### Output
@@ -56,7 +79,7 @@ docker logs -f speedtest-monitor
5679

5780
You can receive speed alerts via Telegram if the measured download or upload speeds fall below the configured thresholds.
5881

59-
To enable this feature, set the environment variables `TELEGRAM_BOT_TOKEN` and `TELEGRAM_CHAT_ID.
82+
To enable this feature, set the environment variables `TELEGRAM_BOT_TOKEN` and `TELEGRAM_CHAT_ID`.
6083

6184
#### 📤 Example Telegram message
6285

@@ -70,10 +93,10 @@ To enable this feature, set the environment variables `TELEGRAM_BOT_TOKEN` and `
7093
### ⚙️ Environment Variables
7194

7295
| Variable | Description | Default |
73-
|---------|----------------------------------------|---------|
74-
| `MIN_DOWNLOAD` | Minimum download speed threshold (Mbps) | 200 |
75-
| `MIN_UPLOAD` | Minimum upload speed threshold (Mbps) | 200 |
76-
| `SERVER_ID` | Optional Speedtest server ID to use. | Auto |
96+
|---------|----------------------------------------|------|
97+
| `MIN_DOWNLOAD` | Minimum download speed threshold (Mbps) | 200 |
98+
| `MIN_UPLOAD` | Minimum upload speed threshold (Mbps) | 200 |
99+
| `SERVER_ID` | Optional Speedtest server ID to use. | Auto |
77100
| `TELEGRAM_BOT_TOKEN` | Your Telegram bot's token | void |
78101
| `TELEGRAM_CHAT_ID` | Your personal chat ID or group chat ID | void |
79102
| `CRON_SCHEDULE` | The cron expression to schedule script | 0 * * * * |
@@ -90,6 +113,28 @@ By default, Speedtest runs every hour. You can modify the schedule by editing th
90113
0 * * * * /usr/local/bin/speedtest-check.sh >> /proc/1/fd/1 2>&1
91114
```
92115

116+
## 🔹 Using Host Cron (ephemeral container)
117+
This method allows the container to run only when scheduled by the host system. Ideal for minimizing resource usage.
118+
119+
Create a cron job on your machine (host) with the following line:
120+
121+
```bash
122+
0 * * * * docker run -d --rm \
123+
-e MIN_DOWNLOAD=270 \
124+
-e MIN_UPLOAD=260 \
125+
-e TELEGRAM_BOT_TOKEN="your_bot_token" \
126+
-e TELEGRAM_CHAT_ID="your_chat_id" \
127+
samuelrb/speedtest-monitor:latest /usr/local/bin/speedtest-check.sh
128+
```
129+
130+
Save it with `crontab -e`
131+
132+
No `CRON_SCHEDULE` is needed, since the scheduling is external.
133+
134+
This overrides the default CMD and directly executes the check script.
135+
136+
✅ This is the most lightweight approach — Docker runs only during the test, then shuts down.
137+
93138
## 🧪 Tests (Optional)
94139
To validate the container before publishing, you can test the script with:
95140

docker/scripts/init.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/bin/bash
22
set -e
3-
set -x
43

54
export CRON_SCHEDULE=${CRON_SCHEDULE:-"0 * * * *"}
65

docker/scripts/speedtest-check.sh

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
#!/bin/bash
22
set -e
3-
set -x
43

54
set -a
65
[ -f /etc/environment ] && . /etc/environment
76
set +a
87

8+
echo_log() {
9+
local level="${2:-info}"
10+
local timestamp
11+
timestamp=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
12+
13+
echo "{\"timestamp\":\"$timestamp\",\"level\":\"$level\",\"message\":\"$1\"}"
14+
}
15+
916
MIN_DOWNLOAD=${MIN_DOWNLOAD:-200}
1017
MIN_UPLOAD=${MIN_UPLOAD:-200}
1118
SERVER_ID=${SERVER_ID:-}
@@ -25,12 +32,12 @@ RESULT_URL=$(echo "${RESULT}" | jq -r '.result.url')
2532
DOWNLOAD_MBPS=$(echo "scale=2; ${DOWNLOAD_BPS} * 8 / 1000000" | bc -l)
2633
UPLOAD_MBPS=$(echo "scale=2; ${UPLOAD_BPS} * 8 / 1000000" | bc -l)
2734

28-
echo "${DATE} - Result: ${RESULT}"
29-
echo "${DATE} - Download: ${DOWNLOAD_MBPS} Mbps, Upload: ${UPLOAD_MBPS} Mbps"
35+
echo_log "Result: ${RESULT}"
36+
echo_log "Download: ${DOWNLOAD_MBPS} Mbps, Upload: ${UPLOAD_MBPS} Mbps"
3037

3138
if (( $(echo "${DOWNLOAD_MBPS} < ${MIN_DOWNLOAD}" | bc -l) )) || \
3239
(( $(echo "${UPLOAD_MBPS} < ${MIN_UPLOAD}" | bc -l) )); then
33-
echo "${DATE} - ⚠️ Warning: low speed (Download: ${DOWNLOAD_MBPS}, Upload: ${UPLOAD_MBPS})
40+
echo_log "⚠️ Warning: low speed (Download: ${DOWNLOAD_MBPS}, Upload: ${UPLOAD_MBPS})
3441
threshold (Download: ${MIN_DOWNLOAD}, Upload: ${MIN_UPLOAD})"
3542
if [[ -n "${TELEGRAM_BOT_TOKEN}" && -n "${TELEGRAM_CHAT_ID}" ]]; then
3643
ALERT_MSG=$(cat <<EOF

version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.3.0
1+
1.4.0

0 commit comments

Comments
 (0)