BASH
Nginx Service Control and Status Check Script
A bash script to check the status, start, stop, or restart the Nginx web server, useful for web server management and deployment workflows.
#!/bin/bash
SERVICE="nginx"
# Function to check Nginx status
check_status() {
systemctl is-active --quiet "$SERVICE"
if [ $? -eq 0 ]; then
echo "$SERVICE is running."
else
echo "$SERVICE is not running."
fi
}
# Main script logic
case "$1" in
start)
echo "Starting $SERVICE..."
sudo systemctl start "$SERVICE"
check_status
;;
stop)
echo "Stopping $SERVICE..."
sudo systemctl stop "$SERVICE"
check_status
;;
restart)
echo "Restarting $SERVICE..."
sudo systemctl restart "$SERVICE"
check_status
;;
status)
check_status
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
;;
esac
How it works: This script provides a simple command-line interface to manage the Nginx service. It uses `systemctl` (common on modern Linux distributions) to perform `start`, `stop`, `restart`, or `status` actions. The `check_status` function verifies if the Nginx service is active, providing immediate feedback on its state, making it convenient for server administrators and deployment automation.