Chuyển đến nội dung
Skillpedia
Triển khai và đám mây (DevOps)48k · #5919

systemd-services

Skill này làm gì

Công cụ này giúp quản lý các chương trình chạy nền trên máy tính Linux, gọi là “service”. Bạn có thể tạo, khởi động, dừng và cấu hình các service này để chúng tự chạy khi máy tính khởi động.

Khi nào nên dùng

  • ✓Khi bạn muốn một chương trình chạy liên tục mà không cần mở nó.
  • ✓Để lên lịch các tác vụ chạy vào thời điểm cụ thể, như sao lưu.
  • ✓Để cấu hình cách một chương trình dùng tài nguyên máy tính (bộ nhớ, bộ xử lý).

Liên kết skill

https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/systemd-services/SKILL.md

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/systemd-services/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Thiết lập chương trình “my_app” tự khởi động khi máy tính bật và tự khởi động lại nếu bị lỗi.”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Bạn viết một tệp cấu hình mô tả chương trình và cách nó nên chạy. Sau đó, bạn dùng các lệnh đơn giản để yêu cầu hệ thống khởi động, dừng hoặc quản lý chương trình này.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

Systemd Services

Create, manage, and monitor systemd services and timers. Covers unit file authoring, dependency management, socket activation, resource limits, journalctl log analysis, and production hardening.

When to Use

  • Deploying an application as a managed background service
  • Replacing cron jobs with systemd timers for better logging and dependency control
  • Setting up socket activation for on-demand service startup
  • Configuring resource limits (CPU, memory, I/O) for services
  • Debugging service startup failures and runtime crashes
  • Managing service dependencies and ordering

Prerequisites

  • Linux system running systemd (most modern distributions)
  • Root or sudo access for creating system-level unit files
  • Application binary or script to run as a service
  • Understanding of the application's start/stop lifecycle

Service Unit File -- Complete Example

# /etc/systemd/system/myapp.service
[Unit]
Description=MyApp Production Server
Documentation=https://docs.example.com/myapp
After=network-online.target postgresql.service
Wants=network-online.target
Requires=postgresql.service

[Service]
Type=notify
User=myapp
Group=myapp
WorkingDirectory=/opt/myapp

# Environment configuration
EnvironmentFile=/etc/myapp/env
Environment=NODE_ENV=production
Environment=PORT=8080

# Execution
ExecStartPre=/opt/myapp/bin/migrate --check
ExecStart=/opt/myapp/bin/server --config /etc/myapp/config.yaml
ExecStartPost=/opt/myapp/bin/healthcheck.sh
ExecReload=/bin/kill -HUP $MAINPID
ExecStop=/opt/myapp/bin/graceful-stop.sh

# Restart behavior
Restart=on-failure
RestartSec=5
StartLimitIntervalSec=300
StartLimitBurst=5

# Timeouts
TimeoutStartSec=30
TimeoutStopSec=30
WatchdogSec=60

# Security hardening
NoNewPrivileges=true
ProtectSystem=strict
ProtectHome=true
PrivateTmp=true
ReadWritePaths=/var/lib/myapp /var/log/myapp
CapabilityBoundingSet=
AmbientCapabilities=

# Logging
StandardOutput=journal
StandardError=journal
SyslogIdentifier=myapp

[Install]
WantedBy=multi-user.target

Service Management Commands

# Reload systemd after creating or modifying unit files
systemctl daemon-reload

# Start, stop, restart a service
systemctl start myapp
systemctl stop myapp
systemctl restart myapp

# Reload service configuration without restart (if supported)
systemctl reload myapp

# Enable service to start on boot
systemctl enable myapp

# Enable and start in one command
systemctl enable --now myapp

# Disable and stop
systemctl disable --now myapp

# Check service status
systemctl status myapp

# Check if a service is active, enabled, or failed
systemctl is-active myapp
systemctl is-enabled myapp
systemctl is-failed myapp

# List all running services
systemctl list-units --type=service --state=running

# List all failed services
systemctl list-units --type=service --state=failed

# Show all properties of a service
systemctl show myapp

# Show specific property values
systemctl show myapp -p MainPID,MemoryCurrent,CPUUsageNSec

# Mask a service (prevent it from being started at all)
systemctl mask myapp

# Unmask
systemctl unmask myapp

# Reset a failed service state
systemctl reset-failed myapp

Timer Units (Cron Replacement)

Timer File

# /etc/systemd/system/backup.timer
[Unit]
Description=Daily backup timer

[Timer]
# Run daily at 2:30 AM
OnCalendar=*-*-* 02:30:00
# If the system was off at the scheduled time, run when it boots
Persistent=true
# Add random delay up to 15 minutes to avoid thundering herd
RandomizedDelaySec=900
# Associate with a specific service (defaults to same name .service)
Unit=backup.service

[Install]
WantedBy=timers.target

Corresponding Service File

# /etc/systemd/system/backup.service
[Unit]
Description=Daily backup job
After=network-online.target
Wants=network-online.target

[Service]
Type=oneshot
User=backup
ExecStart=/usr/local/bin/run-backup.sh
StandardOutput=journal
StandardError=journal

Timer Management

# Common OnCalendar expressions:
# minutely, hourly, daily, weekly, monthly
# *-*-* 06:00:00       Daily at 6 AM
# Mon..Fri *-*-* 09:00 Weekdays at 9 AM
# *:0/15               Every 15 minutes

# Validate calendar expressions
systemd-analyze calendar "Mon..Fri *-*-* 09:00"

# List all active timers
systemctl list-timers --all

# Enable and start a timer
systemctl enable --now backup.timer

# Run the associated service immediately (for testing)
systemctl start backup.service

Socket Activation


…
Câu hỏi thường gặp

“Service” trong Linux là gì?

Đó là chương trình chạy nền, không cần người dùng tương tác trực tiếp, như máy chủ web hoặc công cụ giám sát.

“Timer” của systemd là gì?

Đây là cách hiện đại và linh hoạt hơn để lên lịch tác vụ, thay thế cho “cron” trước đây. Chúng cho phép đặt thời gian chính xác và thiết lập phụ thuộc.

Tôi có cần là chuyên gia để dùng không?

Không, công cụ giúp đơn giản hóa việc quản lý. Bạn chỉ cần biết chương trình nào muốn chạy và có quyền quản trị trên Linux.

Chi tiết

Mô tả gốc của tác giả

Create and manage systemd services and timers. Configure service dependencies and resource limits. Use when managing system services.

repo: sickn33/agentic-awesome-skills

Độ phổ biến

Sao GitHub
48k
Vị trí xếp hạng
#5919
Giấy phép
MIT
Cập nhật
15/10/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự