How to Create Custom Startup and Shutdown Scripts in Debian Linux


11 views

Modern Debian systems use systemd as their init system, though some legacy systems might still use SysVinit. We'll cover both approaches to ensure compatibility.

For Debian 8 and newer versions, systemd provides a robust way to manage services:

# Create a service unit file
sudo nano /etc/systemd/system/myservice.service

Sample service file content:

[Unit]
Description=My Custom Service
After=network.target

[Service]
Type=simple
ExecStart=/path/to/startup.sh
ExecStop=/path/to/shutdown.sh
Restart=on-failure
User=myuser

[Install]
WantedBy=multi-user.target

Enable and start the service:

sudo systemctl daemon-reload
sudo systemctl enable myservice.service
sudo systemctl start myservice.service

For older Debian systems, use the traditional init.d method:

sudo cp startup.sh /etc/init.d/myservice
sudo chmod +x /etc/init.d/myservice
sudo update-rc.d myservice defaults

For shutdown scripts, create a symlink in the appropriate runlevel:

sudo ln -s /etc/init.d/myservice /etc/rc0.d/K01myservice
sudo ln -s /etc/init.d/myservice /etc/rc6.d/K01myservice

Your scripts should:

  • Include proper shebang (#!/bin/bash)
  • Implement status return codes (0 for success, others for errors)
  • Include logging functionality
  • Handle SIGTERM properly for graceful shutdowns
#!/bin/bash

# Logging setup
LOG_FILE=/var/log/myservice.log
exec > >(tee -a "$LOG_FILE")
exec 2>&1

echo "$(date) - Starting My Service"

# Your startup commands here
/path/to/your/server --daemon

exit 0
#!/bin/bash

# Logging setup
LOG_FILE=/var/log/myservice.log
exec > >(tee -a "$LOG_FILE")
exec 2>&1

echo "$(date) - Stopping My Service"

# Graceful shutdown procedure
if [ -f /var/run/myservice.pid ]; then
    kill -TERM $(cat /var/run/myservice.pid)
    rm -f /var/run/myservice.pid
fi

exit 0

Check service status:

systemctl status myservice  # For systemd
service myservice status    # For SysVinit

View logs:

journalctl -u myservice -b  # For systemd
tail -f /var/log/myservice.log  # For both methods

Debian traditionally used System V init system, but modern versions may use systemd. We'll cover both approaches to ensure compatibility.

For legacy systems or when you need maximum compatibility:


# Create your startup script
sudo nano /etc/init.d/myserver

#!/bin/bash
### BEGIN INIT INFO
# Provides:          myserver
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: My Server Application
# Description:       Custom server application startup/shutdown script
### END INIT INFO

case "$1" in
  start)
    echo "Starting myserver"
    /path/to/startup.sh
    ;;
  stop)
    echo "Stopping myserver"
    /path/to/shutdown.sh
    ;;
  *)
    echo "Usage: /etc/init.d/myserver start|stop"
    exit 1
    ;;
esac

exit 0

Make it executable and register:


sudo chmod +x /etc/init.d/myserver
sudo update-rc.d myserver defaults

For systems using systemd (Debian 8+):


# Create service file
sudo nano /etc/systemd/system/myserver.service

[Unit]
Description=My Server Application
After=network.target

[Service]
ExecStart=/path/to/startup.sh
ExecStop=/path/to/shutdown.sh
Type=oneshot
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

Enable and start the service:


sudo systemctl daemon-reload
sudo systemctl enable myserver.service
sudo systemctl start myserver.service

To check if your script is working:


# For System V
sudo service myserver status

# For systemd
sudo systemctl status myserver.service

# View boot logs
journalctl -b
  • Always test scripts manually before adding to startup
  • Include proper error handling in your scripts
  • Consider using absolute paths in your scripts
  • Document any dependencies your scripts require

For simple cases, you can also use:


# Adding to rc.local (not recommended for production)
sudo nano /etc/rc.local
/path/to/startup.sh &
exit 0

Or using cron:


@reboot /path/to/startup.sh