fix: better handeling of restart/reload logic

This commit is contained in:
Ruben Solvang 2025-05-07 14:53:07 +02:00
parent df83b8aa59
commit 0f17ec7fd5
2 changed files with 28 additions and 8 deletions

View file

@ -87,6 +87,12 @@ Use the `--disable-pem` to only fetch the .key and .crt:
./certman.sh --disable-pem ./certman.sh --disable-pem
``` ```
### Restart service instead of Reload
Some services do not support at soft restart (reload). use `--restart` to force this option insted of the default reload.
```bash
./certman.sh --restart
```
## Environment Variables ## Environment Variables
| Variable | Description | Required | | Variable | Description | Required |
@ -97,6 +103,7 @@ Use the `--disable-pem` to only fetch the .key and .crt:
| TEMP_PATH | Temporary directory for downloads | Yes | | TEMP_PATH | Temporary directory for downloads | Yes |
| FULLCHAIN_PEM | Enabled by default | No | | FULLCHAIN_PEM | Enabled by default | No |
| SERVICE_NAME | Service to reload after certificate updates | Yes | | SERVICE_NAME | Service to reload after certificate updates | Yes |
| SERIVCE_SUPPORTS | Reload or restart? Reload is the default | No |
| CERT_OWNER | User owner for certificate files | Yes | | CERT_OWNER | User owner for certificate files | Yes |
| CERT_GROUP | Group owner for certificate files | Yes | | CERT_GROUP | Group owner for certificate files | Yes |
| CERT_PERMISSIONS | Certificate file permissions | Yes | | CERT_PERMISSIONS | Certificate file permissions | Yes |

View file

@ -26,6 +26,7 @@ load_env || exit 1
AUTO_MODE="false" AUTO_MODE="false"
FORCE_UPDATE="false" FORCE_UPDATE="false"
SERVICE_SUPPORTS="${SERVICE_SUPPORTS:-reload}"
FULLCHAIN_PEM="${FULLCHAIN_PEM:-true}" FULLCHAIN_PEM="${FULLCHAIN_PEM:-true}"
while [[ $# -gt 0 ]]; do while [[ $# -gt 0 ]]; do
@ -42,6 +43,10 @@ while [[ $# -gt 0 ]]; do
FULLCHAIN_PEM="false" FULLCHAIN_PEM="false"
shift shift
;; ;;
--restart)
SERVICE_SUPPORTS="restart"
shift
;;
*) *)
shift shift
;; ;;
@ -222,14 +227,22 @@ process_certificates() {
# Reload service if needed # Reload service if needed
if [ $service_reloaded -eq 1 ]; then if [ $service_reloaded -eq 1 ]; then
echo -e "${BLUE}Reloading/restarting $SERVICE_NAME service...${NC}" if [[ $SERVICE_SUPPORTS = reload ]]; then
if systemctl reload "$SERVICE_NAME"; then echo -e "${BLUE}Reloading $SERVICE_NAME service...${NC}"
echo -e "${GREEN}Service reloaded successfully${NC}" if systemctl reload "$SERVICE_NAME"; then
elif systemctl restart "$SERVICE_NAME"; then echo -e "${GREEN}Service reloaded successfully${NC}"
echo -e "${GREEN}Service restarted successfully${NC}" else
else echo -e "${RED}Failed to reload service${NC}"
echo -e "${RED}Failed to reload or restart service${NC}" return 1
return 1 fi
elif [[ $SERVICE_SUPPORTS = restart ]]; then
echo -e "${BLUE}Restarting $SERVICE_NAME service...${NC}"
if systemctl restart "$SERVICE_NAME"; then
echo -e "${GREEN}Service restarted successfully${NC}"
else
echo -e "${RED}Failed to restart service${NC}"
return 1
fi
fi fi
fi fi
} }