13

I'm looking for a way to get a list of all the services (/etc/init.d or upstart or systemd), which are supposed to be starting (or having been started at) boot.

How do you get a list of all starting services? — I know that question, and it suggests to use service --status-all.

But that doesn't show me, which services are SUPPOSED TO BE RUNNING. It calls all the init scripts with "status" argument. This is a list of all the services that could be running.

But I would like to know, if a service, that had been started at boot is still running.

Example…

I have webfs installed. But I do not want it to be running at boot. Thus I disabled it: sudo update-rc.d webfs disable. And I also have samba installed and it should be running at boot. But it is stopped (for whatever reason). sudo service --status-all doesn't help me here:

$ sudo service --status-all 2>/dev/null | grep -E 'samba$|webfs'
 [ - ]  samba
 [ - ]  webfs

Both are off but I get no clue, that one (samba) is supposed to be on.

So…

How could I get a list of all services which are starting at boot? And, as an extension, is there an easy way to get the "status" of these services (if not, I'll simply loop over this list and run "service $service status", or something like this).

Thanks, Alexander

Edit 2015-05-04: I'm on Ubuntu 14.04.

The main focus of this question is the following:

But I would like to know, if a service, that had been started at boot is still running.

Because of this, initctl list doesn't help much. It doesn't really take into consideration the services started by /etc/init.d scripts (compared to upstart scripts in /etc/init).

Also a listing of /etc/rc?.d/S* doesn't help. It would generate a list of services, which might have been started in a given runlevel. It doesn't show me, if a service, that should've been started, is still running.

Basically, I'm looking for something like svcs -x from Solaris for Ubuntu. With svcs -x, I'd have output if a service, which was started, isn't running anymore. This, I'd need for Ubuntu as well.

Reason: I'd like to have a (more or less...) generic check for our Icinga/Nagios monitoring system, which would alert me, if a service isn't running anymore, which should be running. As we've got many different servers for many different customers, it doesn't really scale to define a list of "important" services.

  • Which Ubuntu version? – A.B. Apr 24 '15 at 7:23
  • possible duplicate of Command to list services that start on startup? – Parto Apr 24 '15 at 8:04
  • @Parto The accepted answer in that question is initctl list which lists processes that are started/running, but that list would also include processes that are started manually – Sergiy Kolodyazhnyy Apr 24 '15 at 8:16
  • @A.B. I'm on Ubuntu 14.04. – Alexander Skwar May 4 '15 at 12:18
  • @Parto askubuntu.com/questions/218/… doesn't quite provide an answer. As @Serg wrote, the answer there was to use initctl list. But that doesn't really answer it, as it would also show manually started services. And it doesn't show services, which are not running. – Alexander Skwar May 4 '15 at 12:21
17

All services start on startup:

initctl list

List of all services which exist in system:

service --status-all
  • Wrong. Neither provide the list of enabled services (started at startup). They show all known services (some of which may be disabled) and whether or not they are currently running (which doesn't necessarily mean they are started automatically). – Reinier Post May 8 '17 at 15:27
2

Here's my oneliner grep -i 'runlevel' /etc/init/* | awk '!/#/ && /start on/ && /2/ {gsub("/"," "); print $0 }' | cut -d ' ' -f4-

A bit cleaner output can be achieved with grep -i 'runlevel' /etc/init/* | awk '/start on/ && /2/ {gsub("/"," "); gsub(":", " ");gsub(".conf"," "); print $3 }'

Now lets get into details. Upstart jobs are all stored in /etc/init/ , right ? The jobs there all have a lines stating at which runlevel they start. Runlevel 2 is a normal boot, default runlevel. So if we grep all the lines that say runlevel and clean output of grep with awk to match only lines with start on and runlevel #2, we get the list of upstart jobs. The rest is just for formating needs, replace / in file names to space, and cut away the path to those files, leaving filenames themselves.

The !/#/ part is to address possibly commented out start directives, which I had personally added manually.

Among other things,there is /etc/rc2.d where (if I understand correctly ) you have lesser priority scripts.

Crude one-liner for that is: ls /etc/rc2.d/* | grep S | awk '{sub("S"," "); print $2}

Scripts that in that folder that actually run on runlevel 2 are named starting with S and followed by priority number. Now, basically, we get all files starting with S, and just list them with only priority name (still have to figure out how to clean that up).

As muru properly pointed out in the comments, there is also /etc/init/*.override files, which may " . . .modify how a job will run without having to modify its configuration file directly" (init manpage)

At current moment, since I've not much experience with these files, I can only suggest looking through those .override files by using 'grep 'manual' /etc/init/*.override' to list services that are told to be started manually with initctl or services commands.

  • Note that because of event dependencies, it cannot easily be determined if a given service will result in other services being started. There's also the small matter of .override files. – muru Apr 24 '15 at 8:01
  • @Serg thanks. But that only deals with upstart stuff. Also "ls /etc/rc2.d/* ..." doesn't help much. It would show, which services might have been started. But it doesn't show, which services are still running. I guess, the main point of my post wasn't clear. I'm looking for: But I would like to know, if a service, that had been started at boot is still running. Don't know if you know Solaris. There, I'd simply do "svcs -x", and if it returns something, I know that something isn't running. I would need something like this for Ubuntu. – Alexander Skwar May 4 '15 at 12:26
  • Well one could write a script or bashrc function to compare outputs of my one liner there and of initctl list. Want me to post that ? – Sergiy Kolodyazhnyy May 4 '15 at 14:39
  • @Serg well — if you're offering ;) – Alexander Skwar May 5 '15 at 6:04
2

List all services and respective runlevel status - chkconfig style:

# sysv-rc-conf --list

acpid        2:on       3:on    4:on    5:on
apache-htcac 0:off      1:off   2:off   3:off   4:off   5:off   6:off
apache2      0:off      1:off   2:on    3:on    4:on    5:on    6:off

Interactive text based edit of all listed:

# sysv-rc-conf

l SysV Runlevel Config   -: stop service  =/+: start service  h: help  q: quit qk
x 
x service      1       2       3       4       5       0       6       S 
x ----------------------------------------------------------------------------
x acpid       [ ]     [X]     [X]     [X]     [X]     [ ]     [ ]     [ ]
x apache-ht$  [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]
x apache2     [ ]     [X]     [X]     [X]     [X]     [ ]     [ ]     [ ]
x
x Use the arrow keys or mouse to move around.      ^n: next pg     ^p: prev pg
x                        space: toggle service on / off

Might have to install it if not there:

apt install sysv-rc-conf

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.