Skripty životního cyklu
Požadavky na skripty:
- Všechny skripty životního cyklu musí začínat direktivou shebang
#!/bin/bash - Kódování souboru: UTF-8
- Oprávnění souboru:
755(spustitelné pro všechny, zápis pro vlastníka) - Všechny skripty musí skončit s návratovým kódem
0, který označuje úspěch - Použijte
set -e, aby došlo k selhání při jakékoli chybě
preinst — Před instalací
#!/bin/bash
set -e
# Create a dedicated user (if it does not exist)
if ! id -u <appid> > /dev/null 2>&1; then
useradd --system --no-create-home --shell /usr/sbin/nologin <appid> 2>/dev/null || true
fi
# Create data directories
mkdir -p /var/lib/<appid>
chown <appid>:<appid> /var/lib/<appid> 2>/dev/null || true
# Create Unix Socket directory (WebUI Internal Open)
mkdir -p /var/api
exit 0
postinst — Po instalaci
#!/bin/bash
set -e
# Set file permissions
chown -R <appid>:<appid> /usr/local/<appid> 2>/dev/null || true
chown -R <appid>:<appid> /var/lib/<appid> 2>/dev/null || true
# Decompress webui.bz2 if it exists (WebUI applications)
if [ -f /usr/local/<appid>/webui.bz2 ]; then
cd /usr/local/<appid> && tar -xjf webui.bz2 2>/dev/null || true
fi
# Enable and start the service
systemctl daemon-reload
systemctl enable <system_id>.service
systemctl start <system_id>.service
exit 0
prerm — Před odebráním
#!/bin/bash
set -e
# Kill all residual processes of the application user (upgrade safety)
pkill -u <appid> 2>/dev/null || true
sleep 1
# Stop and disable the service
systemctl stop <system_id>.service 2>/dev/null || true
systemctl disable <system_id>.service 2>/dev/null || true
exit 0
postrm — Po odebrání
#!/bin/bash
set -e
# Reload systemd
systemctl daemon-reload
# Remove user and data on purge
if [ "$1" = "purge" ]; then
if id -u <appid> > /dev/null 2>&1; then
userdel <appid> 2>/dev/null || true
fi
rm -rf /var/lib/<appid>
rm -f /var/api/<appid>.sock
# Remove nginx configuration
rm -f /etc/nginx/conf.d/<appid>.conf 2>/dev/null || true
# Remove systemd service file
rm -f /etc/systemd/system/<system_id>.service 2>/dev/null || true
# Reload systemd
systemctl daemon-reload 2>/dev/null || true
fi
exit 0