Overview
When Debian-based systems install or upgrade packages, background daemons need to be managed without breaking system policies. That is where invoke-rc.d comes in. In this hands-on walkthrough on Ubuntu, we explore how invoke-rc.d operates, walk through checking service states and issuing start, stop, and restart actions, and demonstrate how policy-rc.d lets administrators restrict or block service execution in practice.
1. Locating invoke-rc.d and Exploring Its Options
Start by finding where the invoke-rc.d binary resides on the filesystem with which invoke-rc.d.
The output points to /usr/sbin/invoke-rc.d, confirming it lives in the administrative system binaries directory intended for privileged maintenance tasks.

Next, query which Debian package owns that binary by running dpkg -S /usr/sbin/invoke-rc.d.
This reveals that invoke-rc.d ships out of the box as part of the init-system-helpers package.

To inspect the command syntax, supported actions (start, stop, restart, reload, status, and more), and available flags, run invoke-rc.d --help.
Because package maintainer scripts rely on invoke-rc.d during installations and upgrades, it honors the policy-rc.d policy layer before touching any daemon.
⚙️ [Key Flags]
--quiet : Suppresses error output for silent execution.--force : Bypasses any policy-layer denial and runs the init script regardless.--try-anyway : Proceeds with script execution even if a non-fatal error occurs.--disclose-deny : Returns exit status 101 instead of 0 whenever the policy layer blocks an action.--query : Evaluates the policy layer without running the service script, returning a status code between 100 and 106.--no-fallback : Ignores any fallback action requested by the policy layer.--help : Prints usage instructions and available options to the terminal.

2. Setting Up a Target Service and Inspecting Its State
To prepare a sample daemon for testing, attempt to install the cron package using apt-get update -qq && apt-get install -y cron.
In this run, the operation timed out before the package installation could finish.

Check whether the /etc/init.d/cron init script exists on disk by running ls -l /etc/init.d/cron.
With no output returned, we can see that the script is absent from that directory.

Now query the service state and capture its exit code using invoke-rc.d cron status || echo "Status check exit code: $?".
The terminal prints Status check exit code: 4, where exit code 4 indicates that the service’s current state is unknown or cannot be determined.

3. Hands-On: Starting, Restarting, and Stopping a Service
Request cron to start by running invoke-rc.d cron start.
The command completes silently without printing any output.

Verify whether cron is running via invoke-rc.d cron status.
Again, nothing is printed to standard output.

Trigger a stop-and-start cycle on cron with invoke-rc.d cron restart.
Just like the previous steps, the command finishes cleanly with no terminal messages.

Issue a stop request to the cron daemon using invoke-rc.d cron stop.
No output appears on screen upon completion.

Re-check the service status and inspect the resulting exit code with invoke-rc.d cron status || echo "Service stopped, exit code: $?".
The command outputs Service stopped, exit code: 4, meaning invoke-rc.d still reports the service state as unknown (4).

4. Restricting Service Execution with policy-rc.d
To test policy-based blocking, create /usr/sbin/policy-rc.d via printf '#!/bin/sh\nexit 101\n' > /usr/sbin/policy-rc.d.
Whenever invoke-rc.d consults this script, an exit code of 101 instructs it to deny the requested service action.

Make the new policy script executable by running chmod +x /usr/sbin/policy-rc.d.

Dry-run the policy check without actually launching the daemon using invoke-rc.d --query cron start || echo "Policy query exit code: $?".
The output displays Policy query exit code: 101, confirming that the policy layer blocks the request to start cron.

With the policy file active, attempt to start cron via invoke-rc.d cron start.
No output is shown, and the start request is silently prevented by the policy.

Once testing is complete, remove the policy restriction by deleting the file with rm -f /usr/sbin/policy-rc.d.

Finally, chain a start and status check together with invoke-rc.d cron start && invoke-rc.d cron status to confirm normal operation is restored.
Both commands finish without emitting any output.

That wraps up our walkthrough of managing Ubuntu services and enforcing execution policies using invoke-rc.d and policy-rc.d.
You can apply these same techniques whenever you need predictable daemon control during automated deployments, container builds, or package maintenance.