Skip to main content

Lifecycle Scripts

Script Requirements:

  • All lifecycle scripts must begin with the #!/bin/bash shebang
  • File encoding: UTF-8
  • File permissions: 755 (executable by all, writable by owner)
  • All scripts must exit with exit code 0 to indicate success
  • Use set -e to fail on any error

preinst — Before Installation

#!/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 — After Installation

#!/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 — Before Removal

#!/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 — After Removal

#!/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