56

How do you stop the command service <name> status from using less on its output?

I have a script that automates some sysadmin actions, and after I upgraded my server to Ubuntu 16.04, it's breaking because actions that check service status are blocking because it's using something like less to show output, specifically the supervisor service.

I have several daemons configured to run, and when run sudo service supervisor status, I get:

* supervisor.service - Supervisor process control system for UNIX
   Loaded: loaded (/lib/systemd/system/supervisor.service; disabled; vendor preset: enabled)
   Active: active (running) since Mon 2017-02-06 20:35:34 EST; 12h ago
     Docs: http://supervisord.org
  Process: 18476 ExecStop=/usr/bin/supervisorctl $OPTIONS shutdown (code=exited, status=0/SUCCESS)
 Main PID: 20228 (supervisord)
   CGroup: /system.slice/supervisor.service
           |- 7387 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7388 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7389 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7390 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7391 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7392 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7393 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7394 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7395 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7396 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7397 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7398 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7678 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7679 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7680 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7681 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7682 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7683 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7684 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7685 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7693 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7694 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7698 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7702 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7703 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7705 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7707 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7709 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7710 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7712 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7713 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7717 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7720 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7723 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7724 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7728 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7730 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7731 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7733 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7734 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7735 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7738 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7743 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7747 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7748 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7750 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7752 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7756 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7758 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7761 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7763 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7764 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7772 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7781 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7785 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7794 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7799 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7801 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
           |- 7805 /usr/local//myproject/.env/bin/python2.7 /usr/local//myproject/.env/bin/celery worker -A /myproject -l info --autoreload
lines 1-66

And it doesn't return until I manually scroll down or press Q to exit. How do I disable this feature?

Toby Speight
  • 8,460
  • 3
  • 26
  • 50
Cerin
  • 1,601
  • 4
  • 18
  • 25

4 Answers4

89

Ubuntu is a systemd system, where the service status command actually calls systemctl status, and systemctl has a --no-pager option that does exactly what you're looking for. So you may be better off using the straight systemctl command in your script.

sudo systemctl --no-pager status supervisor

environment variable SYSTEMD_PAGER

Another way, as pointed out by @jwodder, is to set the SYSTEMD_PAGER environment variable. This has the added benefit of also affecting the output of systemctl when called by another application like service.

export SYSTEMD_PAGER=
sudo service supervisor status

Will allow you to achieve the same output.

pevik
  • 1,397
  • 15
  • 27
Tim Kennedy
  • 19,369
  • 4
  • 38
  • 58
  • 2
    I'd like to suggest putting the option before the command: `sudo systemctl --no-pager status supervisor` – Jeff Schaller Feb 07 '17 at 15:26
  • edited to match your suggestion. good to follow intended use of tools. thanks. – Tim Kennedy Feb 07 '17 at 15:37
  • 2
    "Ubuntu is a `systemd` system" ... when Upstart is not in use that is – cat Feb 07 '17 at 17:46
  • 7
    To make `systemctl` always behave like `--no-pager` is given, you can set the `SYSTEMD_PAGER` environment variable to either `cat` or the empty string. – jwodder Feb 07 '17 at 17:46
  • @jwodder - setting SYSTEMD_PAGER has the added advantage of also affecting the output of `systemctl` even when called by `service`. Nice. – Tim Kennedy Feb 08 '17 at 14:49
  • The edit seems to say the opposite of what you just said! _"it also affects how systemctl output is passed to 3rd party calling applications like service"_ seems strange/wrong; _"has the added advantage of also affecting the output of systemctl even when called by service"_ makes sense. Did you write it backwards in the edit perchance? – Lightness Races in Orbit Feb 09 '17 at 14:20
  • I have updated the answer given your feedback. Thanks, @LightnessRacesinOrbit – Tim Kennedy Feb 09 '17 at 17:59
  • 1
    I have never seen a pager with my openSUSE installations, but `$SYSTEMD_PAGER` is not set, `$PAGER` has a value of `less`, and `systemctl` has no alias. On the other hand, my Arch system hasn't set anything, either, but appears to use the pager on every `systemctl` output. Does `systemctl` have yet another configuration hook for the pager somewhere? – palswim Sep 15 '17 at 22:43
  • Great, I thought `--quiet` would do the job, but it still paged. – Tomasz Gandor Jul 18 '19 at 08:01
37

less will normally turn its pager mode off if it detects that its output is not a terminal. So you could try making your command's output not-a-terminal by running something like:

sudo service supervisor status | cat
dhag
  • 15,440
  • 4
  • 54
  • 65
  • 4
    This answer is good because it's generic. Tim Kennedy's answer is good because it is specific to `systemctl`. I upvoted both. – Kamil Maciorowski Feb 07 '17 at 18:28
  • 1
    I was almost about to delete my answer in favor of the other one, but I see how the generic knowledge tidbit can be valuable. For the record, I think the other answer should be the one to be accepted. – dhag Feb 07 '17 at 19:08
  • I'll always choose the more generic answer, all other things being equal. This is exactly what I'd do on RHEL if the back-end command were going to `less` and I didn't want that behavior. Of course, if I were doing this in a script that greps out something like the " Active:" line, it already wouldn't be going to terminal, and this problem wouldn't even exist. – Monty Harder Feb 07 '17 at 20:42
  • 1
    It's not `less` turning off anything. It's `systemctl` not invoking a pager in the first place when its standard output is not a terminal. – JdeBP Feb 09 '17 at 16:40
  • 1
    @MontyHarder besides the fact that you probably shouldn't grep out the Active: line because you can just check exit code of systemctl status (really you should use systemctl show in a script instead of status) or systemctl is-{active,failed}, which will tell you without any of this parsing BS :) – Yet Another User Feb 12 '17 at 04:53
  • This answer is a really good illustration of how the designer(s) of systemctl do not respect the design of the underlying system. This (mis)feature also violates the principle of least surprise. – JGurtz Jun 30 '22 at 19:43
  • This does not show the green color from the output – alper Jul 26 '22 at 11:22
19

From man systemctl:

ENVIRONMENT
...
   $SYSTEMD_PAGER
       Pager to use when --no-pager is not given; overrides $PAGER.
       Setting this to an empty string or the value "cat" is equivalent to
       passing --no-pager.

   $SYSTEMD_LESS
       Override the default options passed to less ("FRSXMK").

So, somewhere in your environment initialization, set:

SYSTEMD_PAGER=
muru
  • 69,900
  • 13
  • 192
  • 292
  • Nice. This nice thing about that, is that setting SYSTEMD_PAGER also affects other programs that call systemctl! so now, `service supervisor status` exhibits the same behavior. – Tim Kennedy Feb 08 '17 at 14:47
  • 1
    @Toby sorry, but I prefer my manpages in code blocks. – muru Feb 08 '17 at 15:11
7

You will also need to modify your sudoers file:

 Defaults        env_keep += "SYSTEMD_PAGER"
  • This is very useful information.. was getting irritated over the fact that after switching the SYSTEMD_PAGER, `sudo systemctl status ` was still paged.. +1 – rustysys-dev Sep 01 '18 at 05:06