How to Check Available systemctl Commands for a Specific Service in Linux


2 views

When working with systemd services, you might encounter situations where certain commands aren't available for a particular service unit. Unlike traditional init.d scripts that typically display available commands when run without arguments, systemd requires a different approach to discover supported operations.

The most reliable way to determine available commands is by examining the service unit file:

systemctl cat nagios.service

Look for these key directives in the output:

  • ExecStart - indicates the service supports start
  • ExecStop - indicates the service supports stop
  • ExecReload - indicates the service supports reload
  • ExecRestart - indicates the service supports restart

You can query specific properties of a service unit:

systemctl show nagios.service -p ExecStart,ExecStop,ExecReload,ExecRestart

This will output only the relevant execution commands, making it easier to parse programmatically.

Let's examine a real-world example with Apache httpd:

# Check unit file
systemctl cat httpd.service

# Sample output might include:
[Service]
ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND
ExecReload=/usr/sbin/httpd $OPTIONS -k graceful
ExecStop=/bin/kill -WINCH ${MAINPID}

This output tells us that httpd.service supports start, reload, and stop commands.

For services like the original example (nagios.service) that don't support reload, you have alternatives:

# Option 1: Use restart (less graceful)
systemctl restart nagios.service

# Option 2: Implement custom reload if needed
# Edit the unit file to add ExecReload directive
sudo systemctl edit --full nagios.service

When writing your own service files, ensure you include all necessary directives:

[Unit]
Description=My Custom Service

[Service]
ExecStart=/usr/local/bin/myservice start
ExecStop=/usr/local/bin/myservice stop
ExecReload=/usr/local/bin/myservice reload
ExecRestart=/usr/local/bin/myservice restart
Restart=on-failure

[Install]
WantedBy=multi-user.target

For automation purposes, you can create a script to check command support:

#!/bin/bash

check_command_support() {
    local service=$1
    local command=$2
    
    case $command in
        start)   systemctl show "$service" -p ExecStart | grep -q = ;;
        stop)    systemctl show "$service" -p ExecStop | grep -q = ;;
        reload)  systemctl show "$service" -p ExecReload | grep -q = ;;
        restart) systemctl show "$service" -p ExecRestart | grep -q = ;;
        *)       return 1 ;;
    esac
}

# Usage example:
if check_command_support nagios.service reload; then
    systemctl reload nagios.service
else
    echo "Reload not supported for nagios.service"
fi

When managing services with systemctl, you might encounter errors like:

# systemctl reload nagios.service
failed to reload nagios.service: Job type reload is not applicable for unit nagios.service.

Unlike traditional init.d scripts that display available commands when executed without arguments, systemd requires different approaches to discover supported operations for a service.

The most reliable way is to examine the service unit file:

systemctl cat nagios.service

Look for these key directives in the output:

  • ExecStart - Indicates start capability
  • ExecStop - Indicates stop capability
  • ExecReload - Indicates reload capability
  • Restart - Shows auto-restart behavior

For programmatic inspection:

systemctl show nagios.service --property=ExecStart,ExecStop,ExecReload

While not as comprehensive as init.d scripts, you can try:

systemctl -h | grep ""

This shows all potentially available commands, though service-specific support still depends on the unit file.

For httpd.service, we can see full capabilities:

# systemctl cat httpd.service
[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/httpd
ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND
ExecReload=/usr/sbin/httpd $OPTIONS -k graceful
ExecStop=/bin/kill -WINCH ${MAINPID}

Create a simple bash function for your shell:

function syscmds() {
    echo "Supported commands for $1:"
    systemctl cat "$1" | grep -E "Exec(Start|Stop|Reload)" | cut -d= -f1 | sed 's/Exec//'
}

Usage:

syscmds nginx.service

For services converted from init.d scripts, check the compatibility wrapper:

ls -l /usr/lib/systemd/system/nagios.service

These often have limited command support compared to native systemd services.

Different service types support different commands:

  • simple: Basic start/stop
  • notify: Supports reload
  • forking: Traditional daemon behavior

Check the service type with:

systemctl show -p Type nagios.service