Minimalist Ways to Auto-Start Scripts on Debian Without Complex init.d Hacks


1 views

Modern Debian systems (9+) have evolved beyond the old /etc/init.d approach, yet many tutorials still recommend creating elaborate LSB headers. Here are cleaner alternatives:

Create this unit file (replace paths/values as needed):

# /etc/systemd/system/myscript.service
[Unit]
Description=My Custom Startup Script

[Service]
ExecStart=/path/to/your/script.sh
Type=simple
User=youruser

[Install]
WantedBy=multi-user.target

Then enable it:

sudo systemctl enable myscript.service

For ultra-simple cases, edit your user's crontab:

crontab -e
@reboot /path/to/script.sh

First enable the service:

systemctl enable rc-local.service

Then edit /etc/rc.local:

#!/bin/sh
/path/to/script.sh &
exit 0

Here's how I launch a monitoring script:

# /etc/systemd/system/pymonitor.service
[Unit]
Description=Python Server Monitor

[Service]
ExecStart=/usr/bin/python3 /opt/scripts/server_monitor.py
WorkingDirectory=/opt/scripts
Restart=on-failure

[Install]
WantedBy=multi-user.target
  • Check logs: journalctl -u yourservice
  • Test without reboot: systemctl start yourservice
  • Verify execution: ps aux | grep yourscript

Many Debian administrators hit this roadblock when trying to run simple scripts at boot. The legacy System V init system requires elaborate LSB headers that feel excessive for trivial automation tasks. Here's why the complaint is valid:

#!/bin/bash
# Actual script content would be just 1 line!
/path/to/your_script.sh

Yet the required boilerplate makes this 30+ lines. There are cleaner alternatives.

For Debian 8+ systems using systemd (most modern installations), create a minimal service:

# /etc/systemd/system/myscript.service
[Unit]
Description=My Custom Startup Script

[Service]
ExecStart=/path/to/your_script.sh

[Install]
WantedBy=multi-user.target

Then enable it with:

sudo systemctl enable myscript.service

Option A: rc.local

For trivial cases, append to /etc/rc.local (ensure it's executable):

#!/bin/sh
/path/to/your_script.sh &
exit 0

Option B: Crontab @reboot

crontab -e
@reboot /path/to/your_script.sh

If you're stuck with System V init, here's a minimal compliant template:

#!/bin/sh
### BEGIN INIT INFO
# Provides:          myscript
# Default-Start:     2 3 4 5
# Default-Stop:      
# Short-Description: My script
### END INIT INFO

/path/to/your_script.sh

Install with:

sudo update-rc.d myscript defaults

After setting up any method, test with:

sudo reboot
# Then check
ps aux | grep your_script
journalctl -b | grep your_script  # For systemd