LinuxCommandLibrary

systemctl-condrestart

Conditionally restart a running service

TLDR

View documentation for the original command

$ tldr systemctl try-restart
copy

SYNOPSIS

systemctl condrestart [OPTIONS...] NAME...

PARAMETERS

NAME...
    One or more unit names (e.g., apache2.service, network.target). These specify the units to be conditionally restarted.

-h, --help
    Show a short help text and exit.

--no-block
    Do not wait for the operation to finish. If this option is specified, systemctl will exit immediately after sending the command to the systemd manager.

--no-wall
    Do not send a wall message to all logged-in users before the operation starts.

-q, --quiet
    Suppress output messages, including errors.

--system
    Connect to the systemd system manager. This is the default.

--user
    Connect to the systemd user manager for the current user.

--runtime
    Operate on runtime configuration (temporary changes that last until next reboot).

--volatile
    Make changes to a volatile storage.

DESCRIPTION

systemctl condrestart is a subcommand of systemctl used to conditionally restart one or more systemd units.
Unlike the restart command, which attempts to stop and then start a service regardless of its current state, condrestart performs a restart operation only if the specified unit is currently running or active. If the unit is not active, condrestart will simply do nothing and exit successfully.

This command is particularly useful in scripting and automation scenarios where you want to apply configuration changes to a service without inadvertently starting it if it was intentionally stopped. For instance, a post-installation script might run systemctl condrestart apache2.service to ensure that Apache picks up new configurations, but only if Apache was already running. If Apache was stopped, perhaps for maintenance, condrestart will leave it in its stopped state, preventing an unintended start-up. It provides a safer and more idempotent way to manage service restarts.

CAVEATS

The command will only restart units that are in an active (running) state. If a service is stopped, failed, or inactive, condrestart will have no effect on it.
Requires appropriate permissions to control the specified systemd units. Typically, root privileges are needed for system services, or sufficient privileges for user services.
The specified unit NAME must correspond to an existing unit file in systemd's configuration paths.

USE CASES AND IDEMPOTENCY

condrestart is ideal for idempotent operations in automation. Configuration management tools (like Ansible, Puppet, Chef) often use condrestart or similar logic when applying changes to services. This ensures that a service is only restarted if it was previously running, minimizing disruption and avoiding unintended starts, which is crucial for maintaining system stability and predictability.

COMPARISON WITH SYSTEMCTL RESTART

The key difference lies in the behavior when the service is not running.

  • systemctl restart service: Always attempts to stop the service (if running) and then start it. If the service is already stopped, it will start it.
  • systemctl condrestart service: Only performs the restart if the service is currently running. If the service is stopped, it does nothing.

HISTORY

The concept of a "conditional restart" originated in traditional SysVinit initialization scripts, where an init.d script might offer a condrestart action.

When systemd replaced SysVinit as the default init system in many Linux distributions, systemctl was designed to provide a more modern and unified interface for service management. The condrestart subcommand was included in systemctl to maintain compatibility with existing practices and scripts, recognizing its utility in managing service lifecycles without unwanted side effects. It became an integral part of the systemctl command set from early systemd versions.

SEE ALSO

systemctl(1), systemctl restart(1), systemctl start(1), systemctl stop(1), systemctl status(1), systemd.unit(5)

Copied to clipboard