我可以防止“ service foo status”通过“ less”分页输出吗?


35

如何停止在其输出上service <name> status使用该命令less

我有一个脚本可以自动执行一些sysadmin操作,并且在将服务器升级到Ubuntu 16.04之后,由于检查服务状态的操作被阻止(因为它使用的是less显示输出的内容,特别是supervisor服务),该脚本正在中断。

我有几个配置为运行的守护程序,运行时sudo service supervisor status,我得到:

* 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

直到我手动向下滚动或按Q退出它才返回。如何禁用此功能?

Answers:


54

Ubuntu是一个systemd系统,该service status命令实际上调​​用systemctl status,而systemctl有一个--no-pager选项可以完全满足您的需求。因此,在脚本中使用直接的systemctl命令可能会更好。

sudo systemctl --no-pager status supervisor

编辑:env var SYSTEMD_PAGER

@jwodder指出的另一种方法是设置SYSTEMD_PAGER环境变量。这样做还有一个好处,就是还会影响systemctl诸如的其他应用程序调用时的输出service

export SYSTEMD_PAGER=''
sudo service supervisor status

将使您实现相同的输出。


2
我想建议将该选项放在命令前:sudo systemctl --no-pager status supervisor
Jeff Schaller

根据您的建议进行了修改。遵循工具的预期用途。谢谢。
肯尼迪

2
“ Ubuntu是一个systemd系统” ...当不使用Upstart时,即
cat

6
为了使systemctl行为始终像--no-pager给定,可以将SYSTEMD_PAGER环境变量设置为cat或空字符串。
jwodder

@jwodder设置SYSTEMD_PAGER的附加优点是systemctl即使被调用时也影响的输出service。真好
肯尼迪

34

如果less会检测到其输出不是终端,通常会关闭其寻呼机模式。因此,您可以尝试通过运行以下命令使命令的输出不是a-terminal:

sudo service supervisor status | cat

4
这个答案很好,因为它是通用的。蒂姆·肯尼迪(Tim Kennedy)的回答很好,因为它针对systemctl。我都赞成。
卡米尔Maciorowski

我几乎要删除我的答案,而转向另一个答案,但是我看到了通用知识窍门是多么有价值。记录下来,我认为另一个答案应该是被接受的答案。
dhag

在其他所有条件相同的情况下,我将始终选择更通用的答案。如果要执行后端命令,less而我不希望这种行为,那正是我在RHEL上要做的事情。当然,如果我是在脚本中执行此操作的,该脚本可以捕获“ Active:”行之类的内容,那么它就不会终止,甚至不会出现此问题。
Monty Harder

它没有less关闭任何东西。它systemctl不调用摆在首位寻呼机时,它的标准输出不是终端。
JdeBP

@MontyHarder除了您可能不应该grep出Active:这一事实之外,因为您可以仅检查systemctl状态的退出代码(确实应该在脚本中使用systemctl show而不是status)或systemctl is- {active,failed} ,它将在没有任何此解析BS的情况下告诉您:)
另一个用户是

16

来自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").

因此,在您的环境初始化中的某处设置:

SYSTEMD_PAGER=

真好 这样做的好处是,设置SYSTEMD_PAGER还会影响其他调用systemctl的程序!所以现在,service supervisor status表现出相同的行为。
肯尼迪

@Toby抱歉,但我更喜欢代码块中的联机帮助页。
muru

3

您还需要修改sudoers文件:

 Defaults        env_keep += "SYSTEMD_PAGER"

这是非常有用的信息..被惹恼了的事实,切换SYSTEMD_PAGER后,sudo systemctl status <service>仍然分页.. +1
Procyclinsur
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.