"monit restart <service>" how do I know when it's done restarting?
Categories:
Ensuring Service Restart Completion with Monit

Learn how to effectively monitor and confirm the successful restart of services managed by Monit, understanding its asynchronous nature and best practices.
When managing services on a Linux or Unix system, tools like Monit are invaluable for ensuring their continuous operation. Monit can automatically start, stop, or restart services based on predefined conditions. However, a common question arises: how do you know when a monit restart <service>
command has actually completed its job, especially given that Monit often operates asynchronously?
Understanding Monit's Restart Mechanism
Monit's restart
command doesn't block until the service is fully up and running. Instead, it instructs the Monit daemon to perform the restart operation in the background. This means that immediately after executing monit restart <service>
, your terminal prompt returns, but the service might still be in the process of shutting down, starting up, or even encountering issues. Monit's primary role is to ensure the desired state of a service, not necessarily to provide immediate synchronous feedback on command completion.
sequenceDiagram participant User participant MonitCLI as "Monit CLI" participant MonitDaemon as "Monit Daemon" participant Service as "Managed Service" User->>MonitCLI: monit restart <service> MonitCLI->>MonitDaemon: Request restart for <service> MonitCLI-->>User: Command returns immediately MonitDaemon->>Service: Send stop signal Service-->>MonitDaemon: (Optional) Acknowledge stop MonitDaemon->>Service: Send start signal Service-->>MonitDaemon: (Optional) Acknowledge start MonitDaemon->>MonitDaemon: Begin monitoring service status
Asynchronous Monit Restart Process Flow
Methods to Confirm Restart Completion
Since Monit's restart
command is non-blocking, you need alternative strategies to confirm that your service has successfully restarted and is operational. These methods leverage Monit's monitoring capabilities and standard system tools.
start program
and stop program
directives, along with check process
or check host
rules to accurately determine its running state.1. Using monit status
The most straightforward way to check the status of a service managed by Monit is using monit status
. After initiating a restart, you can repeatedly check the status until it reflects the desired 'Running' state. This command provides a summary of all monitored services or a specific one.
monit status my_service
# Example output for a running service:
# Process 'my_service'
# status running
# monitoring status monitored
# pid 12345
# parent pid 1
# uptime 1m 30s
# memory usage 15.0 MB
# cpu usage 0.5%
# data collected Thu, 01 Jan 2023 12:00:00 +0000
Checking service status with monit status
2. Monitoring Logs
Services typically log their startup and shutdown events. Monitoring the service's log file (e.g., /var/log/syslog
, /var/log/messages
, or application-specific logs) can provide definitive proof of a successful restart. You can use tail -f
to watch the logs in real-time.
tail -f /var/log/my_service/my_service.log | grep -i "started\|listening"
Monitoring service logs for startup messages
3. Checking Network Ports or HTTP Endpoints
For network-facing services (e.g., web servers, databases), checking if the associated port is listening or if a specific HTTP endpoint responds correctly is a robust way to confirm operational status. Tools like netstat
, ss
, curl
, or wget
are useful here.
# Check if port 8080 is listening
ss -tuln | grep 8080
# Check an HTTP endpoint
curl -s -o /dev/null -w "%{http_code}" http://localhost:8080/healthz
Verifying service availability via network checks
4. Scripting for Automated Confirmation
For automated deployments or scripts, you can combine these methods into a loop that waits for the service to become available. This involves repeatedly checking the status or logs until a success condition is met, with a timeout to prevent infinite loops.
#!/bin/bash
SERVICE_NAME="my_service"
TIMEOUT=60
INTERVAL=5
echo "Attempting to restart $SERVICE_NAME..."
monit restart $SERVICE_NAME
for i in $(seq 1 $(($TIMEOUT / $INTERVAL))); do
STATUS=$(monit status $SERVICE_NAME | grep 'status' | awk '{print $2}')
if [ "$STATUS" == "running" ]; then
echo "$SERVICE_NAME restarted successfully!"
exit 0
fi
echo "Waiting for $SERVICE_NAME to start... (attempt $i/$((TIMEOUT/INTERVAL)))"
sleep $INTERVAL
done
echo "Error: $SERVICE_NAME did not restart within $TIMEOUT seconds."
exit 1
Bash script to wait for Monit service restart