When maintaining long-running processes on Linux servers, developers often face the monitoring vs restart dilemma. Supervisor provides robust process control, but introduces a single point of failure - when Supervisor itself crashes. Cronjobs offer simplicity but lack real-time monitoring capabilities.
While Supervisor's memory usage is generally modest (typically 10-30MB), it can grow with:
- Numerous child processes
- Extensive logging configurations
- Complex event listeners
Supervisor + Cron Fallback:
# crontab -e
*/5 * * * * /usr/bin/pgrep supervisord > /dev/null || /usr/bin/supervisord -c /etc/supervisord.conf
Pure Cron Solution:
*/5 * * * * /usr/bin/pgrep my_process || /path/to/my_process --daemon
For mission-critical systems, consider combining both approaches:
#!/bin/bash
# monitor_wrapper.sh
PROCESS="my_process"
SUPERVISOR_PID=$(pgrep supervisord)
if [ -z "$SUPERVISOR_PID" ]; then
# Fallback direct monitoring
if ! pgrep $PROCESS > /dev/null; then
/path/to/$PROCESS --restart
fi
# Attempt to revive supervisor
/usr/bin/supervisord -c /etc/supervisord.conf
fi
Metric | Supervisor | Cron |
---|---|---|
Restart Latency | Immediate | Up to cron interval |
Memory Overhead | Moderate | Minimal |
Monitoring Granularity | Process state | Simple existence |
Choose Supervisor when:
- You need immediate restarts
- Process state monitoring is required
- Multiple processes need coordinated management
Opt for Cron when:
- System resources are extremely constrained
- Sub-minute restart delays are acceptable
- You prefer simplicity over features
When managing long-running processes, developers often face the dilemma between using dedicated process managers like Supervisor and relying on traditional cronjobs. The key challenge is ensuring process reliability while minimizing system overhead.
Supervisor provides several advantages for process monitoring:
- Real-time process supervision with immediate restart capabilities
- Centralized management through a single configuration file
- Process grouping and event notifications
- Built-in web interface for monitoring
Example Supervisor configuration:
[program:my_app]
command=/usr/bin/python /path/to/your/app.py
autostart=true
autorestart=true
stderr_logfile=/var/log/my_app.err.log
stdout_logfile=/var/log/my_app.out.log
While cronjobs can be used for basic monitoring, they have limitations:
- No immediate process restart (depends on cron interval)
- Requires additional scripting for proper monitoring
- No built-in process grouping or logging
Example cronjob check script:
#!/bin/bash
if ! pgrep -f "my_app" > /dev/null; then
/path/to/your/app.py &
fi
The memory usage concern with Supervisor is often overstated. In most cases, Supervisor adds minimal overhead (typically <10MB). The benefits usually outweigh the costs for production environments.
For maximum reliability, consider combining both approaches:
- Use Supervisor as the primary process manager
- Implement a cronjob as a fallback to check Supervisor's status
Example hybrid monitoring script:
#!/bin/bash
# Check if supervisor is running
if ! pgrep supervisord > /dev/null; then
service supervisor start
fi
# Verify managed process is running
if ! supervisorctl status my_app | grep -q RUNNING; then
supervisorctl restart my_app
fi
Use Supervisor when:
- You need immediate process restart
- Multiple processes require coordinated management
- You want centralized logging and monitoring
Consider cronjobs when:
- Running on extremely resource-constrained systems
- Processes are not critical and can tolerate downtime
- You already have robust monitoring infrastructure
In tests comparing both approaches on a standard Ubuntu server:
Metric | Supervisor | Cronjob |
---|---|---|
Restart latency | <1s | Up to 60s |
Memory overhead | 8-12MB | Negligible |
CPU usage | 0.1% avg | Spikes during checks |
Supervisor offers several powerful features often overlooked:
[eventlistener:process_watcher]
command=/path/to/custom_event_handler.py
events=PROCESS_STATE
This allows for custom actions based on process state changes, something impossible with simple cronjobs.
Both approaches have security implications:
- Supervisor runs with elevated privileges by default
- Cronjobs require proper permission handling in scripts
- Supervisor's web interface needs proper authentication