Skip to content

Instantly share code, notes, and snippets.

@zanshin
Created February 12, 2025 13:39
Show Gist options
  • Save zanshin/8ea4b110b0be3061e34a2be2e593ec3a to your computer and use it in GitHub Desktop.
Save zanshin/8ea4b110b0be3061e34a2be2e593ec3a to your computer and use it in GitHub Desktop.
Check to see if a service is running, if not, start the service
#!/bin/bash
# Check if a service name was provided
if [ -z "$1" ]; then
echo "Usage: $0 <service_name>"
exit 1
fi
SERVICE_NAME="$1"
# Check if the service is running
if systemctl is-active --quiet "$SERVICE_NAME"; then
echo "$SERVICE_NAME is already running."
else
echo "$SERVICE_NAME is not running. Starting it now..."
sudo systemctl start "$SERVICE_NAME"
# Verify if it started successfully
if systemctl is-active --quiet "$SERVICE_NAME"; then
echo "$SERVICE_NAME started successfully."
else
echo "Failed to start $SERVICE_NAME."
fi
fi
@zanshin
Copy link
Author

zanshin commented Feb 12, 2025

Created using ChatGPT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment