Table of Contents

Process Control

Slackware systems often run hundreds or thousands of programs, each of
which is referred to as a process. Managing these processes is an
important part of system administration. So how exactly do we handle
all of these seperate processes?

ps

The first step in managing processes is figuring out what processes are
currently running. The most popular and powerful tool for this is
ps(1). Without any arguments,
ps won't tell you much information. By
default, it only tells you what processes are running in your currently
active shell. If we want more information, we'll need to look deeper.

Here you can see what processes you are running in your currently
active shell or terminal and only some information is included. The
PID is the “Process ID”; every process is assigned a unique number. The
TTY tells you what terminal device the process is attached to.
Naturally, CMD is the command that was run. You might be a little
confused by TIME though, since it seems to move so slowly. This isn't
the amount of real time the process has been running, but rather the
amount of CPU time the process has consumed. An idle process uses
virtually no CPU time, so this value may not increase quickly.

Viewing only our own processes isn't very much fun, so let's take a
look at all the processes on the system with the -e
argument.

As you can see, BSD syntax offers much more information, including what
user controls the process and what percentage of RAM and CPU the process
is consuming when ps is run.

To accomplish bits of this, on a per process basis, ps allows
one or more process IDs (PIDs) to be provided in the command line, and has the '-o' flag
to show a particular attribute of the PID.

darkstar:~$ ps -o cmd -o etime $$
CMD ELAPSED
/bin/bash 12:22

What this is displaying, is the PID's command name (cmd), and its elapsed time (etime).
The PID in this example, is a shell variable for the PID of the current shell. So you
can see, in this example, the shell process has existed for 12 minutes, 22 seconds.

In this example, a subshell execution, using pgrep,
is returning the PIDs of any process, whose command
name includes 'httpd'. Then ps displaying the command name,
resident memory size, and virtual memory size.

Finally, ps can also create a process tree.
This shows you which processes have children processes. Ending the
parent of a child process also ends the child. We do this with the
-ejH argument.

kill and killall

Managing processes isn't only about knowing which ones are running, but
also about communicating with them to change their behavior. The most
common way of managing a program is to terminate it. Thus, the tool for
the job is named kill(1). Despite the name,
kill doesn't actually terminate processes,
but sends signals to them. The most common signal is a SIGTERM, which
tells the process to finish up what it is doing and terminate. There
are a variety of other signals that can be sent, but the three most
common are SIGTERM, SIGHUP, and SIGKILL.

What a process does when it receives a signal varies. Most programs
will terminate (or attempt to terminate) whenever they receive any
signal, but there are a few important differences. For starters, the
SIGTERM signal informs the process that it should terminate itself at
its earliest convenience. This gives the process time to finish up any
important activities, such as writing information to the disk, before
it closes. In contrast, the SIGKILL signal tells the process to
terminate itself immediately, no questions asked. This is most useful
for killing processes that are not responding and is sometimes called
the “silver bullet”. Some processes (particularly daemons) capture the
SIGHUP signal and reload their configuration files whenever they
receive it.

In order to signal a process, we first need to know it's PID. You can
get this easily with ps as we discused. In
order to send different signals to a running process, you simply pass
the signal number and -s as an argument. The -l
argument lists all the signals you can choose and their number. You can
also send signals by their name with -s.

top

So far we've learned how to look at the active processes for a moment
in time, but what if we want to monitor them for an extended period?
top(1) allows us to do just that. It
displays an ordered list of the processes on your system, along with
vital information about them, and updates periodically. By default,
processes are ordered by their CPU percentage and updates occur every
three seconds.

The man page has helpful details on how to interact with
top such as changing its delay interval, the
order processes are displayed, and even how to terminate processes
right from within top itself.

cron

Ok, so we've learned many different ways of viewing the active
processes on our system and means of signalling them, but what if we
want to run a process periodically? Fortunately, Slackware includes
just the thing, crond(8). cron runs
processes for every user on the schedule that user demands. This makes
it very useful for processes that need to be run periodically, but
don't require full daemonization, such as backup scripts. Every user
gets their own entry in the cron database, so non-root users can
periodically run processes too.

In order to run programs from cron, you'll need to use the
crontab(1). The man page lists a variety of
ways to do this, but the most common method is to pass the
-e argument. This will lock the user's entry in the cron
database (to prevent it from being overwritten by another program),
then open that entry with whatever text editor is specified by the
VISUAL environment variable. On Slackware systems, this is typically
the vi editor. You may need to refer to the
chapter on vi before continuing.

The cron database entries may seem a little archaic at first, but they
are highly flexible. Each uncommented line is processed by
crond and the command specified is run if
all the time conditions match.

As mentioned before, the syntax for cron entries is a little difficult
to understand at first, so let's look at each part individually. From
left to right, the different sections are: Minute, Hour, Day, Month,
Week Day, and Command. Any asterisk * entry matches
every minute, hour, day, and so on. So from the example above, the
command is “/usr/local/bin/rsync-slackware64.sh 1>/dev/null 2>&1”, and
it runs every weekday or every week of every month at 2:30 a.m.

crond will also e-mail the local user with
any output the command generates. For this reason, many tasks have
their output redirected to /dev/null, a special
device file that immediately discards everything it receives. In order
to make it easier for you to remember these rules, you might wish to
paste the following commented text at the top of your own cron entries.

By default, Slackware includes a number of entries and comments in
root's crontab. These entries make it easier to setup periodic system
tasks by creating a number of directories in /etc
corresponding to how often the tasks should run. Any script placed
within these directories will be run hourly, daily, weekly, or monthly.
The names should be self-explanatory:
/etc/cron.hourly,
/etc/cron.daily,
/etc/cron.weekly, and
/etc/cron.monthly.