๐ฆ System Services & Daemons
Shell scripts frequently interact with system services and daemons such as web servers, databases, background workers, and system-level components.
Understanding how to manage, monitor, and integrate with these services is essential for reliable automation and productionโgrade operations.
๐ Who This Is For
- DevOps/SRE engineers maintaining Linux servers
- Sysadmins managing systemd or legacy init systems
- Developers writing deployment or provisioning scripts
- Anyone integrating shell automation with longโrunning services
๐งฉ Role in the Ecosystem
Linux services are managed by:
- systemd (modern, dominant)
- SysV init (legacy)
- OpenRC, runit, s6 (specialized systems)
Shell scripts often:
- Start/stop/restart services
- Check service health
- Read logs
- Reload configuration
- Manage environment files
- Integrate with daemons during deployment
๐งฉ Key Concepts
1. systemd Units
Systemd uses unit files to define services:
.service โ daemons
.timer โ scheduled tasks
.socket โ socket activation
.target โ grouping
2. Service Lifecycle
Common operations:
| systemctl start service
systemctl stop service
systemctl restart service
systemctl reload service
|
3. Logs
Systemd logs are centralized:
4. Environment Files
Services can load environment variables from:
| /etc/default/service
/etc/sysconfig/service
EnvironmentFile=/etc/service.env
|
5. Exit Codes
Service status is determined by exit codes and systemd state.
๐ง Techniques
Start/Stop/Restart Services
| systemctl restart nginx
systemctl stop postgresql
systemctl start docker
|
Check Service Status
| systemctl is-active nginx
systemctl is-enabled nginx
systemctl status nginx
|
Reload Configuration Without Restart
Follow Logs
Run Commands as a Service User
Enable/Disable Services at Boot
| systemctl enable ssh
systemctl disable cups
|
Timers (systemd cron replacement)
List timers:
Run timer immediately:
| systemctl start backup.timer
|
Legacy SysV Init
| service nginx start
service nginx status
/etc/init.d/nginx restart
|
Check Open Ports
Check Daemon Health
| systemctl is-active --quiet nginx || echo "nginx down"
|
โ ๏ธ Limitations & Pitfalls
- systemd behavior varies across distributions
- EnvironmentFile paths differ (Debian vs RHEL)
- Reload vs restart semantics vary by service
- Some services ignore reload
- SysV scripts may not support all operations
- systemd may restart services automatically (Restart=always)
- Logs may rotate or be rateโlimited
๐ง When to Use Shell for Service Management
- Deployment scripts
- CI/CD pipelines
- Health checks
- Configuration reloads
- Log inspection
- Service orchestration on single hosts
โ When Not to Use Shell
- Complex orchestration (use Ansible, Salt, Puppet)
- Multiโnode coordination (use Kubernetes, Nomad, systemdโrun)
- Highโavailability failover logic
- Longโrunning monitoring loops (use systemd timers or proper daemons)
โ
Best Practices
- Use
systemctl instead of legacy service
- Check exit codes explicitly
- Use
systemctl is-active for health checks
- Use
journalctl -f for realโtime logs
- Avoid hardโcoding paths to unit files
- Use
systemctl daemon-reload after modifying units
- Use environment files for configuration
- Prefer timers over cron for systemd systems
- Document service dependencies
๐งช Testing Service Integration
Check service state:
Check logs:
Test restart:
| systemctl restart ssh
echo $? # 0 = success
|
Test health:
| systemctl is-active --quiet ssh && echo ok || echo fail
|
๐ง Summary
Shell scripts integrate naturally with system services through systemd, SysV init, and related tools.
Understanding service lifecycle, logs, environment files, and health checks is essential for reliable automation and productionโgrade operations.