Dapatkah saya mencegah "status layanan foo" dari paging outputnya melalui "kurang"?

35

Bagaimana Anda menghentikan perintah service <name> statusdari menggunakan lessoutputnya?

Saya memiliki skrip yang mengotomatiskan beberapa tindakan sysadmin, dan setelah saya memutakhirkan server saya ke Ubuntu 16.04, itu melanggar karena tindakan yang memeriksa status layanan diblokir karena menggunakan sesuatu seperti lessuntuk menunjukkan keluaran, khususnya supervisorlayanan.

Saya memiliki beberapa daemon yang dikonfigurasi untuk dijalankan, dan ketika dijalankan sudo service supervisor status, saya mendapatkan:

* 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

Dan itu tidak kembali sampai saya gulir ke bawah secara manual atau tekan Quntuk keluar. Bagaimana cara menonaktifkan fitur ini?

Cerin
sumber

Jawaban:

54

Ubuntu adalah sistem systemd, di mana service statusperintah sebenarnya memanggil systemctl status, dan systemctl memiliki --no-pageropsi yang melakukan persis apa yang Anda cari. Jadi Anda mungkin lebih baik menggunakan perintah systemctl langsung dalam skrip Anda.

sudo systemctl --no-pager status supervisor

EDIT: env var SYSTEMD_PAGER

Cara lain, seperti yang ditunjukkan oleh @jwodder, adalah mengatur variabel lingkungan SYSTEMD_PAGER. Ini memiliki manfaat tambahan juga mempengaruhi output systemctlketika dipanggil oleh aplikasi lain seperti service.

export SYSTEMD_PAGER=''
sudo service supervisor status

Akan memungkinkan Anda untuk mencapai hasil yang sama.

Tim Kennedy
sumber
2
Saya ingin menyarankan menempatkan opsi di depan perintah:sudo systemctl --no-pager status supervisor
Jeff Schaller
diedit untuk mencocokkan saran Anda. baik untuk mengikuti penggunaan alat yang dimaksudkan. Terima kasih.
Tim Kennedy
2
"Ubuntu adalah sebuah systemdsistem" ... ketika Upstart tidak digunakan yaitu
cat
6
Untuk membuat systemctlselalu berperilaku seperti --no-pageryang diberikan, Anda dapat mengatur SYSTEMD_PAGERvariabel lingkungan ke salah satu catatau string kosong.
jwodder
@ jwodder - pengaturan SYSTEMD_PAGER memiliki keuntungan tambahan juga mempengaruhi output systemctlbahkan ketika dipanggil oleh service. Bagus.
Tim Kennedy
34

less biasanya akan mematikan mode pager jika mendeteksi bahwa outputnya bukan terminal. Jadi Anda bisa mencoba membuat keluaran perintah Anda bukan terminal dengan menjalankan sesuatu seperti:

sudo service supervisor status | cat
Dhag
sumber
4
Jawaban ini bagus karena generik. Jawaban Tim Kennedy baik karena khusus untuk systemctl. Saya membatalkan keduanya.
Kamil Maciorowski
Saya hampir akan menghapus jawaban saya untuk yang lain, tetapi saya melihat bagaimana berita umum pengetahuan umum bisa berharga. Sebagai catatan, saya pikir jawaban lain yang harus diterima.
Dhag
Saya akan selalu memilih jawaban yang lebih umum, semua hal lain dianggap sama. Inilah yang akan saya lakukan di RHEL jika perintah back-end akan lessdan saya tidak ingin perilaku itu. Tentu saja, jika saya melakukan ini dalam sebuah skrip yang menggambarkan sesuatu seperti baris "Active:", itu sudah tidak akan menuju terminal, dan masalah ini bahkan tidak ada.
Monty Harder
Itu tidak lessmematikan apa pun. Itu systemctltidak memanggil pager di tempat pertama ketika output standarnya bukan terminal.
JdeBP
@MontyHarder selain fakta bahwa Anda mungkin tidak harus mengeluarkan baris Active: karena Anda hanya dapat memeriksa kode keluar dari status systemctl (Anda benar-benar harus menggunakan show systemctl dalam skrip alih-alih status) atau systemctl is- {aktif, gagal} , yang akan memberi tahu Anda tanpa BS penguraian ini :)
Yet Another User
16

Dari 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").

Jadi, di suatu tempat di inisialisasi lingkungan Anda, atur:

SYSTEMD_PAGER=
muru
sumber
Bagus. Yang menyenangkan tentang hal ini, adalah bahwa pengaturan SYSTEMD_PAGER juga memengaruhi program lain yang memanggil systemctl! jadi sekarang, service supervisor statusmenunjukkan perilaku yang sama.
Tim Kennedy
@Toby maaf, tapi saya lebih suka halaman manual saya di blok kode.
muru
3

Anda juga perlu memodifikasi sudoersfile Anda :

 Defaults        env_keep += "SYSTEMD_PAGER"
JalapenoGremlin
sumber
Ini adalah informasi yang sangat berguna .. semakin jengkel atas kenyataan bahwa setelah mengganti SYSTEMD_PAGER, sudo systemctl status <service>masih ada halamannya. +1
Procyclinsur