Best Practices for Permanently Disabling Specific cron.{hourly,daily,weekly} Scripts Across Linux Distributions


1 views

Most Linux administrators eventually face this scenario: you want to disable a cron job installed via system packages, but simply deleting the script isn't a permanent solution. Package managers like emerge (Gentoo) or zypper (OpenSUSE) will happily restore deleted files during upgrades. Let's explore robust solutions that survive package updates.

Many modern distributions support disabling scripts by appending .disabled:

# mv /etc/cron.daily/slocate /etc/cron.daily/slocate.disabled

This works because most cron implementations skip files containing dots in their names. The advantage? Package managers typically won't overwrite renamed files.

For more formal package management:

# gentoo: use etc-update to preserve changes
echo 'CONFIG_PROTECT="/etc/cron.daily/slocate"' >> /etc/portage/make.conf

# opensuse: use rpm alternatives
rpm --setperms cronie-noanacron

Sometimes the simplest solution is best:

# Make script non-executable but keep it in place
chmod -x /etc/cron.weekly/package-cleanup

Most cron implementations check execute permissions before running scripts.

For Gentoo: Use the etc-update system to preserve changes:

etc-update --automode -5 /etc/cron.daily/slocate

For OpenSUSE: Leverage YaST's cron module:

yast2 cron

Navigate to the appropriate tab and disable specific jobs through the GUI.

For systems using systemd timers (common in newer distributions):

# Create override file
mkdir -p /etc/systemd/system/slocate.timer.d
echo -e "[Timer]\nOnCalendar=" > /etc/systemd/system/slocate.timer.d/override.conf
systemctl daemon-reload

Always verify your changes:

# Check cron's view of jobs
crontab -l

# For systemd systems:
systemctl list-timers

When working with traditional vixie-cron implementations:

# Check if run-parts supports --list option
run-parts --test /etc/cron.daily | grep slocate

When managing Unix-like systems, we often encounter situations where default cron scripts (located in /etc/cron.{hourly,daily,weekly}) need to be disabled permanently. The naive approach of simply deleting these files leads to problems during package upgrades, as package managers like emerge (Gentoo) or zypper (OpenSUSE) will reinstall the original files.

Most modern distributions implement one of these standard approaches:

# Method 1: Using .disabled suffix (common in Debian/Ubuntu)
sudo mv /etc/cron.daily/slocate /etc/cron.daily/slocate.disabled

# Method 2: Using noexec prefix (common in RedHat-based systems)
sudo touch /etc/cron.daily/.no-slocate

# Method 3: Using dpkg-divert (Debian-specific)
sudo dpkg-divert --divert /etc/cron.daily/slocate.disabled --rename /etc/cron.daily/slocate

For Gentoo systems using Portage:

# Create a configuration protected file
sudo touch /etc/portage/savedconfig/sys-apps/slocate
sudo echo "# Disabled cron job" > /etc/portage/savedconfig/sys-apps/slocate

# Or use CONFIG_PROTECT
echo 'CONFIG_PROTECT="/etc/cron.daily/slocate"' >> /etc/portage/make.conf

For OpenSUSE systems using zypper:

# Create a custom RPM macro to exclude the file
echo '%_unpackaged_files_terminate_build 0' >> ~/.rpmmacros

# Or use the alternatives system
sudo update-alternatives --install /etc/cron.daily/slocate slocate /bin/true 100

The most portable solution involves creating a wrapper script that exits immediately:

#!/bin/sh
# File: /etc/cron.daily/slocate
exit 0

Then make it immutable:

sudo chattr +i /etc/cron.daily/slocate

After implementing any of these methods, verify with:

sudo lsattr /etc/cron.daily/slocate
sudo grep slocate /var/lib/dpkg/status  # On Debian systems
sudo rpm -V slocate  # On RPM-based systems