systemctl-try-restart
Restart service if active
TLDR
Restart a specific unit if it is running
Restart multiple units if they are running
Restart all units matching a pattern if they are running
SYNOPSIS
systemctl [OPTIONS] try-restart UNIT...
PARAMETERS
UNIT...
One or more unit names to operate on (e.g., `apache2.service`, `network.target`).
--no-block
Do not wait for the operation to finish. The command will return immediately after queuing the operation.
--force
Force the operation. While its direct effect on 'try-restart' is minimal in terms of the 'only if running' logic, it can force other `systemctl` operations or override certain checks.
--wait
Wait for the operation to complete. This is the default behavior unless `--no-block` is used.
--user
Communicate with the systemd user instance, managing user-specific services rather than system services.
--system
Communicate with the systemd system and service manager. This is the default behavior when no `--user` or `--scope` is specified.
--runtime
Make changes that are effective only until the next system reboot. These changes are not persistent across reboots.
--root=PATH
Operate on a specified alternative root directory `PATH` instead of the system's default root.
DESCRIPTION
The `systemctl try-restart` command is a crucial utility within the systemd service manager, designed to restart one or more specified service units, but only if they are currently in a running state. Unlike the `systemctl restart` command, which will start a service if it's stopped and then restart it, `try-restart` will simply do nothing if the target service is not active. This behavior makes `try-restart` particularly useful in scripting and automation scenarios where you want to apply configuration changes or update a service without inadvertently starting a service that was intentionally stopped.
It ensures that your actions only affect currently operational services, preserving the intended state of inactive units. The command communicates with the systemd init system via D-Bus to manage the specified units, providing a robust and controlled way to maintain system services on modern Linux systems.
CAVEATS
The command requires the systemd service manager to be running and accessible via D-Bus.
If the specified service unit is not running, `try-restart` will silently do nothing and return a successful exit code (0). This behavior must be accounted for in scripts to avoid misinterpretation of success.
Requires appropriate permissions (typically root privileges) to manage system services, unless operating on user units with `--user`.
EXIT STATUS
`systemctl try-restart` returns an exit code of 0 on success, even if the service was not running and no action was taken. A non-zero exit code indicates an error (e.g., invalid unit name, permission denied).
SCRIPTING UTILITY
This command is particularly valuable in automated scripts or configuration management tools (like Ansible, Chef, Puppet) where you want to apply updates or configuration changes to a service without accidentally starting it if it was previously stopped (e.g., during maintenance or troubleshooting). It ensures that only services intentionally running are affected by a restart operation.
HISTORY
The `systemctl` command is an integral part of systemd, the system and service manager that largely replaced the traditional SysVinit and Upstart systems in many Linux distributions starting in the early 2010s. `try-restart` was introduced as part of the systemd suite to provide finer-grained control over service management, distinguishing between unconditionally restarting a service and conditionally restarting only active ones. Its development reflects systemd's goal of offering a comprehensive and flexible framework for managing the lifecycle of system processes.


