1

How to suppress output of systemctl?

I have tried

systemctl --quiet
systemctl -q

and even

systemctl 2>&1 1>/dev/null

none of these seem to work when testing for service is disabled when said service is already disabled?

systemctl -q is-disabled dhcpcd
AdminBee
  • 22,803

2 Answers2

8

systemctl does not support is-disabled option, you probably meant is-enabled. To use it and redirect both stdout and stderr to /dev/null:

systemctl is-enabled dhcpcd >/dev/null 2>&1
2

is-disabled is not a systemctl keyword.

This is closer to what I think you want:

systemctl status service &>/dev/null || echo "is-disabled"

I'm using the shell OR branching operator. That line will print is-disabled if the previous command return an error status which systemctl will do in case the service is disabled. If you want to test if the service is enabled instead you can use && instead of ||. The echo is just an example of a following command.

AdminBee
  • 22,803
Daniel
  • 21
  • 2
  • Weird, the error I was seeing is now nonexistent.... disabling something twice would through a message to stderr that seemed undirect-able. – Schorschi May 26 '21 at 21:08
  • @AdminBee 'verb' is the term used by systemd. – Daniel Jun 01 '21 at 23:05
  • Nothing is carved in stone; you can always (and should :) ) edit your own post to add corrections as you see fit. – AdminBee Jun 02 '21 at 07:13
  • Regarding the current version systemctl status $SERVICE &>/dev/null || echo "is-disabled": echoing "is-disabled" is misleading, as systemctl has various nonzero exit statuses, each indicating something specific. And a unit/$SERVICE can be enabled while systemctl status $SERVICE returns a nonzero exit status , See https://www.freedesktop.org/software/systemd/man/systemctl.html – Abdull Jul 18 '23 at 09:39