1 #compdef systemctl -*- shell-script -*-
2 # SPDX-License-Identifier: LGPL-2.1-or-later
4 (( $+functions[_systemctl_commands] )) || _systemctl_commands()
8 local -a unit_commands=(
10 "list-sockets:List sockets"
11 "list-timers:List timers"
12 "list-units:List units"
13 "start:Start (activate) one or more units"
14 "stop:Stop (deactivate) one or more units"
15 "reload:Reload one or more units"
16 "restart:Start or restart one or more units"
17 "condrestart:Restart one or more units if active"
18 "try-restart:Restart one or more units if active"
19 "reload-or-restart:Reload one or more units if possible, otherwise start or restart"
20 "force-reload:Reload one or more units if possible, otherwise restart if active"
21 "try-reload-or-restart:Reload one or more units if possible, otherwise restart if active"
22 "isolate:Start one unit and stop all others"
23 "kill:Send signal to processes of a unit"
24 "is-active:Check whether units are active"
25 "is-failed:Check whether units are failed"
26 "status:Show runtime status of one or more units"
27 "show:Show properties of one or more units/jobs or the manager"
28 "cat:Show the source unit files and drop-ins"
29 "set-property:Sets one or more properties of a unit"
30 "help:Show documentation for specified units"
31 "reset-failed:Reset failed state for all, one, or more units"
32 "list-dependencies:Show unit dependency tree"
33 "clean:Remove configuration, state, cache, logs or runtime data of units"
34 "bind:Bind mount a path from the host into a unit's namespace"
35 "mount-image:Mount an image from the host into a unit's namespace"
38 local -a machine_commands=(
40 "list-machines:List the host and all running local containers"
43 local -a unit_file_commands=(
45 "list-unit-files:List installed unit files"
46 "enable:Enable one or more unit files"
47 "disable:Disable one or more unit files"
48 "reenable:Reenable one or more unit files"
49 "preset:Enable/disable one or more unit files based on preset configuration"
50 "preset-all:Enable/disable all unit files based on preset configuration"
51 "is-enabled:Check whether unit files are enabled"
52 "mask:Mask one or more units"
53 "unmask:Unmask one or more units"
54 "link:Link one or more units files into the search path"
55 "revert:Revert unit files to their vendor versions"
56 "add-wants:Add Wants= dependencies to a unit"
57 "add-requires:Add Requires= dependencies to a unit"
58 "set-default:Set the default target"
59 "get-default:Query the default target"
60 "edit:Edit one or more unit files"
63 local -a job_commands=(
66 "cancel:Cancel all, one, or more jobs"
69 local -a environment_commands=(
70 # Environment Commands
71 "show-environment:Dump environment"
72 "set-environment:Set one or more environment variables"
73 "unset-environment:Unset one or more environment variables"
74 "import-environment:Import environment variables set on the client"
77 local -a manager_state_commands=(
78 # Manager State Commands
79 "daemon-reload:Reload systemd manager configuration"
80 "daemon-reexec:Reexecute systemd manager"
81 "log-level:Get or set the log level"
82 "log-target:Get or set the log target"
83 "service-watchdogs:Get or set the state of software watchdogs"
86 local -a system_commands=(
88 "is-system-running:Query overall status of the system"
89 "default:Enter system default mode"
90 "rescue:Enter system rescue mode"
91 "emergency:Enter system emergency mode"
92 "halt:Shut down and halt the system"
93 "suspend:Suspend the system"
94 "poweroff:Shut down and power-off the system"
95 "reboot:Shut down and reboot the system"
96 "kexec:Shut down and reboot the system with kexec"
97 "exit:Ask for user instance termination"
98 "switch-root:Change root directory"
99 "hibernate:Hibernate the system"
100 "hybrid-sleep:Hibernate and suspend the system"
101 "suspend-then-hibernate:Suspend the system for a period of time, and then hibernate it"
104 local -a groups=( unit machine unit_file job environment manager_state system )
105 local -a _systemctl_cmds
107 _systemctl_cmds+=( "${(@P)${:-"${i}_commands"}}" )
110 if (( CURRENT == 1 )); then
111 _tags ${^groups//_/-}-commands
114 if _requested ${i//_/-}-commands; then
115 _describe -t ${i//_/-}-commands "${i//_/ } command" ${i}_commands \
121 local curcontext="$curcontext"
123 cmd="${${_systemctl_cmds[(r)$words[1]:*]%%:*}}"
124 # Deal with any aliases
126 condrestart) cmd="try-restart";;
127 force-reload) cmd="try-reload-or-restart";;
131 curcontext="${curcontext%:*:*}:systemctl-${cmd}:"
134 zstyle -s ":completion:${curcontext}:" cache-policy update_policy
135 if [[ -z "$update_policy" ]]; then
136 zstyle ":completion:${curcontext}:" cache-policy _systemctl_caching_policy
139 _call_function ret _systemctl_$cmd || _message 'no more arguments'
141 _message "unknown systemctl command: $words[1]"
147 # @todo _systemd-run has a helper with the same name, so we must redefine
150 systemctl $_sys_service_mgr --full --no-legend --no-pager --plain "$@" 2>/dev/null
154 # Fills the unit list
155 (( $+functions[_systemctl_all_units] )) ||
156 _systemctl_all_units()
158 if ( [[ ${+_sys_all_units} -eq 0 ]] || _cache_invalid SYS_ALL_UNITS$_sys_service_mgr ) ||
159 ! _retrieve_cache SYS_ALL_UNITS$_sys_service_mgr;
161 _sys_all_units=( ${${(f)"$(__systemctl list-units --all "$PREFIX*" )"}%% *} )
162 _store_cache SYS_ALL_UNITS$_sys_service_mgr _sys_all_units
166 # Fills the unit list including all file units
167 (( $+functions[_systemctl_really_all_units] )) ||
168 _systemctl_really_all_units()
170 local -a all_unit_files;
171 local -a really_all_units;
172 if ( [[ ${+_sys_really_all_units} -eq 0 ]] || _cache_invalid SYS_REALLY_ALL_UNITS$_sys_service_mgr ) ||
173 ! _retrieve_cache SYS_REALLY_ALL_UNITS$_sys_service_mgr;
175 all_unit_files=( ${${(f)"$(__systemctl list-unit-files "$PREFIX*" )"}%% *} )
177 really_all_units=($_sys_all_units $all_unit_files)
178 _sys_really_all_units=(${(u)really_all_units})
179 _store_cache SYS_REALLY_ALL_UNITS$_sys_service_mgr _sys_really_all_units
183 (( $+functions[_filter_units_by_property] )) ||
184 _filter_units_by_property() {
185 local property=$1 value=$2; shift 2
186 local -a units; units=("${(q-)@}")
188 props=(${(f)"$(_call_program units "$service $_sys_service_mgr show --no-pager --property=\"Id,$property\" -- ${units} 2>/dev/null")"})
189 echo -E - "${(@g:o:)${(k@)props[(Re)$property=$value]}#Id=}"
192 (( $+functions[_systemctl_get_non_template_names] )) ||
193 _systemctl_get_non_template_names() { echo -E - ${^${(R)${(f)"$(
194 __systemctl $mode list-unit-files "$PREFIX*"
195 __systemctl $mode list-units --all "$PREFIX*"
196 )"}:#*@.*}%%[[:space:]]*} }
198 (( $+functions[_systemctl_get_template_names] )) ||
199 _systemctl_get_template_names() { echo -E - ${^${(M)${(f)"$(__systemctl list-unit-files "$PREFIX*" )"}##*@.[^[:space:]]##}%%@.*}\@ }
201 (( $+functions[_systemctl_active_units] )) ||
202 _systemctl_active_units() {_sys_active_units=( ${${(f)"$(__systemctl list-units "$PREFIX*" )"}%% *} )}
204 (( $+functions[_systemctl_startable_units] )) ||
205 _systemctl_startable_units(){
206 _sys_startable_units=( $( _filter_units_by_property ActiveState inactive $(
207 _filter_units_by_property CanStart yes ${${${(f)"$(
208 __systemctl $mode list-unit-files --state enabled,enabled-runtime,linked,linked-runtime,static,indirect,disabled,generated,transient "$PREFIX*"
209 __systemctl $mode list-units --state inactive,failed "$PREFIX*"
210 )"}:#*@.*}%%[[:space:]]*}
214 (( $+functions[_systemctl_restartable_units] )) ||
215 _systemctl_restartable_units(){
216 _sys_restartable_units=( $( _filter_units_by_property CanStart yes ${${${(f)"$(
217 __systemctl $mode list-unit-files --state enabled,disabled,static "$PREFIX*"
218 __systemctl $mode list-units "$PREFIX*"
219 )"}:#*@.*}%%[[:space:]]*} ) )
222 (( $+functions[_systemctl_failed_units] )) ||
223 _systemctl_failed_units() {_sys_failed_units=( ${${(f)"$(__systemctl list-units --state=failed "$PREFIX*" )"}%% *} ) }
225 (( $+functions[_systemctl_unit_state] )) ||
226 _systemctl_unit_state() {
227 setopt localoptions extendedglob
228 typeset -gA _sys_unit_state
229 _sys_unit_state=( ${=${${(f)"$(__systemctl list-unit-files "$PREFIX*" )"}%%[[:space:]]#}% *} )
233 # Completion functions for ALL_UNITS
234 for fun in cat mask ; do
235 (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
237 _systemctl_really_all_units
238 _wanted systemd-units expl unit \
239 compadd "$@" -a - _sys_really_all_units
243 # Completion functions for NONTEMPLATE_UNITS
244 for fun in is-active is-failed is-enabled status show preset help list-dependencies edit revert add-wants add-requires set-property; do
245 (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
247 _wanted systemd-units expl unit \
248 compadd "$@" - $(_systemctl_get_non_template_names)
252 # Completion functions for ENABLED_UNITS
253 (( $+functions[_systemctl_disable] )) || _systemctl_disable()
255 local _sys_unit_state; _systemctl_unit_state
256 _wanted systemd-units expl 'enabled unit' \
257 compadd "$@" - ${(k)_sys_unit_state[(R)enabled]}
260 (( $+functions[_systemctl_reenable] )) || _systemctl_reenable()
262 local _sys_unit_state; _systemctl_unit_state
263 _wanted systemd-units expl 'enabled/disabled unit' \
264 compadd "$@" - ${(k)_sys_unit_state[(R)(enabled|disabled)]} $(_systemctl_get_template_names)
267 # Completion functions for DISABLED_UNITS
268 (( $+functions[_systemctl_enable] )) || _systemctl_enable()
270 local _sys_unit_state; _systemctl_unit_state
271 _wanted systemd-units expl 'disabled unit' \
272 compadd "$@" - ${(k)_sys_unit_state[(R)disabled]} $(_systemctl_get_template_names)
275 # Completion functions for FAILED_UNITS
276 (( $+functions[_systemctl_reset-failed] )) || _systemctl_reset-failed()
278 local _sys_failed_units; _systemctl_failed_units
279 _wanted systemd-units expl 'failed unit' \
280 compadd "$@" -a - _sys_failed_units || _message "no failed unit found"
283 # Completion functions for STARTABLE_UNITS
284 (( $+functions[_systemctl_start] )) || _systemctl_start()
286 local _sys_startable_units; _systemctl_startable_units
287 _wanted systemd-units expl 'startable unit' \
288 compadd "$@" - ${_sys_startable_units[*]}
291 # Completion functions for STOPPABLE_UNITS
292 for fun in stop kill try-restart condrestart ; do
293 (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
295 local _sys_active_units; _systemctl_active_units
296 _wanted systemd-units expl 'stoppable unit' \
297 compadd "$@" - $( _filter_units_by_property CanStop yes \
298 ${_sys_active_units[*]} )
302 # Completion functions for ISOLATABLE_UNITS
303 (( $+functions[_systemctl_isolate] )) || _systemctl_isolate()
306 _wanted systemd-units expl 'isolatable unit' \
307 compadd "$@" - $( _filter_units_by_property AllowIsolate yes \
308 ${_sys_all_units[*]} )
311 # Completion functions for RELOADABLE_UNITS
312 for fun in reload try-reload-or-restart force-reload ; do
313 (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
315 local _sys_active_units; _systemctl_active_units
316 _wanted systemd-units expl 'reloadable unit' \
317 compadd "$@" - $( _filter_units_by_property CanReload yes \
318 ${_sys_active_units[*]} )
322 # Completion functions for RESTARTABLE_UNITS
323 for fun in restart reload-or-restart ; do
324 (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
326 local _sys_restartable_units; _systemctl_restartable_units
327 _wanted systemd-units expl 'restartable unit' \
328 compadd "$@" - ${_sys_restartable_units[*]}
332 # Completion functions for MASKED_UNITS
333 (( $+functions[_systemctl_unmask] )) || _systemctl_unmask()
335 local _sys_unit_state; _systemctl_unit_state
336 _wanted systemd-units expl 'masked unit' \
337 compadd "$@" - ${(k)_sys_unit_state[(R)masked]} || _message "no masked units found"
340 # Completion functions for JOBS
341 (( $+functions[_systemctl_cancel] )) || _systemctl_cancel()
343 _wanted systemd-jobs expl job \
344 compadd "$@" - ${${(f)"$(__systemctl list-jobs)"}%% *} ||
345 _message "no jobs found"
348 # Completion functions for TARGETS
349 (( $+functions[_systemctl_set-default] )) || _systemctl_set-default()
351 _wanted systemd-targets expl target \
352 compadd "$@" - ${${(f)"$(__systemctl list-unit-files --type target --all "$PREFIX*" )"}%% *} ||
353 _message "no targets found"
356 # Completion functions for ENVS
357 for fun in set-environment unset-environment ; do
358 (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
360 local fun=$0 ; fun=${fun##_systemctl_}
362 if [[ "${fun}" = "set-environment" ]]; then
365 _wanted systemd-environment expl 'environment variable' \
366 compadd "$@" ${suf} - ${${(f)"$(systemctl show-environment)"}%%=*}
370 (( $+functions[_systemctl_import-environment] )) || _systemctl_import-environment()
375 (( $+functions[_systemctl_link] )) || _systemctl_link() {
379 (( $+functions[_systemctl_switch-root] )) || _systemctl_switch-root() {
383 (( $+functions[_systemctl_bind] )) || _systemctl_bind() {
387 (( $+functions[_systemctl_mount-image] )) || _systemctl_mount-image() {
391 # no systemctl completion for:
392 # [STANDALONE]='daemon-reexec daemon-reload default
393 # emergency exit halt kexec list-jobs list-units
394 # list-unit-files poweroff reboot rescue show-environment'
396 (( $+functions[_systemctl_caching_policy] )) ||
397 _systemctl_caching_policy()
402 # rebuild if cache is more than a day old
403 oldcache=( "$1"(mh+1) )
404 (( $#oldcache )) && return 0
406 _sysunits=(${${(f)"$(__systemctl --all)"}%% *})
408 if (( $#_sysunits )); then
409 for unit in $_sysunits; do
410 [[ "$unit" -nt "$1" ]] && return 0
417 (( $+functions[_systemctl_unit_states] )) ||
418 _systemctl_unit_states() {
420 _states=("${(fo)$(__systemctl --state=help)}")
421 _values -s , "${_states[@]}"
424 (( $+functions[_systemctl_unit_types] )) ||
425 _systemctl_unit_types() {
427 _types=("${(fo)$(__systemctl -t help)}")
428 _values -s , "${_types[@]}"
431 (( $+functions[_systemctl_unit_properties] )) ||
432 _systemctl_unit_properties() {
433 if ( [[ ${+_sys_all_properties} -eq 0 ]] || _cache_invalid SYS_ALL_PROPERTIES$_sys_service_mgr ) ||
434 ! _retrieve_cache SYS_ALL_PROPERTIES$_sys_service_mgr;
436 _sys_all_properties=( ${${(M)${(f)"$(@rootlibexecdir@/systemd --dump-bus-properties)"}}} )
437 _store_cache SYS_ALL_PROPERTIES$_sys_service_mgr _sys_all_properties
439 _values -s , "${_sys_all_properties[@]}"
442 (( $+functions[_systemctl_job_modes] )) ||
443 _systemctl_job_modes() {
445 _modes=(fail replace replace-irreversibly isolate ignore-dependencies ignore-requirements flush)
446 _values -s , "${_modes[@]}"
449 (( $+functions[_systemctl_timestamp] )) ||
450 _systemctl_timestamp() {
452 _styles=(help pretty us µs utc us+utc µs+utc)
453 _values -s , "${_styles[@]}"
456 (( $+functions[_systemctl_check_inhibitors] )) ||
457 _systemctl_check_inhibitors() {
460 _values -s , "${_modes[@]}"
463 # Build arguments for "systemctl" to be used in completion.
464 local -a _modes; _modes=("--user" "--system")
465 # Use the last mode (they are exclusive and the last one is used).
466 local _sys_service_mgr=${${words:*_modes}[(R)(${(j.|.)_modes})]}
468 {-h,--help}'[Show help]' \
469 '--version[Show package version]' \
470 {-t+,--type=}'[List only units of a particular type]:unit type:_systemctl_unit_types' \
471 '--state=[Display units in the specified state]:unit state:_systemctl_unit_states' \
472 '--job-mode=[Specify how to deal with other jobs]:mode:_systemctl_job_modes' \
473 {-p+,--property=}'[Show only properties by specific name]:unit property:_systemctl_unit_properties' \
474 {-a,--all}'[Show all units/properties, including dead/empty ones]' \
475 '--reverse[Show reverse dependencies]' \
476 '--after[Show units ordered after]' \
477 '--before[Show units ordered before]' \
478 {-l,--full}"[Don't ellipsize unit names on output]" \
479 '--show-types[When showing sockets, show socket type]' \
480 '--check-inhibitors[Specify if inhibitors should be checked]:mode:_systemctl_check_inhibitors' \
481 {-q,--quiet}'[Suppress output]' \
482 '--no-block[Do not wait until operation finished]' \
483 '--no-legend[Do not print a legend, i.e. the column headers and the footer with hints]' \
484 '--no-pager[Do not pipe output into a pager]' \
485 '--system[Connect to system manager]' \
486 '--user[Connect to user service manager]' \
487 "--no-wall[Don't send wall message before halt/power-off/reboot]" \
488 '--global[Enable/disable/mask unit files globally]' \
489 "--no-reload[When enabling/disabling unit files, don't reload daemon configuration]" \
490 '--no-ask-password[Do not ask for system passwords]' \
491 '--kill-who=[Who to send signal to]:killwho:(main control all)' \
492 {-s+,--signal=}'[Which signal to send]:signal:_signals' \
493 {-f,--force}'[When enabling unit files, override existing symlinks. When shutting down, execute action immediately]' \
494 '--root=[Enable/disable/mask unit files in the specified root directory]:directory:_directories' \
495 '--runtime[Enable/disable/mask unit files only temporarily until next reboot]' \
496 {-H+,--host=}'[Operate on remote host]:userathost:_sd_hosts_or_user_at_host' \
497 {-P,--privileged}'[Acquire privileges before execution]' \
498 {-n+,--lines=}'[Journal entries to show]:number of entries' \
499 {-o+,--output=}'[Change journal output mode]:modes:_sd_outputmodes' \
500 '--firmware-setup[Tell the firmware to show the setup menu on next boot]' \
501 '--plain[When used with list-dependencies, print output as a list]' \
502 '--failed[Show failed units]' \
503 '--timestamp=[Change format of printed timestamps]:style:_systemctl_timestamp' \
504 '*::systemctl command:_systemctl_commands'