2

I'm on Ubuntu 16.04.

Running

service nginx status

puts me in a interactive mode with this process. I want to exit, which normally on a terminal is done by pressing q, but right now I'm writing this as part of a shell script. Is there a way to exit out of there and continue execute commands and show their output?

So just to explain this more clearly. When you run this service status command, you are taken out of the shell, just like when you run a less command, and put into a mode to interact with the process. You get out of it by pressing q.

The reason is simply that I'd like to run this command and see the output and proceed to execute other commands and see output.

Michael Hampton
  • 237,123
  • 42
  • 477
  • 940
TurtleTread
  • 123
  • 5

1 Answers1

1

You ran the deprecated command service nginx status and got output similar to this:

error@vmtest-ubuntu1604:~$ sudo service nginx status
* nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: en
   Active: active (running) since Mon 2019-03-04 10:44:30 EST; 13s ago
 Main PID: 16843 (nginx)
   CGroup: /system.slice/nginx.service
           |-16843 nginx: master process /usr/sbin/nginx -g daemon on; master_pr
           |-16844 nginx: worker process                           
           `-16845 nginx: worker process                           

Mar 04 10:44:30 vmtest-ubuntu1604 systemd[1]: Starting A high performance web se
Mar 04 10:44:30 vmtest-ubuntu1604 systemd[1]: Started A high performance web ser
lines 1-11/11 (END)

Upon which you didn't get the terminal prompt back.

First, this command is deprecated because Ubuntu 16.04 replaced upstart with systemd. The service command now attempts to translate your command into the corresponding systemd command, in this case systemctl status nginx. This is what was actually run, and what actually generated the output you saw. In a future Ubuntu release, the service command will be removed entirely.

What's going on?

By default in current versions of systemd, systemctl pipes the status output through a pager, by default less.

You can turn this behavior off by passing --no-pager in the command, in which case the output will just be dumped to standard out, and your terminal comes back immediately.

error@vmtest-ubuntu1604:~$ sudo systemctl --no-pager status nginx
* nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2019-03-04 10:44:30 EST; 3min 33s ago
 Main PID: 16843 (nginx)
   CGroup: /system.slice/nginx.service
           |-16843 nginx: master process /usr/sbin/nginx -g daemon on; master...
           |-16844 nginx: worker process
           `-16845 nginx: worker process

Mar 04 10:44:30 vmtest-ubuntu1604 systemd[1]: Starting A high performance web...
Mar 04 10:44:30 vmtest-ubuntu1604 systemd[1]: Started A high performance web ...
Hint: Some lines were ellipsized, use -l to show in full.
error@vmtest-ubuntu1604:~$ 

Note that this output isn't meant to be machine-parsed. If you are trying to check service status in shell scripts. you should use other systemctl commands, such as systemctl is-active.

error@vmtest-ubuntu1604:~$ sudo systemctl is-active --quiet nginx && echo Running || echo Stopped
Running
error@vmtest-ubuntu1604:~$ sudo systemctl stop nginx
error@vmtest-ubuntu1604:~$ sudo systemctl is-active --quiet nginx && echo Running || echo Stopped
Stopped
Michael Hampton
  • 237,123
  • 42
  • 477
  • 940
  • Well I'm impressed by the speed of the answer. Thanks for letting me know that option flag. But curious, there's not a direct way to feed in commands like 'q'? – TurtleTread Mar 04 '19 at 15:59
  • `less` is reading from standard input, or from the terminal. Maybe you could redirect it, but if you don't actually want the output paged, best to just not call the pager to begin with. – Michael Hampton Mar 04 '19 at 16:07