sysdctl
- wrapper script for the systemctl
utility.
This utility reverses the order of systemctl
's command and service parameters making it much easier to type in normal usage on the command line.
The systemctl
utility allows for systemd
services (called units in systemd
parlance) to be started,
stopped, restarted, checked for status, enabled/disabled etc.
The basic syntax is:
systemctl <verb> <unit>
For example, to restart the postfix email service:
# systemctl restart postfix
Often I find myself wanting to stop, start or restart and check status of a service all in quick succession. That is, the service stays the same, it's just the command that changes.
With systemctl
that involves a lot of typing and backspacing.
By reversing the order things become much easier:
# sysdctl postfix stop
# sysdctl postfix start
# sysdctl postfix status
Only the last few characters of the line need to be edited.
With TAB completion feature enabled, things are even easier:
# sysd<TAB> pos<TAB> re<TAB>
expands to
# sysdctl postfix restart
for example.
The Script
#!/bin/bash
# --------------------------------------------------------
# Helper utility for systemctl util that:
# 1. is easier to type
# 2. has the command specified as the last parameter
# eg instead of:
# > systemctl status service
# we have:
# > sysdctl service status
#
# see: https://telecnatron.com/reference/software/sysdctl/index.html
#
# --------------------------------------------------------
# Make unassigned variables cause an error
set -u
# --------------------------------------------------------
function error_exit ()
{
echo "${1}" > /dev/stderr
exit -1
}
# --------------------------------------------------------
# check that 2 arguments have been provided
[ "$#" -eq "2" ] || error_exit "usage: $0 service_name command_verb"
SYSTEMCTL="/bin/systemctl"
[ -x $SYSTEMCTL ] || error_exit "systemctl utility not found at ${SYSTEMCTL}"
# command (verb) is the last argument
command=${@:$#}
# strip new last argument to get the service (unit)
service=${@:1:$#-1}
# run the systemctl command
cmd_line="${SYSTEMCTL} ${command} ${service}"
#echo $cmd_line
${cmd_line}
Is simple enough. It can be downloaded from here.
TAB Completion
If the following function is called prior to running sysdctl
then TAB-completion for the serivce name and the command will be available.
You could put this in your ~/.bashrc
file, for example, to have it loaded automatically.
# completions fo the (/home/steves/bin/)sysdctl utility
_sysdctl ()
{
if [ "${#COMP_WORDS[@]}" == "2" ]; then
# first argument is the systemd-service 'unit' name:
units=$(systemctl --full --no-legend --no-pager | awk '{print $1}')
COMPREPLY=($(compgen -W "${units}" "${COMP_WORDS[1]}"))
return
elif [ "${#COMP_WORDS[@]}" == "3" ]; then
# second argument is the command
cmds="start stop status restart enable disable"
COMPREPLY=($(compgen -W "${cmds}" "${COMP_WORDS[2]}"))
fi
}
complete -F _sysdctl sysdctl
You can download it here.