How to Disable or Backup Init.d Scripts in Linux Without Deleting Them


2 views

In Linux systems, scripts located in /etc/init.d/ are traditional SysV init scripts that control service startup during system boot. These scripts follow a specific convention and are typically symlinked to various runlevel directories (e.g., /etc/rcX.d/) where X represents the runlevel.

To prevent a service from starting automatically while keeping the script intact:

# For Debian/Ubuntu systems:
sudo update-rc.d -f varnish remove

# For RHEL/CentOS systems:
sudo chkconfig varnish off

Alternatively, you can manually remove the symlinks from runlevel directories:

sudo find /etc/rc?.d/ -name "*varnish" -exec rm {} \;

To create a backup while keeping the original functional:

sudo cp /etc/init.d/varnish /etc/init.d/varnish.backup
sudo chmod -x /etc/init.d/varnish.backup

When you need to restore the automatic startup:

# Debian/Ubuntu:
sudo update-rc.d varnish defaults

# RHEL/CentOS:
sudo chkconfig varnish on

For systems using systemd (most modern distributions), the equivalent commands would be:

# Disable
sudo systemctl disable varnish

# Enable
sudo systemctl enable varnish

Check if the service is enabled to start at boot:

# SysV init:
ls -l /etc/rc?.d/*varnish

# Systemd:
systemctl is-enabled varnish

Remember that even when disabled, you can still start the service manually when needed:

sudo service varnish start  # SysV
sudo systemctl start varnish  # systemd

In traditional SysVinit systems, the /etc/init.d/ directory contains startup scripts for various services. These scripts are linked to corresponding runlevel directories (like /etc/rcX.d/) where X represents the runlevel number. The naming convention follows S##service for start and K##service for kill sequences.

Method 1: Using update-rc.d (Debian/Ubuntu)


# Disable Varnish from starting at boot
sudo update-rc.d varnish disable

# To re-enable later
sudo update-rc.d varnish enable

Method 2: Using chkconfig (RedHat/CentOS)


# Disable service
sudo chkconfig varnish off

# Check status
sudo chkconfig --list varnish

For systems without these tools, you can manually manage symlinks:


# Find all symlinks pointing to your script
ls -l /etc/rc*.d/* | grep varnish

# Remove specific symlink (example for runlevel 2)
sudo rm /etc/rc2.d/S20varnish

# To restore, create the symlink again
sudo ln -s /etc/init.d/varnish /etc/rc2.d/S20varnish

Before making changes, always back up your scripts:


# Create timestamped backup
sudo cp /etc/init.d/varnish /etc/init.d/varnish.bak_$(date +%Y%m%d)

# Or store in your home directory
sudo cp /etc/init.d/varnish ~/varnish_backup_$(date +%Y%m%d)

For systems using systemd (most modern distributions), use these commands instead:


# Disable service
sudo systemctl disable varnish.service

# Mask service (prevent all activation)
sudo systemctl mask varnish.service

# To revert
sudo systemctl unmask varnish.service
sudo systemctl enable varnish.service

After making changes, verify your configuration:


# Check if service is enabled
sudo systemctl is-enabled varnish.service

# Alternative method
ls -la /etc/rc*.d/ | grep varnish
  • Always document your changes in /etc/init.d/README or similar
  • Consider using version control for critical scripts
  • Test changes in a staging environment first
  • Keep original scripts with .dist or .orig extensions