Unix: Showing tasks by run state

what's running

The command to use to display what runs in each run state depends on the version of Linux that you are using – in particular, whether your system depends on the sysvinit start/kill scripts that have been around for decades, the upstart system which seems to be in the process of fading away, or system which seems to be taking over the job of booting Linux systems.

what's running

flickr / Mika Stetsovski


For systems which are still heavily based in the sysvinit camp, using /etc/rc.d files to start services, the chkconfig command provides a very convenient display. As you can see from the output below, each run state occupies a column in this output and for each service, designations like 1:off and 5:on who whether the service is set up to run when that run state is active.

# chkconfig --list | head -10
NetworkManager  0:off   1:off   2:off   3:off   4:off   5:off   6:off
VRTSralus.init  0:off   1:off   2:on    3:on    4:off   5:on    6:off
acpid           0:off   1:off   2:on    3:on    4:on    5:on    6:off
afx_server      0:off   1:off   2:off   3:on    4:on    5:on    6:off
anacron         0:off   1:off   2:on    3:on    4:on    5:on    6:off
atd             0:off   1:off   2:off   3:on    4:on    5:on    6:off
auditd          0:off   1:off   2:on    3:on    4:on    5:on    6:off
autofs          0:off   1:off   2:off   3:on    4:on    5:on    6:off
avahi-daemon    0:off   1:off   2:off   3:on    4:on    5:on    6:off
avahi-dnsconfd  0:off   1:off   2:off   3:off   4:off   5:off   6:off

To list just the services set up to run in some particular run state, you can use the same basic command and pipe the output to a grep command that looks for the run state you want to report on following by :on. Here’s an example:

# chkconfig --list | grep 5:on | awk '{print $1}' | head -10

This works because the chkconfig will show you what runs in each of the run states while the grep command specifically looks for those services which are enabled in run state 5. And then we narrow our output down to just the list of services. Obviously, you could use this same technique to list services running in any of the seven run states.

In a similar manner, you could display the services which are turned off in any particular run state or count the numbers of services enabled In any particular run state.

# chkconfig –list | grep 5:off | wc –l

If you run these commands on a system which is using systemd, they will only report on the services which are still running under the sysvinit framework. For newer versions of Linux (e.g., Fedora 20), this is likely to be a very small group – only 4-5 services in all likelihood.

The chkconfig command can also be used to get services to run or not run in particular run states with commands like chkconfig smbfs on and chkconfig smbfs off. The chkconfig command then manipulates the symbolic links that are sitting the /etc/rc.d/rc?.d (where ? is 0-6 or S) directories, creating links for services that should run and removing them when you request that they not run. The command is basically meant to make this aspect of administering a system a little less tedious by setting up the symlinks for you. The files the symlinks point at are stored in /etc/rc.d/init.d or /etc/rc.d itself, depending on the distribution you are using.

This story, "Unix: Showing tasks by run state" was originally published by ITworld.

Computerworld's IT Salary Survey 2017 results
Shop Tech Products at Amazon