systemctl: o canivete suíço
O systemctl é a ferramenta principal para gerenciar serviços no systemd. As operações básicas:
# Iniciar um serviço
sudo systemctl start nginx
# Parar
sudo systemctl stop nginx
# Reiniciar (para + inicia)
sudo systemctl restart nginx
# Recarregar configuração sem parar
sudo systemctl reload nginx
# Ver status detalhado
systemctl status nginx
• nginx.service - A high performance web server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; preset: enabled)
Active: active (running) since Fri 2025-03-21 08:03:12 UTC; 2h ago
Docs: man:nginx(8)
Process: 1520 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Main PID: 1530 (nginx)
Tasks: 3 (limit: 4648)
Memory: 12.4M
CPU: 245ms
CGroup: /system.slice/nginx.service
|-1530 "nginx: master process /usr/sbin/nginx"
|-1531 "nginx: worker process"
enable e disable
Controlar se o serviço inicia automaticamente no boot:
# Habilitar no boot
sudo systemctl enable nginx
# Habilitar E iniciar agora
sudo systemctl enable --now nginx
# Desabilitar do boot
sudo systemctl disable nginx
# Verificar se está habilitado
systemctl is-enabled nginx
enabled
Listando units
Para ver todos os serviços e seus estados:
# Serviços ativos
systemctl list-units --type=service
# Todos (incluindo inativos)
systemctl list-units --type=service --all
# Serviços com falha
systemctl list-units --type=service --state=failed
UNIT LOAD ACTIVE SUB DESCRIPTION
cron.service loaded active running Regular background program processing
nginx.service loaded active running A high performance web server
ssh.service loaded active running OpenBSD Secure Shell server
mysql.service loaded active running MySQL Community Server
journalctl: logs do systemd
O journalctl acessa os logs centralizados do systemd. Para filtrar por serviço:
# Logs de um serviço específico
journalctl -u nginx
# Apenas os logs mais recentes
journalctl -u nginx -n 20
# Acompanhar em tempo real (como tail -f)
journalctl -u nginx -f
# Logs desde o último boot
journalctl -u nginx -b
# Filtrar por período
journalctl -u mysql --since "2025-03-21 08:00" --until "2025-03-21 10:00"
Dica: use
journalctl -p err para ver apenas mensagens de erro de todos os serviços. Combine com -b para filtrar erros desde o último boot.Criando um service file básico
Para criar seu próprio serviço, crie um arquivo .service em /etc/systemd/system/:
sudo vim /etc/systemd/system/minha-app.service
[Unit]
Description=Minha Aplicação Web
After=network.target
Wants=network.target
[Service]
Type=simple
User=app
Group=app
WorkingDirectory=/opt/minha-app
ExecStart=/usr/bin/python3 /opt/minha-app/server.py
Restart=on-failure
RestartSec=5
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
Depois de criar ou editar o arquivo, recarregue o systemd e inicie:
# Recarregar definições de units
sudo systemctl daemon-reload
# Iniciar e habilitar no boot
sudo systemctl enable --now minha-app
# Verificar
systemctl status minha-app
Dica: as diretivas mais importantes do
[Service] são Restart=on-failure (reinicia automaticamente em caso de crash) e After=network.target (garante que a rede esteja pronta antes de iniciar).