🚀 Chạy Script Khi Khởi Động Linux
📋 Giới Thiệu
Systemd là hệ thống quản lý dịch vụ trong các bản phân phối Linux hiện đại như Ubuntu, Debian, CentOS và Fedora. Bài viết này sẽ hướng dẫn cách cấu hình một script để chạy tự động khi khởi động lại máy.
🛠️ Các Bước Thực Hiện
1. Tạo Script
Đầu tiên, tạo một script trong thư mục /usr/local/bin/
:
sudo nano /usr/local/bin/startup_script.sh
Thêm nội dung script:
#!/bin/bash
# Định nghĩa biến
LOG_FILE="/var/log/startup_script.log"
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')
# Ghi log khi script được thực thi
echo "[$TIMESTAMP] Script started" >> $LOG_FILE
# Thêm các tác vụ của bạn ở đây
# Ví dụ:
# - Khởi động service
# - Kiểm tra hệ thống
# - Gửi thông báo
# Ghi log kết thúc
echo "[$TIMESTAMP] Script completed" >> $LOG_FILE
Phân quyền thực thi cho script:
sudo chmod +x /usr/local/bin/startup_script.sh
2. Tạo Systemd Service
Tạo file service trong /etc/systemd/system/
:
sudo nano /etc/systemd/system/startup_script.service
Thêm cấu hình service:
[Unit]
Description=Startup Script Service
After=network.target
[Service]
Type=oneshot
ExecStart=/usr/local/bin/startup_script.sh
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
Giải thích cấu hình
Description
: Mô tả về serviceAfter
: Chỉ định service này sẽ chạy sau khi network đã sẵn sàngType=oneshot
: Service sẽ chạy một lần và kết thúcExecStart
: Đường dẫn tới script cần thực thiRemainAfterExit
: Giữ service ở trạng thái active sau khi script kết thúcWantedBy
: Chỉ định target mà service này thuộc về
3. Kích Hoạt Service
# Reload systemd để nhận cấu hình mới
sudo systemctl daemon-reload
# Kích hoạt service để chạy khi khởi động
sudo systemctl enable startup_script.service
# Khởi động service (để test)
sudo systemctl start startup_script.service
4. Kiểm Tra Trạng Thái
# Xem trạng thái service
sudo systemctl status startup_script.service
# Xem log của script
tail -f /var/log/startup_script.log
# Xem log của systemd
journalctl -u startup_script.service
⚙️ Tùy Chỉnh Nâng Cao
1. Thêm Dependencies
Nếu script cần chạy sau một số service khác:
[Unit]
Description=Startup Script Service
After=network.target postgresql.service mongodb.service
Requires=postgresql.service
2. Thêm Retry Logic
#!/bin/bash
# Thêm logic retry vào script
MAX_RETRIES=3
RETRY_DELAY=5
function retry_command() {
local n=1
local max=$MAX_RETRIES
local delay=$RETRY_DELAY
while true; do
"$@" && break || {
if [[ $n -lt $max ]]; then
((n++))
echo "Command failed. Attempt $n/$max:"
sleep $delay;
else
echo "The command has failed after $n attempts."
return 1
fi
}
done
}
# Sử d ụng function retry
retry_command your_command
3. Thêm Timeout
[Service]
Type=oneshot
ExecStart=/usr/local/bin/startup_script.sh
TimeoutStartSec=300
🔍 Xử Lý Sự Cố
1. Service Không Chạy
- Kiểm tra quyền thực thi của script
- Xem log systemd
- Kiểm tra syntax trong file service
# Kiểm tra log
journalctl -xe
2. Script Chạy Quá Sớm
- Thêm dependencies vào section [Unit]
- Sử dụng sleep trong script
- Kiểm tra order của systemd units
3. Lỗi Permission
- Kiểm tra SELinux context
- Xem quyền truy cập file
- Kiểm tra user executing script