Top Banner
Part Workbook 9. Managing Processes
73

Part Workbook 9. Managing Processes

Mar 13, 2023

Download

Documents

Khang Minh
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Part Workbook 9. Managing Processes

Part Workbook 9. Managing Processes

Page 2: Part Workbook 9. Managing Processes

2

Table of Contents1. An Introduction to Processes ........................................................................................... 5

Discussion .............................................................................................................. 5Processes are How Things Get Done ................................................................... 5What is a Process? ........................................................................................... 5Viewing Processes with the ps Command ............................................................. 9

Process Selection .................................................................................... 10Output Selection ..................................................................................... 10Oddities of the ps Command .................................................................... 10

Monitoring Processes with the top Command ....................................................... 11Monitoring Processes with the gnome-system-monitor Application ........................... 12Locating processes with the pgrep Command. ...................................................... 14

Examples .............................................................................................................. 15Example 1. Viewing All Processes with the "User Oriented" Format .......................... 15Example 2. Viewing A User's Processes with the "Long" Format .............................. 15Example 3. Viewing A Particular Command with the "job oriented" Format ................ 16Example 4. Viewing Processes with a Custom Format ............................................ 16

Online Exercises .................................................................................................... 17Specification .................................................................................................. 17Deliverables ................................................................................................... 18

Questions .............................................................................................................. 182. Process States .............................................................................................................. 21

Discussion ............................................................................................................. 21A Process's Life Cycle .................................................................................... 21

How Processes are Started ....................................................................... 21The Lineage of Processes (and the pstree Command) ..................................... 23How a Process Dies ................................................................................ 24

The 5 Process States ....................................................................................... 24Viewing Process States .................................................................................... 26

Examples .............................................................................................................. 26Example 1. Identifying Process States ................................................................. 26

Online Exercises .................................................................................................... 27Specification .................................................................................................. 27Deliverables ................................................................................................... 28

Questions .............................................................................................................. 283. Process Scheduling: nice and renice ................................................................................ 31

Discussion ............................................................................................................. 31Process Scheduling Nomenclature ...................................................................... 31Process Scheduling, in Essence ......................................................................... 31

Process Priorities .................................................................................... 32Process Niceness .................................................................................... 32

Changing a Process's Niceness .......................................................................... 32Using nice to Start a low Priority Command ................................................ 33Using renice to Alter a Running Process ..................................................... 33Using top to Renice a Process ................................................................... 34Making Processes Greedier ....................................................................... 34

Examples .............................................................................................................. 35Example 1. Viewing Priorities ............................................................................ 35Example 2. Changing Priorities with renice .......................................................... 35

Online Exercises .................................................................................................... 36Specification .................................................................................................. 36Deliverables ................................................................................................... 36

Page 3: Part Workbook 9. Managing Processes

Managing Processes

3

Cleaning Up .................................................................................................. 36Questions .............................................................................................................. 36

4. Sending Signals ........................................................................................................... 40Discussion ............................................................................................................. 40

Signals ......................................................................................................... 40Why Are Signals Sent? .................................................................................... 41Sending Signals: the kill Command .................................................................... 41Receiving Signals ........................................................................................... 42Using Signals to Terminate Processes ................................................................. 42Alternatives to the kill Command ...................................................................... 43

The pkill Command ................................................................................ 43The killall Command ............................................................................... 44The System Monitor ............................................................................... 44The top Command .................................................................................. 45

Examples .............................................................................................................. 45Example 1. Using Signals to Terminate Processes .................................................. 45Example 2. Using Signals to Kill Processes .......................................................... 46Example 3. Make it Stop! ................................................................................. 46

Online Exercises .................................................................................................... 46Specification .................................................................................................. 46Deliverables ................................................................................................... 47

Questions .............................................................................................................. 475. Job Control ................................................................................................................. 50

Discussion ............................................................................................................. 50Running Commands in the Foreground ............................................................... 50Running Commands in the Background as Jobs .................................................... 50Managing Multiple Jobs .................................................................................. 51

Listing Current Jobs with jobs .................................................................. 51Killing Jobs ................................................................................................... 52Summary ...................................................................................................... 53

Examples .............................................................................................................. 53Example 1. Deciding to Background a Command Running in the Foreground .............. 53Example 2. Using CTRLC to Kill a Background Job .............................................. 53

Online Exercises .................................................................................................... 54Specification .................................................................................................. 54Deliverables ................................................................................................... 54Clean Up ...................................................................................................... 54

Questions .............................................................................................................. 546. Scheduling Delayed Tasks: at ......................................................................................... 58

Discussion ............................................................................................................. 58Daemons ....................................................................................................... 58The atd Daemon ............................................................................................. 58Submitting Jobs with at ................................................................................... 59Delaying Tasks with batch ............................................................................... 59Summary of at Commands ............................................................................... 60

Examples .............................................................................................................. 60Example 1. Submitting an at Job as a File ............................................................ 60Example 2. Examining the at Spool Syntax .......................................................... 60

Online Exercises .................................................................................................... 62Online Exercise 1. Submitting a job for Delayed Execution ..................................... 62

Specification .......................................................................................... 62Deliverables ........................................................................................... 62

Questions .............................................................................................................. 627. Scheduling Periodic Tasks: cron ..................................................................................... 65

Page 4: Part Workbook 9. Managing Processes

Managing Processes

4

Discussion ............................................................................................................. 65Performing Periodic Tasks ............................................................................... 65The cron Service ............................................................................................ 65crontab Syntax ............................................................................................... 65Using the crontab Command ............................................................................. 66Editing crontab Files in Place ........................................................................... 67Where does the output go? ............................................................................... 68Environment Variables and cron ........................................................................ 68

Examples .............................................................................................................. 69Example 1. Monitoring a Web Site ..................................................................... 69Example 2. Monitoring Large Files ..................................................................... 69Example 3. Running scripts from cron ................................................................. 70Example 4. Printing fanmail .............................................................................. 70

Online Exercises .................................................................................................... 71Online Exercise 1. Monitoring Who is on the System. ............................................ 71

Specification .......................................................................................... 71Deliverables ........................................................................................... 71

Questions .............................................................................................................. 71

Page 5: Part Workbook 9. Managing Processes

5

Chapter 1. An Introduction toProcesses

Key Concepts

• A process is an instance of a running executable, identified by a process id (pid).

• Because Linux implements virtual memory, every process possesses its own distinct memory context.

• A process has a uid and a collection of gid as credentials.

• A process has a filesystem context, including a cwd, a umask, a root directory, and a collection of openfiles.

• A process has a scheduling context, including a niceness value.

• A process has a collection of environment variables.

• The ps command can be used to examine all currently running processes.

• The top command can be used to monitor all running processes.

Discussion

Processes are How Things Get DoneAlmost anything that happens in a Linux system, happens as a process. If you are viewing this text in a webbrowser, that browser is running as a process. If you are typing at a bash shell's command line, that shellis running as a process. If you are using the chmod command to change a file's permissions, the chmodcommand operates as a separate process. Processes are how things get done, and the primary responsibilityof the Linux kernel is to provide a place for processes to do their stuff without stepping on each other's toes.

Processes are an instance of an executing program. In other operating systems, programs are often large,elaborate, graphical applications that take a noticeably long time to start up. In the Linux (and Unix)world, these types of programs exist as well, but so do a whole class of programs which usually have nocounterpart in other operating systems. These programs are designed to be quick to start, specialized infunction, and play well with others. On a Linux system, processes running these programs are constantlypopping into and out of existence. For example, consider the user maxwell performing the followingcommand line.

[maxwell@station maxwell]$ ps aux | grep httpd > daemons.$(date +%d%b%y)

In the split second that the command line took to execute, no less four than processes (ps, grep, bash, anddate) were started, did their thing, and exited.

What is a Process?By this point, you could well be tired of hearing the answer: a process in an instance of a running program.Here, however, we provide a more detailed list of the components that constitute a process.

Execution Context Every process exists (at least to some extent)within the physical memory of the machine.

Page 6: Part Workbook 9. Managing Processes

An Introduction to Processes

6

Because Linux (and Unix) is designed tobe a multiuser environment, the memoryallocated to a process is protected, and noother process can access it. In its memory,a process loads a copy of its executableinstructions, and stores any other dynamicinformation it is managing. A process alsocarries parameters associated with how oftenit gets the opportunity to access the CPU, suchas its execution state and its niceness value(more on these soon).

I/O Context Every process interacts to some extent withthe filesystem in order to read or writeinformation that exists before or will existafter the lifespan of the process. Elements ofa process's input/output context include thefollowing.

Open File Descriptors Almosteveryprocessisreadinginformationfromorwritinginformationtoexternalsources,usuallyboth.InLinux,openfiledescriptorsactassourcesorsinksofinformation.Processesreadinformationfromorwriteinformationto

Page 7: Part Workbook 9. Managing Processes

An Introduction to Processes

7

filedescriptors,whichmaybeconnectedtoregularfiles,devicenodes,networksockets,oreveneachotheraspipes(allowinginterprocesscommunication).

Memory Mapped Files Memorymappedfilesarefileswhosecontentshavebeenmappeddirectlyintotheprocess'smemory.Ratherthanreadingorwritingtoafiledescriptor,theprocessjustaccessestheappropriatememory

Page 8: Part Workbook 9. Managing Processes

An Introduction to Processes

8

address.Memorymapsaremostoftenusedtoloadaprocess'sexecutablecode,butmayalsobeusedforothertypesofnon-sequentialaccesstodata.

Filesystem Context Wehaveencounteredseveralpiecesofinformationrelatedtothefilesystemthatprocessesmaintain,suchastheprocess'scurrentworkingdirectory(fortranslatingrelativefilereferences)

Page 9: Part Workbook 9. Managing Processes

An Introduction to Processes

9

andtheprocess'sumask(forsettingpermissionsonnewlycreatedfiles).1

Environment Variables Every process maintains its own list ofname-value pairs, referred to as environmentvariables, or collectively as the process'senvironment. Processes generally inherit theirenvironment on startup, and may refer to itfor information such as the user's preferredlanguage or favorite editor.

Heritage Information Every process is identified by a PID, orprocess id, which it is assigned when it iscreated. In a later Lesson, we will discoverthat every process has a clearly definedparent and possibly well defined children.A process's own identity, the identity of itschildren, and to some extent the identity of itssiblings are maintained by the process.

Credentials Every process runs under the context of agiven user (or, more exactly, a given userid), and under the context of a collection ofgroup id's (generally, all of the groups that theuser belongs to). These credentials limit whatresources a process can access, such as whichfiles it can open or with which other processesit is allowed to communicate.

Resource Statistics and Limits Every process also records statistics to trackthe extent to which system resources havebeen utilized, such as its memory size, itsnumber of open files, its amount of CPU time,and others. The amount of many of theseresources that a process is allowed to usecan also be limited, a concept called resourcelimits.

Viewing Processes with the ps CommandWe have already encountered the ps command many times. Now, we will attempt to familiarize ourselveswith a broader selection of the many command line switches associated with it. A quick ps --helpwill display a summary of over 50 different switches for customizing the ps command's behavior. Tocomplicate matters, different versions of Unix have developed their own versions of the ps command,which do not use the same command line switch conventions. The Linux version of the ps command

Page 10: Part Workbook 9. Managing Processes

An Introduction to Processes

10

tries to be as accommodating as possible to people from different Unix backgrounds, and often there aremultiple switches for any give option, some of which start with a conventional leading hyphen (“-”), andsome of which do not.

Process Selection

By default, the ps command lists all processes started from a user's terminal. While reasonable whenusers connected to Unix boxes using serial line terminals, this behavior seems a bit minimalist whenevery terminal window within an X graphical environment is treated as a separate terminal. The followingcommand line switches can be used to expand (or reduce) the processes which the ps command lists.

Table 1.1. Common ps Command Line Switches for Process Selection

Switch Which Processes are Listed

-A, -e, ax All processes.

-C command All instances of command

-U, --user, --User user All processes belonging to user

-t, --tty terminal All processes started from terminal

-p, p, --pid N Process with pid N

Output Selection

As implied by the initial paragraphs of this Lesson, there are many parameters associated with processes,too many to display in a standard terminal width of 80 columns. The following table lists commoncommand line switches used to select what aspects of a process are listed.

Table 1.2. Common ps Command Switches for Output Selection

Switch Output Format

-f "full" listing

-l, l long format

-j, j jobs format

-o, o, --format str user defined format, using fields specified by str (Available fieldsfor str can be listed with ps L, or by consulting the ps(1) man page.)

Additionally, the following switches can be used to modify how the selected information is displayed.

Table 1.3. Common ps Command Switches for Output Formatting

Switch Output Format

-H Show process hierarchy

f, --forest Show process hierarchy including ASCII decorations

h Do not print header lines

-w "wide" output (include longer command names)

Oddities of the ps Command

The ps command, probably more so than any other command in Linux, has oddities associated with itscommand line switches. In practice, users tend to experiment until they find combinations that work forthem, and then stick to them. For example, the author prefers ps aux for a general purpose listing of all

Page 11: Part Workbook 9. Managing Processes

An Introduction to Processes

11

processes, while many people prefer ps -ef. The above tables should provide a reasonable "working set"for the novice.

The command line switches tend to fall into two categories, those with the traditional leading hyphen("Unix98" style options), and those without ("BSD" style options). Often, a given functionality will berepresented by one of each. When grouping multiple single letter switches, only switches of the same stylecan be grouped. For example, ps axf is the same as ps a x f, not ps a x -f.

Monitoring Processes with the top CommandThe ps command displays statistics for specified processes at the instant that the command is run, providinga snapshot of an instance in time. In contrast, the top command is useful for monitoring the general stateof affairs of processes on the machine.

The top command is intended to be run from within a terminal. It will replace the command line witha table of currently running processes, which updates every few seconds. The following demonstrates auser's screen after running the top command.

top - 08:18:11 up 2 days, 46 min, 1 user, load average: 0.00, 0.01, 0.00Tasks: 111 total, 1 running, 110 sleeping, 0 stopped, 0 zombieCpu(s): 0.0%us, 0.3%sy, 0.0%ni, 99.7%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%stMem: 1021740k total, 910724k used, 111016k free, 133604k buffersSwap: 2064376k total, 0k used, 2064376k free, 471164k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1652 root 20 0 122m 17m 5328 S 0.0 1.8 0:08.68 Xorg 1782 gdm 20 0 364m 14m 10m S 0.0 1.5 0:12.93 gdm-simple-gree 1765 gdm 20 0 329m 12m 9560 S 0.0 1.2 2:04.38 gnome-settings- 1781 gdm 20 0 276m 11m 9072 S 0.0 1.2 0:00.05 plymouth-log-vi 1780 gdm 20 0 302m 10m 8260 S 0.0 1.1 0:06.56 gnome-power-man 1778 gdm 20 0 275m 9576 7152 S 0.0 0.9 0:01.68 metacity 1740 gdm 20 0 257m 7584 5960 S 0.0 0.7 0:00.16 gnome-session 1779 gdm 20 0 236m 7096 5580 S 0.0 0.7 0:00.01 polkit-gnome-au 1747 gdm 20 0 129m 5172 2084 S 0.0 0.5 0:01.34 gconfd-2 1261 root 20 0 97408 5064 4176 S 0.0 0.5 0:00.11 NetworkManager 1385 haldaemo 20 0 26056 4784 4024 S 0.0 0.5 0:01.95 hald 1565 root 20 0 256m 4532 3308 S 0.0 0.4 0:00.61 abrtd 1764 gdm 20 0 116m 4260 3428 S 0.0 0.4 0:02.40 at-spi-registry 1794 gdm 9 -11 402m 4020 3004 S 0.0 0.4 0:00.08 pulseaudio 1785 root 20 0 49924 3952 2772 S 0.0 0.4 0:00.15 polkitd 8054 root 20 0 97636 3648 2844 S 0.0 0.4 0:00.09 sshd 1183 root 20 0 242m 3464 948 S 0.0 0.3 0:00.07 rsyslogd

While the command is running, the keyboard is "live". In other words, the top command will respond tosingle key presses without waiting for a return key. The following table lists some of the more commonlyused keys.

Table 1.4. Commonly used top Commands

Key Press Command

q quit

h or ? help

s set the delay between updates (in seconds)

space update display

M Sort processes by Memory Size

P Sort processes by CPU (Processor) Activity

F or O Select a sort field

Page 12: Part Workbook 9. Managing Processes

An Introduction to Processes

12

Key Press Command

< > Move sort field: '<' next col left; '>' next col right

u Reduce display to processes owned by a specific user

k Kill a process (send a process a signal)

r Renice a process

The last two command, which either kill or renice a process, use concepts that we will cover in more detailin a later Lesson.

Although most often run without command line configuration, top does support the following commandline switches.

Table 1.5. Command Line Switches for the top Command

Switch Effect

-d secs Delay secs seconds between refreshes (Default = 5 seconds).

-q Refresh as often as possible.

-n N Run for N iterations, then exit.

-b Run in "batch mode", writing simply as if to a dumb terminal.

Monitoring Processes with the gnome-system-monitorApplication

If running an X server, the GNOME desktop environment provides an application similar in function totop, with the benefits (and drawbacks) of a graphical application. The application can be started from thecommand line as gnome-system-monitor, or by selecting the System : Administration : System Monitormenu item.

Figure 1.1. The GNOME System Monitor

Page 13: Part Workbook 9. Managing Processes

An Introduction to Processes

13

Like the top command, the System Monitor displays a list of processes running on the local machine,refreshing the list every few seconds. In its default configuration, the System Monitor provides a muchsimpler interface: it lists only the processes owned by the user who started the application, and reducesthe number of columns to just the process's command, owner, Process ID, and simple measures of theprocess's Memory and CPU utilization. Processes may be sorted by any one of these fields by simplyclicking on the column's title.

When right-clicking on a process, a pop-up menu allows the user to perform many of the actions that topallowed, such as renicing or killing a process, though again with a simpler (and not as flexible) interface.

Figure 1.2. Right Clicking on a Process in the GNOME System Monitor

The System Monitor may be configured by opening the Edit : Preferences menu selection. Within thePreferences dialog, the user may set the update interval (in seconds), and configure many more fields tobe displayed.

Figure 1.3. Configuring Display Fields in the GNOME System Monitor

Page 14: Part Workbook 9. Managing Processes

An Introduction to Processes

14

Locating processes with the pgrep Command.Often, users are trying to locate information about processes identified by the command they are running,or the user who is running them. One technique is to list all processes, and use the grep command to reducethe information. In the following, maxwell first looks for all instances of the sshd daemon, and then forall processes owned by the user maxwell.

[maxwell@station maxwell]$ ps aux | grep sshdroot 1474 0.0 0.1 63944 1072 ? Ss Jul18 0:00 /usr/sbin/sshdmaxwell 14716 0.0 0.0 103240 824 pts/0 S+ 08:51 0:00 grep sshd[maxwell@station maxwell]$ ps aux | grep maxwellroot 14684 0.0 0.4 163332 4600 pts/0 S 08:51 0:00 su - maxwellmaxwell 14691 0.0 0.1 108324 1792 pts/0 S 08:51 0:00 -bashmaxwell 14721 13.1 5.8 589760 60220 pts/0 Sl 08:52 0:02 /usr/lib64/firefox-3.6/firefoxmaxwell 14754 0.2 0.4 132912 5056 ? S 08:52 0:00 /usr/libexec/gconfd-2maxwell 14772 0.0 0.1 108052 1048 pts/0 R+ 08:53 0:00 ps auxmaxwell 14773 0.0 0.0 103236 820 pts/0 S+ 08:53 0:00 grep maxwell

While maxwell can find the information he needs, there are some unpleasant issues.

1. The approach is not exacting. Notice that, in the second search, a su process showed up, not because itwas owned by maxwell, but because the word maxwell was one of its arguments.

2. Similarly, the grep command itself usually shows up in the output.

3. The compound command can be awkward to type.

In order to address these issues, the pgrep command was created. Named pgrep for obvious reasons, thecommand allows users to quickly list processes by command name, user, terminal, or group.

pgrep [SWITCHES] [PATTERN]

Its optional argument, if supplied, is interpreted as an extended regular expression pattern to be matchedagainst command names. The following command line switches may also be used to qualify the search.

Table 1.6. Common Command Line Switches for Specifying pgrep ProcessSelection Criteria

Switch Effect

-n Select only the newest (most recently started) matching process.

-u USER Select processes owned by the user USER.

-t TERM Select processes controlled by terminal TERM.

In addition, the following command line switches can be use to qualify the output formatting of thecommand.

Table 1.7. Common Command Line Switches for Specifying pgrep OutputFormatting

Switch Effect

-d delimiter Use delimiter to delimit each process ID (by default, a newlineis used).

-l List process name as well as process ID.

For a complete list of switches, consult the pgrep(1) man page.

Page 15: Part Workbook 9. Managing Processes

An Introduction to Processes

15

As a quick example, maxwell will repeat his two previous process listings, using the pgrep command.

[maxwell@station maxwell]$ pgrep -l sshd1474 sshd[maxwell@station maxwell]$ pgrep -lu maxwell 14691 bash14721 firefox14754 gconfd-2

Examples

Viewing All Processes with the "User Oriented" FormatIn the following transcript, maxwell uses the ps -e u command to list all processes (-e) with the "useroriented" format (u).

[maxwell@station maxwell]$ ps -e uUSER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMANDroot 1 0.0 0.0 1380 76 ? S Oct12 0:04 init [root 2 0.0 0.0 0 0 ? SW Oct12 0:00 [keventd]root 3 0.0 0.0 0 0 ? SW Oct12 0:00 [kapmd]...root 174 0.0 0.0 0 0 ? SW Oct12 0:00 [kjournald]root 250 0.0 0.0 1356 4 tty2 S Oct12 0:00 /sbin/mingetty ttroot 496 0.0 0.1 2104 448 ? S Oct12 0:00 /sbin/dhclient -1root 566 0.0 0.0 1448 160 ? S Oct12 0:00 syslogd -m 0root 570 0.0 0.0 1376 164 ? S Oct12 0:00 klogd -xrpc 588 0.0 0.0 1548 104 ? S Oct12 0:00 portmap...maxwell 4202 0.0 1.3 57948 3400 ? S Oct12 0:02 nautilus --no-defmaxwell 4204 0.0 0.1 16392 436 ? S Oct12 0:00 magicdev --sm-climaxwell 4207 0.0 0.5 16784 1500 ? S Oct12 0:00 eggcups --sm-cliemaxwell 4210 0.0 0.3 11596 988 ? S Oct12 0:00 pam-panel-icon --maxwell 4212 0.1 0.8 24464 2152 ? S Oct12 0:41 /usr/bin/python /root 4213 0.0 0.0 1416 136 ? S Oct12 0:00 /sbin/pam_timestamaxwell 4220 0.0 0.3 17024 1012 ? S Oct12 0:00 /usr/libexec/notimaxwell 4293 2.4 1.4 18356 3760 ? S Oct12 15:28 gnome-system-moniapache 5048 0.0 0.6 18424 1776 ? S Oct12 0:00 /usr/sbin/httpd...maxwell 13166 0.7 0.5 4304 1392 pts/5 S 07:35 0:00 -bashmaxwell 13200 0.0 0.2 2696 744 pts/5 R 07:35 0:00 ps -e u

The "user oriented" view displays the user who is running the process, the process id, and a rough estimateof the amount of CPU and memory the process is consuming, as well as the state of the process. (Processstates will be discussed in the next Lesson).

Viewing A User's Processes with the "Long" FormatIn the following transcript, maxwell uses the ps -U maxwell l command to list his own processes (-Umaxwell) with the "long " format (l).

[maxwell@station maxwell]$ ps -U maxwell lF UID PID PPID PRI NI VSZ RSS WCHAN STAT TTY TIME COMMAND4 515 4132 1062 15 0 18632 4 schedu S ? 0:02 /usr/bin/gnom1 515 4175 4132 15 0 3068 72 schedu S ? 0:00 /usr/bin/ssh-0 515 4180 1 15 0 11384 776 schedu S ? 0:00 /usr/libexec/0 515 4182 1 15 0 6364 4 schedu S ? 0:00 /usr/libexec/0 515 4184 1 15 0 17336 4 schedu S ? 0:00 gnome-setting0 515 4193 1 15 0 3728 620 schedu S ? 0:00 xscreensaver0 515 4196 1 15 0 12816 1884 schedu S ? 0:08 /usr/bin/meta0 515 4200 1 15 0 21160 3340 schedu S ? 0:05 gnome-panel -0 515 4202 1 15 0 57948 3192 schedu S ? 0:02 nautilus --no

Page 16: Part Workbook 9. Managing Processes

An Introduction to Processes

16

0 515 4204 1 15 0 16392 424 schedu S ? 0:00 magicdev --sm0 515 4207 1 15 0 16784 1348 schedu S ? 0:00 eggcups --sm-0 515 4210 1 15 0 11596 908 schedu S ? 0:00 pam-panel-ico0 515 4212 1 15 0 24464 2152 schedu S ? 0:43 /usr/bin/pyth0 0 4213 4210 15 0 1416 136 schedu S ? 0:00 /sbin/pam_tim0 515 4220 1 15 0 17024 756 schedu S ? 0:00 /usr/libexec/0 515 4293 1 15 0 18356 3760 schedu S ? 15:43 gnome-system-4 515 13166 13163 15 0 4304 1388 wait4 S pts/5 0:00 -bash0 515 13201 4193 25 10 6676 2592 schedu SN ? 0:00 pulsar -root0 515 13265 13166 20 0 3140 1188 - R pts/5 0:00 ps -U maxwell

The long format focuses on scheduling parameters, such as the priority and niceness of the process, whichwill be discussed in a later Lesson.

Viewing A Particular Command with the "job oriented"Format

In the following transcript, maxwell uses the ps -C bash j command to list all instances of the bashcommand (-C bash) with the "job oriented " format (j).

[maxwell@station maxwell]$ ps -C bash j PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND 1184 2311 2311 2311 pts/4 2311 S 2291 0:01 bash 1184 2565 2565 2565 pts/0 2565 S 2291 0:04 bash 1184 2757 2757 2757 pts/2 2757 S 2291 0:00 bash 1184 3024 3024 3024 pts/3 3052 S 2291 0:00 bash 1184 3348 3348 3348 pts/6 3348 S 2291 0:00 bash 1184 6033 6033 6033 pts/5 13414 S 2291 0:00 bash 1184 6534 6534 6534 pts/8 6534 S 2291 0:00 bash13163 13166 13166 6033 pts/5 13414 S 515 0:00 -bash

The job oriented format focuses on parent processes, process groups, session groups, and controllingterminals. Many of these concepts will be discussed in later workbooks.

Curious that the parent process of many of these shells seems to be process ID 1184, maxwell goes on toexamine that individual process in detail.

[maxwell@station maxwell]$ ps u 1184USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMANDeinstein 1184 0.2 3.3 26900 8660 ? S Oct12 2:51 /usr/bin/gnome-te

Apparently, all of the bash shells were started from within a gnome-terminal.

Viewing Processes with a Custom FormatCurious to see what aspects of a process can be displayed with the ps command, maxwell uses ps L tolist all possible headers.

[maxwell@station maxwell]$ ps L%cpu %CPU%mem %MEMalarm ALARMargs COMMANDblocked BLOCKEDbsdstart STARTbsdtime TIMEc C...vsize VSZvsz VSZwchan WCHAN

Page 17: Part Workbook 9. Managing Processes

An Introduction to Processes

17

Curious about the majflt field, maxwell wants to see how many major memory faults processes have had,and uses the -o command line switch to list the number of faults and the command.

[maxwell@station maxwell]$ ps -e -o majflt,cmdMAJFLT CMD 41 init [5] 0 [migration/0] 0 [ksoftirqd/0] 0 [watchdog/0]... 0 auditd 14 python /sbin/audispd 0 syslogd -m 0 0 klogd -x 0 mcstransd 1 portmap 40 /usr/bin/python -E /usr/sbin/setroubleshootd 0 rpc.statd

To better organize the output, he sorts (reverse) numerically, and only lists the top 5 processes.

[maxwell@station ~]$ ps -e -o majflt,cmd | sort -rn | head 42 /usr/bin/python /usr/sbin/yum-updatesd 41 init [5] 40 /usr/bin/python -E /usr/sbin/setroubleshootd 24 /usr/sbin/gdm-binary -nodaemon 14 python /sbin/audispd 12 nautilus --no-default-window --sm-client-id default3 8 /usr/bin/python -E /usr/bin/sealert -s 5 /usr/lib/firefox-1.5.0.9/firefox-bin 5 cupsd 5 automount

Online ExercisesLab Exercise

Objective: View Process Information

Estimated Time: 20 mins.

SpecificationIn order to have a fixed set of processes to examine, you are to take a snapshot of all current processes onyour machine. Use the following sequence of commands to first capture the headers for the columns ofthe ps aux command into a file called snapshot. Next rerun the ps aux command, stripping the headerand sorting the remainder of the output by the size of each process's virtual memory. The sorted list ofprocesses will then be appended to the previously captured header in the file snapshot.

It's easier than it sounds.

[student@station student]$ ps aux | head -1 > snapshot[student@station student]$ ps aux --noheader | sort -rn -k4 >> snapshot[student@station student]$ head -5 snapshotUSER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMANDeinstein 3057 0.2 7.4 97088 18932 ? S Oct12 2:01 /usr/bin/galeon-root 1063 14.3 6.2 36528 15936 ? S Oct12 163:48 /usr/X11R6/bin/Xeinstein 1184 0.2 3.8 27160 9868 ? S Oct12 3:00 /usr/bin/gnome-teinstein 1164 0.0 3.0 27856 7792 ? S Oct12 0:11 /usr/bin/python

Use your snapshot file to answer the following questions.

Page 18: Part Workbook 9. Managing Processes

An Introduction to Processes

18

1. In the file ~/biggest.pid, store the process ID of the process with the largest size of virtual memory(the VSZ column).

2. Experiment with the cut command, until you can extract the initial single letter column of the STATField. It should be filled almost exclusively “R”'s and “S”'s, implying that processes are in theRunning or Sleeping state. Save this one extracted column (minus the header!) into a file called ~/pstates.txt.

3. Use the grep command, perhaps with the wc command, to determine how many instances of theprogram /sbin/mingetty are running. Store the answer as a single number in the file ~/nmingetty.txt.

4. Use the grep command, perhaps with the wc command, to determine how many processes are runningas the user root. Store the answer as a single number in the file ~/nroot.txt.

5. Start the top command in a terminal, and leave it running while you grade your exercise.

Do not edit or remove your snapshot file until you have finished grading your exercise.

Deliverables1.

1. The file ~/biggest.pid, which contains the process ID of the process with the largest virtualmemory.

2. The file ~/pstates.txt, which contains the extracted column of process states, minus theheader line.

3. The file ~/nmingetty.txt, which contains the number of instances of the /sbin/mingetty program were running on your machine.

4. The file ~/nroot.txt, which contains the number of processes running as the user root onthe machine.

5. A running instance of the top command.

Questions1. Which of the following commands can be used to view processes running on a Red Hat Enterprise

Linux Machine?

a. ps

b. top

c. gnome-system-monitor

d. A and B

e. All of the Above

2. Which of the following command lines would list all processes for the user maxwell?

a. ps -a maxwell

b. ps -k maxwell

Page 19: Part Workbook 9. Managing Processes

An Introduction to Processes

19

c. ps -U maxwell

d. ps -l maxwell

e. None of the above

3. When running the top command, which key is used to sort processes by CPU activity?

a. C

b. A

c. P

d. U

e. None of the above

4. When running the top command, which key is used to sort processes by Memory size?

a. M

b. S

c. V

d. T

e. None of the above

5. Which of the following command lines would display a listing for every process on the machine?

a. ps -e l

b. ps ax f

c. ps aux

d. ps -A j

e. All of the above

6. When using the GNOME System Monitor, how does one add new fields to the display?

a. By right clicking in any column title, and choosing "Add New Column".

b. By choosing the Edit:Preferences menu selection.

c. By pressing the C key.

d. By clicking the "More Info" button.

e. None of the above

7. When using the GNOME System Monitor, how does one change the order in which processes aresorted?

a. By pressing the S key.

Page 20: Part Workbook 9. Managing Processes

An Introduction to Processes

20

b. By clicking in the appropriate column title.

c. By choosing the Edit:Preferences menu selection.

d. By clicking the "Sort By" button.

e. None of the above

8. Which of the following commands lists fields available for custom formatting of the ps command'soutput?

a. ps l

b. ps --list

c. ps V

d. ps --columns

e. None of the above

9. Which of the following would list all instances of the httpd command?

a. ps --cmd httpd

b. ps p httpd

c. ps -C httpd

d. ps -l httpd

e. None of the above

10. Which of the following command line switches to the ps command is used to specify customformatting?

a. -o

b. -c

c. --custom

d. -f

e. None of the above

Page 21: Part Workbook 9. Managing Processes

21

Chapter 2. Process StatesKey Concepts

• In Linux, the first process, /sbin/init, is started by the kernel on bootup. All other processes are the resultof a parent process duplicating itself, or forking.

• A process begins executing a new command through a process called execing.

• Often, new commands are run by a process (often a shell) first forking, and then execing. Thismechanism is referred to as the fork and exec mechanism.

• Processes can always be found in one of five well defined states: runnable, voluntarily sleeping,involuntarily sleeping, stopped, or zombie.

• Process ancestry can be viewed with the pstree command.

• When a process dies, it is the responsibility of the process's parent to collect it's return code and resourceusage information.

• When a parent dies before it's children, the orphaned children are inherited by the first process (usually/sbin/init).

Discussion

A Process's Life Cycle

How Processes are Started

In Linux (and Unix), unlike many other operating systems, process creation and command execution aretwo separate concepts. Though usually a new process is created so that it can run a specified command(such as the bash shell creating a process to run the chmod command), processes can be created withoutrunning a new command, and new commands can be executed without creating a new process.

Creating a New Process (Forking) New processes are created through atechnique called forking. When a processforks, it creates a duplicate of itself.Immediately after a fork, the newly createdprocess (the child) is an almost exactduplicate of the original process (the parent).The child inherits an identical copy of theoriginal process's memory, any open filesof the parent, and identical copies of anyparameters of the parent, such as the currentworking directory or umask. About the onlydifference between the parent and the childis the child's heritage information (the childhas a different process ID and a differentparent process ID, for starters), and (for theprogrammers in the audience) the return valueof the fork() system call.

Page 22: Part Workbook 9. Managing Processes

Process States

22

As a quick aside for any programmers in theaudience, a fork is usually implemented usinga structure similar to the following.

int rc, child_pid; rc = fork(); if (rc == -1) { perror("bad fork"); } else if (rc == 0) { do_child(); } else { child_pid = rc; do_parent(child_pid); }

When a process wants to create a newprocess, it calls the fork() system call (withno arguments). Though only one processenters the fork() call, two processes returnfrom in. For the newly created process (thechild), the return value is 0. For the originalprocess (the parent), the return value is theprocess ID of the child. By branching onthis value, the child may now go off to dowhatever it was started to do (which ofteninvolves exec()ing, see next), and the parentcan go on to do its own thing.

Executing a New Command (Exec-ing) New commands are run through a techniquecalled execing (short for executing). Whenexecing a new command, the current processwipes and releases most of its resources,and loads a new set of instructions fromthe command specified in the filesystem.Execution starts with the entry point of thenew program.

After execing, the new command is still thesame process. It has the same process ID,and many of the same parameters (such as itsresource utilization, umask, current workingdirectory, and others). It merely forgets itsformer command, and adopts the new one.

Again for any programmers, execs areperformed through one of several variants ofthe execve() system call, such as the execl()library call.

rc = execl("chmod", "chmod 755 /etc/passwd"); perror("bad exec");

The process enters the the execl(...) call,specifying the new command to run. If allgoes well, the execl(...) call never returns.Instead, execution picks up at the entry point(i.e., main()) of the new program. If for somereason execl(...) does return, it must be an

Page 23: Part Workbook 9. Managing Processes

Process States

23

error (such as not being able to locate thecommand's executable in the filesystem).

Combining the Two: Fork and Exec Some programs may fork without execing.Examples include networking daemons, whofork a new child to handle a specific clientconnection, while the parent goes back tolisten for new clients. Other programs mightexec without forking. Examples include thelogin command, which becomes the user'slogin shell after successfully confirming auser's password. Most often, and for shell's inparticular, however, forking and execing gohand and hand. When running a command,the bash shell first forks a new bashshell. The child then execs the appropriatecommand, while the parent waits for the childto die, and then issues another prompt.

The Lineage of Processes (and the pstree Command)

Upon booting the system, one of the responsibilities of the Linux kernel is to start the first process (usually/sbin/init). All other processes are started because an already existing process forked. 1

Because every process except the first is created by forking, there exists a well defined lineage of parentchild relationships among the processes. The first process started by the kernel starts off the family tree,which can be examined with the pstree command.

[maxwell@station maxwell]$ pstree init-+-apmd |-atd |-automount |-battstat-applet ... |-evolution-execu |-evolution-mail |-fetchmail |-galeon-bin |-gconfd-1 |-2*[gconfd-2] |-gdm-binary-+-gdm-binary-+-X | | `-gnome-session---ssh-agent | `-gdm-binary---gnome-session---ssh-agent |-2*[gnome-panel] |-2*[gnome-settings-] |-gnome-system-mo |-gnome-terminal-+-3*[bash] | |-bash---man---sh---sh---less | |-bash---mutt | |-bash---su---bash---pstree | `-gnome-pty-helpe |-gpm |-gvim |-httpd---11*[httpd] |-kapmd |-keventd |-khubd

1 Linux also includes kernel threads, which in special situations may fork and exec to become user processes, confusing matters. This behavior isnot usually found in traditional Unix, however, and is very much the exception instead of the rule.

Page 24: Part Workbook 9. Managing Processes

Process States

24

...

How a Process Dies

When a process dies, it either dies normally by electing to exit, or abnormally as the result of receiving asignal. We here discuss a normally exiting process, postponing a discussion of signals until a later Lesson.

We have mentioned previously that processes leave behind a status code (also called return value) whenthey die, in the form of an integer. (Recall the bash shell, which uses the $? variable to store the returnvalue of the previously run command.) When a process exits, all of its resources are freed, except the returncode (and some resource utilization accounting information). It is the responsibility of the process's parentto collect this information, and free up the last remaining resources of the dead child. For example, whenthe bash shell forks and execs the chmod command, it is the parent bash shell's responsibility to collectthe return value from the exited chmod command.

Orphans

If it is a parent's responsibility to clean up after their children, what happens if the parent dies before thechild does? The child becomes an orphan. One of the special responsibilities of the first process startedby the kernel is to "adopt" any orphans. (Notice that in the output of the pstree command, the first processhas a disproportionately large number of children. Most of these were adopted as the orphans of otherprocesses).

Zombies

In between the time when a process exits, freeing most of its resources, and the time when its parentcollects its return value, freeing the rest of its resources, the child process is in a special state referred toas a Zombie. Every process passes through a transient zombie state. Usually, users need to be looking atjust the right time (with the ps command, for example) to witness a zombie. They show up in the list ofprocesses, but take up no memory, no CPU time, or any other system resources. They are just the shadowof a former process, waiting for their parent to come and finish them off.

Negligent Parents and Long Lived Zombies

Occasionally, parent processes can be negligent. They start child processes, but then never go back toclean up after them. When this happens (usually because of a programmer's error), the child can exit, enterthe zombie state, and stay there. This is usually the case when users witness zombie processes using, forexample, the ps command.

Getting rid of zombies is perhaps the most misunderstood basic Linux (and Unix) concept. Many peoplewill say that there is no way to get rid of them, except by rebooting the machine. Using the clues discussedin this section, can you figure out how to get rid of long lived zombies?

You get rid of zombies by getting rid of the negligent parent. When the parent dies (or is killed), the noworphaned zombie gets adopted by the first process, which is almost always /sbin/init. /sbin/initis a very diligent parent, who always cleans up after its children (including adopted orphans).

The 5 Process States

The previous section discussed how processes are started, and how they die. While processes are alivethey are always in one of five process states, which effect how and when they are allowed to have accessto the CPU. The following lists each of the five states, along with the conventional letter that is used bythe ps, top, and other commands to identify a process's current state.

Page 25: Part Workbook 9. Managing Processes

Process States

25

Runnable (R) Processes in the Runnable state are processesthat, if given the opportunity to access theCPU, would take it. More formally, thisis know as the Running state, but becauseonly one process may be executing on theCPU at any given time, only one of theseprocesses will actually be "running" at anygiven instance. Because runnable processesare switched in and out of the CPU soquickly, however, the Linux system givesthe appearance that all of the processes arerunning simultaneously.

Voluntary (Interruptible) Sleep (S) As the name implies, a process which isin a voluntary sleep elected to be there.Usually, this is a process that has nothingto do until something interesting happens. Aclassic example is a networking daemon, suchas the httpd process that implements a webserver. In between requests from a client (webbrowser), the server has nothing to do, andelects to go to sleep. Another example wouldbe the top command, which lists processesevery five seconds. While it is waiting forfive seconds to pass, it drops itself into avoluntary sleep. When something that theprocess in interested in happens (such as aweb client makes a request, or a five secondtimer expires), the sleeping process is kickedback into the Runnable state.

Involuntary (Non-interruptible) Sleep (D) Occasionally, two processes try to access thesame system resource at the same time. Forexample, one process attempts to read froma block on a disk while that block is beingwritten to because of another process. In thesesituations, the kernel forces the process intoan involuntary sleep. The process did notelect to sleep, it would prefer to be runnableso it can get things done. When the resourceis freed, the kernel will put the process backinto the runnable state.

Although processes are constantly droppinginto and out of involuntary sleeps, theyusually do not stay there long. As a result,users do not usually witness processes in aninvoluntary sleep except on busy systems.

Stopped (Suspended) Processes (T) Occasionally, users decide to suspendprocesses. Suspended processes will notperform any actions until they are restarted bythe user. In the bash shell, the CTRL+Z keysequence can be used to suspend a process.In programming, debuggers often suspend

Page 26: Part Workbook 9. Managing Processes

Process States

26

the programs the are debugging when certainevents happen (such as breakpoints occur).

Zombie Processes (Z) As mentioned above, every dieing processgoes through a transient zombie state.Occasionally, however, some get stuck there.Zombie processes have finished executing,and have freed all of their memory andalmost all of their resources. Because they arenot consuming any resources, they are littlemore than an annoyance that can show up inprocess listings.

Viewing Process StatesWhen viewing the output of commands such as ps and top, process states are usually listed under theheading STAT. The process is identified by one of the following letters.

• Runnable - R

• Sleeping - S

• Stopped - T

• Uninterruptible sleep - D

• Zombie - Z

Examples

Identifying Process States[maxwell@station maxwell]$ ps -alx F UID PID PPID PRI NI VSZ RSS WCHAN STAT TTY TIME COMMAND

100 0 1 0 15 0 1344 436 schedu S ? 0:06 init100 500 4248 775 15 0 4136 1412 wait4 S tty3 0:00 -bash100 500 4292 776 15 0 4144 1420 schedu S tty4 0:00 -bash

004 0 1829 1774 17 0 1472 528 down D pts/3 0:00 updatedb004 0 1827 1774 16 0 1464 520 - R pts/3 0:00 updatedb

000 500 4333 4292 15 0 7612 2616 do_sig T tty4 0:00 vim proj1_s000 500 4334 4248 15 0 3612 1052 schedu S tty3 2:57 top

004 501 5486 1220 16 0 0 0 do_exi Z ? 0:00 [netstat 000 501 5793 2600 15 0 7708 2816 wait4 S pts/0 0:00 vim c000 501 5798 5793 16 0 3804 944 wait4 S pts/0 0:00 /bin/bash -040 501 5799 5798 17 0 3808 1000 wait4 S pts/0 0:00 /bin/bash -

000 501 5800 5799 17 0 3148 1240 - R pts/0 0:00 ps -alx000 501 5801 5799 17 0 3144 420 pipe_w S pts/0 0:00 tail

A (voluntarily) sleeping process. The init process is waiting for something "interesting" to happen,such a newly created orphan to inherit.A (involuntarily) sleeping, or "blocked" process. The updatedb command is competing for someresource on the system, probably with the other instance of the updatedb process just below it.A stopped process. This vim editor has probably been manually suspended with the CTRL+Z keysequence.A zombie process, probably left by a negligent galeon web browser.A runnable process, in this case the running ps command.

Page 27: Part Workbook 9. Managing Processes

Process States

27

Online ExercisesLab Exercise

Objective: Explore different process states

Estimated Time: 10 mins.

Specification1. In order to explore process states, we must create processes which are competing for the same resources.

Use a simple text editor to create the following script, store it as ~/clobber_it.sh, and make itexecutable.

[maxwell@station maxwell]$ cat clobber_it.sh#!/bin/bash for i in $(seq 1000); do echo "hello world" > poor_overworked_filedone

2. While this script will write to the poor, overworked file 1000 times, it will do it sequentially, and sowill never be competing for access to the file. Because we need multiple processes competing over thesame resource, create the following script as well. Name it ~/clobber_it_lots.sh, and makeit executable.

[maxwell@station maxwell]$ cat clobber_it_lots.sh#!/bin/bash for i in $(seq 1000); do ./clobber_it.sh &done

That should do the trick.

3. In one terminal, run the command top -q. This should run the top command, updating continuously.While top is running, use the U command to limit the display to your own processes (i.e., type in yourown username).

4. When we say go, in a separate terminal (either another terminal window in an X graphical environment,or a separate virtual console), run the script really_clobber_it.sh. This will start about 1000processes on your machine, which will obviously stress the system, but it should be able to handle it. 2

The system may seem sluggish, but with patience, it should still be responsive. If things get unbearable,the script can be canceled with a CTRL+C. We haven't said go yet.

5. While the script is running, observe the terminal with the top command. You should see many processesstarting and stopping, as well as many processes in the “D” (involuntary sleep) state. If you are lucky,you might even catch a zombie. We haven't said go yet.

6. Also while the script is running, in yet another terminal, run the ps aux command, and redirect theoutput to the file ~/lotsa_processes.txt. Glance at the contents of the script, and make surethat at lest five processes in the “D” state have been recorded. We still haven't said go yet.

7. Go.

8. After you have created your ~/lotsa_processes.txt file, and feel that you have gotten the point,you can cancel the clobber_it_lots.sh script with a CTRL+C, and quit the top command.

Page 28: Part Workbook 9. Managing Processes

Process States

28

Deliverables1.

1. The file ~/lotsa_processes.txt, which contains the output of the ps aux command,with at least 5 instances of a process in the “D” (uninterruptible sleep) state.

Questions1. If a process were expecting keyboard input before continuing, which state would it be in?

a. voluntary sleep

b. involuntary sleep

c. runnable

d. stopped

e. zombie

2. The Apache Web Server uses multiple httpd processes, so it can serve multiple concurrent requests.If hundreds of were to request information stored in the same file concurrently, which state wouldmost of the httpd processes be found in?

a. stopped

b. voluntary sleep

c. zombie

d. runnable

e. involuntary sleep

3. A physics simulation is performing intensive numerical calculations. What state would the processprobably be found in?

a. voluntary sleep

b. runnable

c. involuntary sleep

d. stopped

e. zombie

4. You have suspended the vi editor with the CTRL+Z key combination. Which state would theprocess be found in?

a. runnable

b. voluntary sleep

c. involuntary sleep

d. stopped

Page 29: Part Workbook 9. Managing Processes

Process States

29

e. zombie

5. You are running a complicated application (such as evolution), that uses many subprocesses toperform its various tasks. Occasionally, some of the subprocesses seem to be finished (taking nomemory or CPU time), but still show up in the process list. What state are these subprocesses in?

a. zombie

b. stopped

c. runnable

d. involuntary sleep

e. voluntary sleep

6. While observing a list of processes, your friend is concerned about a few zombie processes whichshow up. What advice should you give her?

a. Your friend should reboot the machine when its convenient, because the zombie processeswill slowly begin consuming more and more resources.

b. Your friend should not be too concerned. They are not consuming resources, and will go awaywhen their parent process dies. Besides, they're fun to talk about.

c. Your friend should reboot the machine immediately, before the zombie processes begininfecting other processes, turning them into zombies as well. What a ghoulish nightmare thatwould be.

d. Your friend should log out and log back on again to get rid of the zombies.

e. None of the above suggestions apply.

7. What does Linux (and Unix) call the act of creating a new process?

a. spawning

b. forking

c. launching

d. execing

e. None of the above

8. What does Linux (and Unix) call the act of executing a new command?

a. spawning

b. forking

c. launching

d. execing

e. None of the above

9. Which letter does ps and top use to represent the involuntary sleep state?

Page 30: Part Workbook 9. Managing Processes

Process States

30

a. I

b. T

c. D

d. Z

e. None of the above

10. Which letter does ps and top use to represent the stopped (suspended) state?

a. I

b. T

c. D

d. Z

e. None of the above

Page 31: Part Workbook 9. Managing Processes

31

Chapter 3. Process Scheduling: niceand renice

Key Concepts

• A primary task of the Linux kernel is scheduling processes.

• Every process has a niceness value that influences its scheduling.

• The nice and renice commands can change a process's scheduling priority.

Discussion

Process Scheduling NomenclatureOne of the fundamental tasks of the Linux kernel is ensure that processes share system resourceseffectively. One of the most fundamental resources which has to be shared is the CPU. How the kerneldecides which process gets to execute on the CPU at which time is known as scheduling.

Every process has two values which influence its scheduling. The first is a dynamic value which isconstantly being changed by the kernel. The second is a fixed value, which is only occasionally (if ever)explicitly changed by a user. In the open source community, the nomenclature used to describe these twovalues has been inconsistent (at best), which leads to confusion. As much as possible, this text will try tobe consistent with the ps and top command, and refer to the first (dynamic) value as the process's priority,and the second (fixed) value as the process's niceness.

Process Scheduling, in EssenceRecently, much attention has been focused on methods used by the Linux kernel to implement scheduling,and the technique has varied from kernel release to kernel release. While the following discussion is notcorrect at a detailed level, it nevertheless conveys the essence of how the Linux kernel schedules processes.

In order to more easily illustrate scheduling, maxwell will start four versions of the cat command, runningin the background. (Processes can be run in the background by appending an ampersand (“& ”), as will bediscussed in a later Lesson). The cat commands read from /dev/zero (a pseudo device that acts as aninfinite source of binary zeros), and write to /dev/null (a pseudo device which throws away everythingthat is written to it).

[maxwell@station maxwell]$ cat /dev/zero > /dev/null &[1] 6698[maxwell@station maxwell]$ cat /dev/zero > /dev/null &[2] 6699[maxwell@station maxwell]$ cat /dev/zero > /dev/null &[3] 6700[maxwell@station maxwell]$ cat /dev/zero > /dev/null &[4] 6701

How long will these cat commands run? Forever. The user maxwell next monitors the processes on hismachine using the top command.

top - 07:41:10 up 51 min, 2 users, load average: 3.55, 1.39, 0.51Tasks: 123 total, 8 running, 115 sleeping, 0 stopped, 0 zombie

Page 32: Part Workbook 9. Managing Processes

Process Scheduling: nice and renice

32

Cpu(s): 21.0%us, 79.0%sy, 0.0%ni, 0.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%stMem: 512172k total, 507376k used, 4796k free, 34212k buffersSwap: 530136k total, 0k used, 530136k free, 311180k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 6698 maxwell 25 0 3692 448 384 R 25.3 0.1 0:27.55 cat 6699 maxwell 25 0 3688 448 384 R 25.0 0.1 0:27.74 cat 6700 maxwell 25 0 3688 444 384 R 25.0 0.1 0:27.51 cat 6701 maxwell 25 0 3688 444 384 R 24.7 0.1 0:27.74 cat 1 root 15 0 2044 628 536 S 0.0 0.1 0:00.21 init 2 root RT 0 0 0 0 S 0.0 0.0 0:00.00 migration/0 3 root 34 19 0 0 0 S 0.0 0.0 0:00.00 ksoftirqd/0 4 root RT 0 0 0 0 S 0.0 0.0 0:00.00 watchdog/0 5 root 10 -5 0 0 0 S 0.0 0.0 0:00.02 events/0 ...

While watching the top command, maxwell observes that the values in the third column (labeled PRI)are constantly changing. These are the process's dynamic "priority" values mentioned above. The fourthcolumn (labeled NI) is the fixed "niceness" value of the process.

Process Priorities

When scheduling processes, the kernel effectively gives every process a handful of counters. Every timea process gets scheduled onto the CPU, it gives up one of its counters. When deciding which process toschedule onto the CPU next, the kernel chooses the runnable process with the most counters. Eventually,the kernel will reach a state where all of the runnable processes have used up their counters. This is referredto as the end of a scheduling epoch, and at this point, the kernel starts all of the processes over again witha new handful of counters.

Notice that processes which are not in the runnable state never give up their counters. If, however, asleeping process were to suddenly awaken (because something interesting happened) and be kicked intothe runnable state, it would most likely have more counters than processes which had been running for awhile, and would be quickly scheduled onto the CPU.

How does this relate to the values shown in the PRI column? Think of this column as a process's numberof counters, subtracted from 40. Therefore, processes with a lower priority (as listed by the top command)have the scheduling advantage. In the output above, the cat commands, which are constantly in therunnable state, are consuming their counters. The init process, however, who is sleeping quietly in thebackground, is not.

Process Niceness

As mentioned above, every process also has a static value referred to as its niceness value. This value mayrange from -20 to 19 for any process, starting at 0 by default. How does a process's niceness influence itsscheduling? At the beginning of a scheduling epoch, you can think of the kernel subtracting a process'sniceness value from the number of counters the process is allocated. As a result, "nicer" processes (thosewith a higher niceness value) get less counters, and thus less time on the CPU, while "greedy" processes(those with a niceness value less than 0) get more counters and more time on the CPU. If a "nice" processis the only one running on the machine, however, it would get full access to the CPU.

Changing a Process's NicenessSuppose maxwell were about to run a physics simulation that would take several days to complete. Byincreasing the process's niceness, the process would patiently wait if anyone else were running processeson the machine. If no one else were running processes, however, the physics simulation would have fullaccess to the CPU.

Page 33: Part Workbook 9. Managing Processes

Process Scheduling: nice and renice

33

There are several techniques by which maxwell could alter his process's niceness value.

Using nice to Start a low Priority Command

The nice command is used to set a process's niceness as the process is started. When maxwell starts hissimulation (which is an executable in his home directory named ~/simulation), he makes it as niceas possible, with a value of +19. (He also places the process in the background. Again, don't worry aboutthat now. It will be discussed in a later Lesson.)

[maxwell@station maxwell]$ nice -19 ./simulation &

Notice that the syntax can be misleading. The token -19 should not be considered negative 19, but insteadthe numeric command line switch 19. The user maxwell then again monitors processes using the topcommand. The first few processes listed are listed below.

PID USER PRI NI SIZE RSS SHARE STAT %CPU %MEM TIME CPU COMMAND 7192 maxwell 25 0 400 400 352 R 22.7 0.1 0:35 0 cat 7193 maxwell 25 0 400 400 352 R 22.7 0.1 0:35 0 cat 7191 maxwell 25 0 400 400 352 R 21.9 0.1 0:36 0 cat 7194 maxwell 25 0 404 404 352 R 21.9 0.1 0:35 0 cat 1184 einstein 15 0 11140 8708 3144 R 4.7 3.4 1:28 0 gnome-termina

7198 maxwell 39 19 404 404 352 R N 2.1 0.1 0:02 0 simulation 4293 maxwell 15 0 5164 5016 2384 S 1.7 1.9 6:07 0 gnome-system-

As a convenience, the ps command decorates the process state with a “N”, to indicate the processhas had its niceness value increased.Because other cat commands are using the machine, maxwell's simulation is only getting about 2.1%of the CPU.

Next, maxwell gets rid of the cat commands (using techniques we will learn next Lesson).

[maxwell@station maxwell]$ killall cat[maxwell@station maxwell]$[3] Terminated cat /dev/zero >/dev/null[4] Terminated cat /dev/zero >/dev/null[5] Terminated cat /dev/zero >/dev/null[6] Terminated cat /dev/zero >/dev/null

When he observes the top command again, his simulation, now (almost) the lone runnable process on themachine, is receiving almost all of the CPU's time.

PID USER PRI NI SIZE RSS SHARE STAT %CPU %MEM TIME CPU COMMAND 7198 maxwell 39 19 404 404 352 R N 94.4 0.1 0:51 0 simulation 7210 maxwell 20 0 1120 1120 852 R 2.8 0.4 0:00 0 top 1063 root 15 0 33516 17M 832 S 1.9 7.0 47:01 0 X 4212 maxwell 15 0 5828 3012 1072 S 0.9 1.1 0:21 0 rhn-applet-gu 1 root 15 0 108 76 52 S 0.0 0.0 0:04 0 init

As an additional subtlety, the number specified is the number to be added to the current shell's nicenessvalue. Since most shells run with a niceness of 0, this is seldom noticed. But if a shell were running with aniceness value of 10, the following command line would result in the simulation running with a nicenessvalue of 15.

[maxwell@station maxwell]$ nice -5 ./simulation &

Using renice to Alter a Running Process

The renice command can be used to change the niceness of an already running process. Processes can bespecified by process id, username, or group name, depending on which of the following command lineswitches are used.

Page 34: Part Workbook 9. Managing Processes

Process Scheduling: nice and renice

34

Table 3.1. Command Line Switches for the renice Command

Switch Effect

-p interpret remaining arguments as process ID's (the default)

-u interpret remaining arguments as usernames.

-g interpret remaining arguments as group ID's.

Suppose maxwell had already started his simulation, without altering its niceness value.

[maxwell@station maxwell]$ ps -C simulation uUSER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND

maxwell 7348 58.9 0.1 3408 400 pts/5 R 01:29 0:50 simulation [maxwell@station maxwell]$ ps -C simulation lF UID PID PPID PRI NI VSZ RSS WCHAN STAT TTY TIME COMMAND

0 515 7348 6064 25 0 3408 400 - R pts/5 0:51 simulation

The process is receiving large amounts of CPU time.The process has the default niceness value of 0.

He decides to be more polite to other people who might be using the machine, and uses the renice commandto bump up the process's niceness value. In the absence of any command line switches, the renice commandexpects a niceness value and a process ID as its two arguments.

[maxwell@station maxwell]$ renice 19 73477348: old priority 0, new priority 19

Notice the inconsistency in syntax. The renice command, unlike the nice command, does not expect theniceness value to be specified as a command line switch, but as an argument. In fact, renice -19 7347would be interpreted as a niceness value of -19. Further muddying the waters, the output of the renicecommand refers to the value as a priority, instead of a niceness. (as some of the related documentationdoes, as well).

Using top to Renice a Process

As mentioned in the previous unit, the top command uses the r key to renice a process. While monitoringprocesses with top, pressing r will open the following dialog above the list of processes.

PID to renice: 7347Renice PID 7347 to value: 19

Making Processes Greedier

What if maxwell were more malicious in intent, and wanted to make his simulation greedier instead ofnicer? Fortunately for other users on the machine, normal users cannot lower the niceness of a process.This has two implications.

1. Because processes start with a default niceness of 0, standard users cannot make "greedy" processeswith negative niceness values.

2. Once a process has been made nice, it cannot be made "normal" again by normal users.

Suppose the administrator noticed that maxwell's simulation was taking up excessive amounts of CPUtime. She could use the renice command as root to bump up maxwell's niceness, and maxwell could notrestore it.

Page 35: Part Workbook 9. Managing Processes

Process Scheduling: nice and renice

35

Examples

Viewing PrioritiesBelow are several screen shots of ps -alx as a long process runs. Notice the "PRI" and "nice" fields overtime. They are fields 5 and 6, reading from left to right.

[maxwell@station maxwell]$ find / 2>/dev/null >/dev/null &[1] 2739[maxwell@station maxwell]$ ps -alx F UID PID PPID PRI NI VSZ RSS WCHAN STAT TTY TIME COMMAND...000 501 2676 898 15 0 4232 1544 wait4 S pts/0 0:00 bash

000 501 2739 2676 18 0 3284 644 - R pts/0 0:00 find /000 501 2740 2611 16 0 3804 948 wait4 S pts/1 0:00 /bin/bash -040 501 2741 2740 16 0 3808 1004 wait4 S pts/1 0:00 /bin/bash -000 501 2742 2741 17 0 3144 1232 - R pts/1 0:00 ps -alx000 501 2743 2741 17 0 3144 420 pipe_w S pts/1 0:00 tail...

[maxwell@station maxwell]$ ps -alx F UID PID PPID PRI NI VSZ RSS WCHAN STAT TTY TIME COMMAND...000 501 2676 898 16 0 4232 1544 wait4 S pts/0 0:00 bash000 501 2718 2611 15 0 3808 944 wait4 S pts/1 0:00 /bin/bash -040 501 2719 2718 16 0 3812 1000 wait4 S pts/1 0:00 /bin/bash -000 501 2720 2719 17 0 3148 1232 - R pts/1 0:00 ps -alx000 501 2721 2719 17 0 3136 384 - R pts/1 0:00 tail

000 501 2739 2676 17 0 3248 576 - R pts/0 0:00 find /...

The priority of the find process fluctuates over time.The niceness, by contrast, remains fixed at 0.

Next, maxwell uses the renice command to alter the niceness of the process.

[maxwell@station maxwell]$ renice 5 2739[maxwell@station maxwell]$ ps -alx000 501 2982 898 15 0 4228 1544 schedu S pts/0 0:00 bash

000 501 2739 2676 20 5 3248 576 - RN pts/0 0:00 find /000 501 3010 2611 16 0 3804 948 wait4 S pts/1 0:00 /bin/bash -040 501 3011 3010 17 0 3808 1004 wait4 S pts/1 0:00 /bin/bash -000 501 3012 3011 17 0 3140 1228 - R pts/1 0:00 ps -alx000 501 3013 3011 17 0 3140 416 pipe_w S pts/1 0:00 tail

Because the niceness value has been increased, the process consistently has higher priority values(implying less access to the CPU per scheduling epoch).The new niceness value.

Changing his mind, maxwell decides to restore the niceness to its default value of 0.

[maxwell@station maxwell]$ renice 0 2739renice: 2739: setpriority: Permission denied

Because standard users are not allowed to lower niceness values, the command fails.

Changing Priorities with reniceUsing the -u command line switch, the user maxwell can change the niceness values for all of his processesat once.

Page 36: Part Workbook 9. Managing Processes

Process Scheduling: nice and renice

36

[maxwell@station maxwell]$ ps -lu maxwellF S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD4 S 515 3031 3028 0 75 0 - 1078 wait4 pts/4 00:00:00 bash1 S 515 8954 1 0 79 0 - 3313 schedu ? 00:00:00 gvim0 S 515 8958 3031 1 80 0 - 1078 wait4 pts/4 00:00:00 bash0 R 515 8984 8958 0 83 0 - 779 - pts/4 00:00:00 ps[maxwell@station maxwell]$ renice 5 -u maxwell515: old priority 0, new priority 5[maxwell@station maxwell]$ ps -lu maxwellF S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD4 S 515 3031 3028 0 85 5 - 1078 wait4 pts/4 00:00:00 bash1 S 515 8954 1 0 85 5 - 3313 schedu ? 00:00:00 gvim0 S 515 8958 3031 0 80 5 - 1078 wait4 pts/4 00:00:00 bash0 R 515 8986 8958 0 86 5 - 779 - pts/4 00:00:00 ps

Online ExercisesLab Exercise

Objective: Change the scheduling priorities of processes.

Estimated Time: 10 mins.

Specification1. Run the following command in a terminal.

[student@station student]$ cat /dev/zero > /dev/null

2. In another terminal, use the renice command to change the niceness value of all processes owned by youto 5. (You might want to consider using the pgrep command in conjunction with the xargs commandfor this step.)

3. After completing the last step, change the niceness value of the cat process (started in step 1) to 10.

4. Use the nice command to start another cat command (again reading /dev/zero redirected to /dev/null) with a niceness value of 15.

Grade your exercise with both instances of the cat command still running.

Deliverables1.

1. A cat command running with a niceness value of 10.

2. A cat command running with a niceness value of 15.

3. All other processes running by you have a niceness value of 5.

Cleaning UpWhen you are finished grading your exercise, you may stop all of your cat processes with the CTRL+Ccontrol sequence.

QuestionsUse the following output from the top command to answer the next 6 questions.

Page 37: Part Workbook 9. Managing Processes

Process Scheduling: nice and renice

37

PID USER PRI NI SIZE RSS SHARE STAT %CPU %MEM TIME CPU COMMAND 9311 maxwell 24 0 400 400 352 R 15.9 0.1 0:03 0 sim_a 9312 maxwell 24 0 400 400 352 R 15.9 0.1 0:03 0 sim_a 9313 maxwell 24 0 400 400 352 R 14.3 0.1 0:03 0 sim_a 9308 maxwell 30 5 400 400 352 R N 11.1 0.1 0:07 0 sim_b 9307 maxwell 30 5 400 400 352 R N 10.5 0.1 0:07 0 sim_b 9305 maxwell 30 5 516 516 452 R N 9.7 0.2 0:18 0 sim_b 9306 maxwell 30 5 404 404 352 R N 9.7 0.1 0:08 0 sim_b 2648 maxwell 16 0 9752 8496 2980 R 6.5 3.3 2:00 0 gnome-termina 9309 maxwell 37 15 404 404 352 R N 2.7 0.1 0:01 0 sim_c 9310 maxwell 37 15 400 400 352 R N 2.7 0.1 0:01 0 sim_c 9314 maxwell 15 0 1132 1132 864 R 0.3 0.4 0:00 0 top 1 root 15 0 108 76 52 S 0.0 0.0 0:04 0 init 2 root 15 0 0 0 0 SW 0.0 0.0 0:00 0 keventd 3 root 15 0 0 0 0 SW 0.0 0.0 0:00 0 kapmd 4 root 34 19 0 0 0 SWN 0.0 0.0 0:00 0 ksoftirqd_CPU

1. What is the niceness value of the sim_c processes?

a. 15

b. 37

c. 2.7

d. 404

e. None of the above

2. What is the niceness value of the sim_a processes?

a. 0

b. 24

c. 400

d. 352

e. None of the above

3. Which of the following command lines could have been used to start the sim_c command,consistent with the output above?

a. nice +15 sim_c

b. nice -15 sim_c

c. nice 15 sim_c

d. nice --value=15 sim_c

e. None of the above

4. Which of the following command lines could be used to alter the niceness of one or all of the sim_acommands to 19?

a. renice -c sim_a 19

b. renice 19 9311

Page 38: Part Workbook 9. Managing Processes

Process Scheduling: nice and renice

38

c. renice -c sim_a -19

d. renice -19 9311

e. None of the above

5. If you were to look at the output of the same top command in another few seconds, what wouldyou expect to see for the priority of the sim_b simulations (assuming machine activity does notalter much in the interim)?

a. 30

b. 5

c. 400

d. Although you cannot say exactly, you would expect it to be an integer near 30.

e. Not enough information is provided.

6. Which of the following command lines could maxwell use to lower the niceness of one of hissim_c processes to 10?

a. renice 10 9309

b. renice -10 9309

c. renice +10 9309

d. Normal users cannot lower the niceness value of processes.

e. Not enough information is provided.

7. Which of the following commands can be used to set a process's niceness value?

a. nice

b. renice

c. top

d. All of the above

e. None of the above

8. Which of the following commands can be used to set (what ps and top call) a process's priority?

a. nice

b. renice

c. top

d. All of the above

e. None of the above

9. Which key does top use to change a process's niceness value?

Page 39: Part Workbook 9. Managing Processes

Process Scheduling: nice and renice

39

a. n

b. r

c. c

d. p

e. None of the above

Refer to the following transcript when answering the next question.

[maxwell@station maxwell]$ renice 10 -u maxwell515: old priority 0, new priority 10[maxwell@station maxwell]$ ps l -u maxwellF UID PID PPID PRI NI VSZ RSS WCHAN STAT TTY TIME COMMAND4 515 9245 9242 25 10 4312 1376 wait4 SN pts/4 0:00 -bash0 515 9445 9245 31 10 3112 1160 - RN pts/4 0:00 ps l -u maxwe

10. Which of the following best explains why the ps command has a niceness value of 10?

a. The renice command set the niceness value of all processes owned by the user maxwell to10, including the ps process.

b. The renice command set the default niceness value of all processes that maxwell starts to 10.

c. When the bash shell forked to execute the ps command, the child process inherited theniceness value of its parent, which was 10.

d. The renice command set the default niceness levels of all processes on the machine to 10.

e. None of the above adequately explains why the ps command has a niceness value of 10.

Page 40: Part Workbook 9. Managing Processes

40

Chapter 4. Sending SignalsKey Concepts

• Signals are a low level form of inter-process communication, which arise from a variety of sources,including the kernel, the terminal, and other processes.

• Signals are distinguished by signal numbers, which have conventional symbolic names and uses. Thesymbolic names for signal numbers can be listed with the kill -l command.

• The kill command sends signals to other processes.

• Upon receiving a signal, a process may either ignore it, react in a kernel specified default manner, orimplement a custom signal handler.

• Conventionally, signal number 15 (SIGTERM) is used to request the termination of a process.

• Signal number 9 (SIGKILL) terminates a process, and cannot be overridden.

• The pkill and killall commands can be used to deliver signals to processes specified by command name,or the user who owns them.

• Other utilities, such as top and the GNOME System Monitor can be used to deliver signals as well.

DiscussionSignals

Linux (and Unix) uses signals to notify processes of abnormal events, and as a primitive mechanism ofinterprocess communication. Signals are sometimes referred to as software interrupts, in that they caninterrupt the normal flow of execution of a process. The kernel uses signals to notify processes of abnormalbehavior, such as if the process tries to divide a number by zero, or tries to access memory that does notbelong to it.

Processes can also send signals to other processes. For example, a bash shell could send a signal to anxclock process. The receiving process knows very little about the origins of the signal. It doesn't know ifthe signal originated from the kernel, or from another process; all it knows is that it received a signal.

Figure 4.1. Xclock Receiving a Signal Number 15 From Bash

There are, however, different flavors of signals. The different flavors have symbolic names, but are alsoidentified by integers. The various integers, and the symbolic name they are mapped to, can be listed usingthe kill -l command, or examined in the signal(7) man page.

[einstein@station einstein]$ kill -l 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR213) SIGPIPE 14) SIGALRM 15) SIGTERM 17) SIGCHLD

Page 41: Part Workbook 9. Managing Processes

Sending Signals

41

18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO30) SIGPWR 31) SIGSYS 33) SIGRTMIN 34) SIGRTMIN+135) SIGRTMIN+2 36) SIGRTMIN+3 37) SIGRTMIN+4 38) SIGRTMIN+539) SIGRTMIN+6 40) SIGRTMIN+7 41) SIGRTMIN+8 42) SIGRTMIN+943) SIGRTMIN+10 44) SIGRTMIN+11 45) SIGRTMIN+12 46) SIGRTMIN+1347) SIGRTMIN+14 48) SIGRTMIN+15 49) SIGRTMAX-15 50) SIGRTMAX-1451) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-1055) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-659) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-263) SIGRTMAX-1

Linux, like most versions of Unix, implements 32 "normal" signals. In Linux, signals numbered 32 through63 (which are not standard among the various versions of Unix) are "real time" signals, and beyond thescope of this text.

Why Are Signals Sent?There are a variety of reasons why signals might be sent to a process, as illustrated by the followingexamples.

Hardware Exceptions The process asked the hardware to perform someerroneous operation. For example, the kernel will send aprocess a SIGFPE (signal number 8) if it performs a divideby 0.

Software Conditions Processes may need to be notified of some abnormalsoftware condition. For example, whenever a processdies, the kernel sends a SIGCHLD (signal number 17)to the process's parent. As another example, X graphicalapplications receive a SIGWINCH (signal number 28)whenever their window is resized, so that they can respondto the new geometry.

Terminal Interrupts Various terminal control key sequences send signals to thebash shell's foreground process. For example, CTRL+Csends a SIGINT (signal number 2), while CTRL+Z sendsa SIGTSTP (signal number 20).

Other Processes Processes may elect to send any signals to any otherprocess which is owned by the same user. The killcommand is designed to do just this.

Sending Signals: the kill CommandThe kill command is used to deliver custom signals to other processes. It expects to be called with anumeric or symbolic command line switch, which specifies which signal to send, and a process ID, whichspecifies which process should receive it. As an example, the following commands deliver a SIGCHLD(signal number 17) to the xclock process, process ID number 8060.

[einstein@station einstein]$ ps PID TTY TIME CMD 7985 pts/5 00:00:00 bash 8060 pts/5 00:00:01 xclock 8061 pts/5 00:00:00 ps[einstein@station einstein]$ kill -17 8060[einstein@station einstein]$ kill -CHLD 8060[einstein@station einstein]$ kill -SIGCHLD 8060

Page 42: Part Workbook 9. Managing Processes

Sending Signals

42

When using the symbolic name to specify a signal, the “SIG” prefix (which all signals share) can eitherbe included or omitted.

Receiving SignalsWhen a process receives a signal, it may take one of the following three actions.

Implement a Kernel Default Signal Handler For each type of the signal, there is a defaultresponse which is implemented by the kernel.Each signal is mapped to one of the followingbehaviors.

• Terminate: The receiving process is killed.

• Ignore: The receiving process ignores thesignal

• Core: The receiving process terminates,but first dumps an image of its memoryinto a file named core in the process'scurrent working directory. The core filecan be used by developers to help debug theprogram. This response if affectionatelyreferred to as "puking" by many in the Unixcommunity.

• Stop: Stop (suspend) the process.

The signal(7) man page documents whichbehavior is mapped to which signal.

Choose to Ignore the Signal Programmers may elect for their applicationto simply ignore specified signals.

Choose to Implement a Custom Signal Handler Programmers may elect to implement theirown behavior when a specified signal isreceived. The response of the program iscompletely determined by the programmer.

Unless a program's documentation says otherwise, you can usually assume that a process will respond withthe kernel implemented default behavior. Any other response should be documented.

Using Signals to Terminate ProcessesOf the 32 signals used in Linux (and Unix), standard users in practice only (explicitly) make use of a few.

Table 4.1. Important Signals for Standard Users

Number Symbol Action

2 SIGINT Interrupt (request termination) of the process. Thisis the signal generated by the CTRL+C controlsequence.

9 SIGKILL Force termination of the process (This signal maynot be overridden by the process).

Page 43: Part Workbook 9. Managing Processes

Sending Signals

43

Number Symbol Action

15 SIGTERM Request Termination of the process.

20 SIGTSTP Stop (suspend) the process. This is the signalgenerated by the CTRL+Z control sequence.

Usually, standard users are using signals to terminate a process (thus the name of the kill command). Byconvention, if programmers want to implement custom behavior when shutting down (such as flushingimportant memory buffers to disk, etc.), they implement a custom signal handler for signal number 15 toperform the action. Signal number 9 is handled specially by the kernel, and cannot be overridden by acustom signal handler or ignored. It is reserved as a last resort, kernel level technique for killing a process.

As an example, einstein will start a cat command that would in principle run forever. He then tracks downthe process ID of the command, and terminates it with a SIGTERM.

[einstein@station einstein]$ cat /dev/zero > /dev/null &[1] 8375[einstein@station einstein]$ ps PID TTY TIME CMD 7985 pts/5 00:00:00 bash 8375 pts/5 00:00:01 cat 8376 pts/5 00:00:00 ps[einstein@station einstein]$ kill -15 8375[einstein@station einstein]$[1]+ Terminated cat /dev/zero >/dev/null

SIGTERM (signal number 15) is the default signal for the kill command, so einstein could have used kill8375 to the same effect. In the following, einstein repeats the sequence, this time sending a SIGKILL.

[einstein@station einstein]$ cat /dev/zero > /dev/null &[1] 8387[einstein@station einstein]$ ps PID TTY TIME CMD 7985 pts/5 00:00:00 bash 8387 pts/5 00:00:01 cat 8388 pts/5 00:00:00 ps[einstein@station einstein]$ kill -9 8387[einstein@station einstein]$[1]+ Killed cat /dev/zero >/dev/null

Alternatives to the kill Command

Using signals to control processes is such a common occurrence, alternatives to using the kill commandabound. The following sections mention a few.

The pkill Command

In each of the previous examples, einstein needs to determine the process ID of a process before sendinga signal to it with the kill command. The pkill command can be used to send signals to processes selectedby more general means. The pkill command expects the following syntax.

pkill [-signal] [SWITCHES] [PATTERN]

The first token optionally specifies the signal number to send (by default, signal number 15). PATTERNis an extended regular expression that will be matched against command names. The following table listscommonly used command line switches. Processes that meet all of the specified criteria will be sent thespecified signal.

Page 44: Part Workbook 9. Managing Processes

Sending Signals

44

Table 4.2. Common Command Line Switches for the pkill Command

Switch Effect

-n Select only the newest (most recently started) matching process.

-u USER Select processes owned by the user USER.

-t TERM Select processes controlled by terminal TERM.

Conveniently, the pkill command omits itself and the shell which started it when killing all processesowned by a particular user or terminal. Consider the following example.

[maxwell@station maxwell]$ ps PID TTY TIME CMD10353 pts/4 00:00:00 bash10396 pts/4 00:00:00 gnome-calculator10397 pts/4 00:00:03 firefox10422 pts/4 00:00:00 ps[maxwell@station maxwell]$ pkill -u maxwell[1]- Terminated gnome-calculator[2]+ Terminated firefox[maxwell@station maxwell]$ ps PID TTY TIME CMD10353 pts/4 00:00:00 bash10424 pts/4 00:00:00 ps

Notice that, although the bash shell qualifies as a process owned by the user maxwell, it survived theslaughter.

The killall Command

Similar to pkill, the killall command delivers signals to processes specified by command name. The killallcommand supports the following command line switches.

Table 4.3. Command Line Switches for the killall Command

Switch Effect

-i, --interactive Interactively query the user before delivering a signal to a process.

-w, --wait Wait until all processes are killed before returning.

The System Monitor

The System Monitor GNOME application, introduced in a previous Lesson, can also be used to deliversignals to processes. By right clicking on a process, a pop-up menu allows the user to select End Process,which has the effect of delivering a SIGTERM to the process. What do you think the Kill Process menuselection does?

The Kill Process menu selection delivers a SIGKILL signal to the process.

Page 45: Part Workbook 9. Managing Processes

Sending Signals

45

Figure 4.2. Terminating Processes using the System Monitor

The top Command

The top command can , can also be used to deliver signals to processes. Using the K key, the followingdialog occurs above the list of processes, allowing the user to specify which process ID should receivethe signal, and which signal to deliver.

PID to kill: 4859Kill PID 4859 with signal [15]: 9

Examples

Using Signals to Terminate ProcessesSignals are usually the only way to terminate processes that are not being managed by a shell's commandline. For example, graphical X clients (applications) can be killed by delivering a SIGTERM to theappropriate process. In the following example, the user maxwell is running both the gnome-calculatorgraphical calculator and the Firefox web browser. He uses the pgrep command to locate their process ID's,kills the first using the kill command, and the second using the pkill command.

[maxwell@station maxwell]$ pgrep -lu maxwell14691 bash14834 firefox14875 gnome-calculato[maxwell@station maxwell]$ kill 14875[maxwell@station maxwell]$ pkill firefox[1]- Terminated firefox[2]+ Terminated gnome-calculator[maxwell@station maxwell]$ pgrep -lu maxwell14691 bash

But wait, you argue. There is an easy way to terminate graphical applications, other than sending signals.Just click the "go away" box in the upper right hand corner, or select Close from the drop down menu inthe upper left corner. Behind the scenes, that's exactly what these actions do: deliver a SIGTERM signalto the enclosed application.

Page 46: Part Workbook 9. Managing Processes

Sending Signals

46

Using Signals to Kill ProcessesWhenever possible, the SIGTERM signal should be used to terminate processes. Sometimes, however,processes are "locked up", and completely unresponsive, even to SIGTERM signals. In these cases, theSIGKILL signal (signal number 9) can be used as a last resort. The SIGKILL signal kills a process at thekernel level, with out any interaction with the process itself.

In the following, maxwell attempts to terminate a physics simulation (the sim process) with the defaultSIGTERM signal. For some reason, the process is unresponsive, so he next kills it with the SIGKILLsignal.

[maxwell@station maxwell]$ pgrep -lu maxwell2112 bash4489 sim[maxwell@station maxwell]$ pkill sim[maxwell@station maxwell]$ pgrep -lu maxwell2112 bash4489 sim[maxwell@station maxwell]$ pkill -KILL sim[1]+ Killed sim[maxwell@station maxwell]$ pgrep -lu maxwell2112 bash

Make it Stop!The combination of the pkill's ability to select all processes owned by a particular user, and the SIGKILLsignal's low level lethality, makes a convenient technique for a user to kill all processes they might berunning on the local machine.

In the following, maxwell first notes how many processes he's running on the local machine. He then slaysall processes.

[maxwell@station maxwell]$ pgrep -u maxwell | wc -l 22[maxwell@station maxwell]$ pkill -KILL -u maxwell

As a result, maxwell kicks himself off the machine (by slaying his own X session or virtual console loginsession), and must login again.

Online ExercisesLab Exercise

Objective: Effectively terminate running processes.

Estimated Time: 10 mins.

Specification1. Create a short shell script called ~/bin/kill_all_cats, and make it executable. When executed,

the script should kill all currently running cat processes.

2. In a terminal, start a cat process using the following command line. Leave the process running whilegrading your exercise (but don't be surprised if its not running when you're done).

[student@station student]$ cat /dev/zero > /dev/null

Page 47: Part Workbook 9. Managing Processes

Sending Signals

47

Deliverables1.

1. A script shell script called ~/bin/kill_all_cats, which when executed, delivers aSIGTERM signal to all currently running instances of the cat command.

2. An executing cat process.

QuestionsUse the following transcript to help answer the next few questions.

[maxwell@station maxwell]$ ps -U maxwell PID TTY TIME CMD 4785 ? 00:00:00 gnome-session 4828 ? 00:00:00 ssh-agent... 4846 ? 00:00:00 xscreensaver 5410 pts/8 00:00:00 bash 5451 ? 00:00:00 same-gnome 5452 ? 00:00:00 same-gnome 5454 ? 00:00:01 gimp 5455 ? 00:00:00 script-fu 5463 pts/8 00:00:00 ps 5907 pts/7 00:00:00 bash 5942 pts/7 00:00:00 find

And in another terminal, the user maxwell is running the following command.

[maxwell@station maxwell]$ find / > /dev/null 2>/dev/null

1. Which of the following command lines would deliver a SIGTERM to the xscreensaver process?

a. kill TERM xscreensaver

b. kill 4846

c. kill xscreensaver

d. kill -9 4846

e. None of the above.

2. Which of the following would deliver a SIGKILL to the xscreensaver command?

a. kill -9 4846

b. kill xscreensaver

c. kill -KILL xscreensaver

d. kill -15 4846

e. None of the above

3. Which of the following commands can be used to deliver a SIGTERM to a process?

a. kill

b. pkill

Page 48: Part Workbook 9. Managing Processes

Sending Signals

48

c. gnome-system-monitor

d. top

e. All of the above

4. Which of the following would send a SIGINT (signal number 2) to the find command?

a. Using the CTRL+C control sequence on the terminal pts/7.

b. kill -INT 5942

c. pkill -2 "^f.*d$"

d. All of the above

e. A and B only

5. Which of the following command lines would send both instances of the same-gnome applicationa SIGTERM signal?

a. pkill same-gnome

b. pkill -c same-gnome

c. pkill -u same-gnome

d. pkill -9 same-gnome

e. None of the above

6. Which of the following would send a SIGCHLD (signal number 17) to the ssh-agent process?

a. kill -CHLD ssh-agent

b. kill -17 ssh-agent

c. kill -CHLD 4828

d. All of the above

e. A and C only

7. Which key pressed within the top command allows the user to send a signal to a process?

a. s

b. z

c. t

d. k

e. None of the above

8. Which of the following is not one of the possible built in kernel responses to receiving a signal?

a. The process terminates.

Page 49: Part Workbook 9. Managing Processes

Sending Signals

49

b. The process is restarted.

c. The process terminates, and a memory image of the process is dumped to the filesystem.

d. The process ignores the signal.

e. The process is stopped (suspended).

9. Which of the following command lines would effectively kill all of the processes listed above?

a. kill -9 *

b. kill -u maxwell

c. pkill -CHLD -u maxwell

d. pkill -KILL -u maxwell

e. None of the above

The user maxwell is using the interactive python interpreter.

[maxwell@station maxwell]$ pythonPython 2.4.3 (#1, Dec 11 2006, 11:38:52) [GCC 4.1.1 20061130 (Red Hat 4.1.1-43)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>>

Upon using the CTRL+C key sequence, the following response is generated.

KeyboardInterrupt>>>

10. What best describes how the python process responds to the SIGINT signal?

a. The program ignores the SIGINT signal.

b. The program has implemented a custom signal handler for the SIGINT signal.

c. The program implements the kernel default signal handler for the SIGINT signal, which is toterminate the process.

d. The program implements the kernel default signal handler for the SIGINT signal, which is tostop (suspend) the process.

e. None of the above

Page 50: Part Workbook 9. Managing Processes

50

Chapter 5. Job ControlKey Concepts

• The bash shell allows commands to be run in the background as "jobs".

• The bash shell allows one job to run in the foreground, and can have multiple backgrounded jobs.

• The jobs command will list all backgrounded jobs.

• The CTRL-Z key sequence will suspend and background the current foreground job.

• The bg command resumes a backgrounded job.

• The fg command brings a backgrounded job to the foreground.

DiscussionThe topics addressed by this Workbook so far, namely listing process information, changing a process'sniceness, and sending signals to processes, are features shared by all processes, whether they are startedfrom a shell's command line or otherwise. Unlike these previous topics, our remaining topic, job control,concerns itself with managing processes which are started from an interactive shell prompt, and we willfocus on the bash shell in particular.

Running Commands in the ForegroundWhen running a command from the bash shell prompt, unless you specify otherwise, the command runsin the foreground. The bash shell waits for the foreground command to terminate before issuing anotherprompt, and anything typed at the keyboard is generally read as stdin to this command. All of this shouldsound familiar, as almost every command used thus far has been run in the foreground.

Running Commands in the Background as JobsIn contrast, any command you specify can also be run in the background by appending the ampersandcharacter (“&”) to the command line. Generally, only long running commands that do not require inputfrom the keyboard, and do not generate large amounts of output, are appropriate for backgrounding. Whenthe bash shell backgrounds a command, the command is referred to as a job, and assigned a job number.

In the following example, einstein is performing a search of his entire filesystem for files which are largerthan 1 megabyte in size. Because he expects this command to run a while, he redirects stdout to a file,throws stderr away, and runs it as a background job.

[einstein@station einstein]$ find / -size +1024k > bigfiles.txt 2> /dev/null &[1] 7022

After starting the job in the background, the bash shell reports two pieces of information back to einstein.The first is the job number, reported in square brackets. The second is the process ID of the backgroundedjob. In this case, the job is job number 1, and the process ID of the find command is 7022.

While this command is running in the background, einstein decides he would also like to find all filesowned by him which he has not modified in two week. He composes the appropriate find command, andagain backgrounds the job.

[einstein@station einstein]$ find / -user einstein -and -mtime +14 > oldfiles.txt 2> /dev/null & [2] 7023

Page 51: Part Workbook 9. Managing Processes

Job Control

51

[1] Exit 1 find / -size +1M >bigfiles.txt 2>/dev/null

Again, bash reports the job number (2) and the process ID of the second find command (7023).

The second message from the bash shell is notifying einstein that job number one has finished. The bashshell reports that it has exited with a return code of 1 (as opposed to being killed by a signal), and redisplaysthe command line to remind einstein of what he had run. The bash shell does not report immediately whenjobs die, but waits until the next time it interprets a command line.

By the time einstein has digested all of this, he suspects his second job has finished as well. He simplyhits the RETURN key (so that bash will "interpret" the empty command line). The bash shell similarlyreports his now finished job number 2.

[einstein@station einstein]$[2]+ Exit 1 find / -user einstein -and -mtime +14 >oldfiles.txt 2>/dev/null

Managing Multiple JobsThe user einstein, like the user maxwell, is often performing physics calculations that take a long time toexecute. He starts several different versions of the simulation, backgrounding each.

[einstein@station einstein]$ lsbin sim_a sim_b sim_c sim_d[einstein@station einstein]$ ./sim_a &[1] 7309[einstein@station einstein]$ ./sim_b &[2] 7311[einstein@station einstein]$ ./sim_c &[3] 7313[einstein@station einstein]$ ./sim_d &[4] 7315

Listing Current Jobs with jobs

The user einstein can use the jobs builtin command to report all of his currently running jobs.

[einstein@station einstein]$ jobs[1] Running ./sim_a &[2] Running ./sim_b &[3]- Running ./sim_c &[4]+ Running ./sim_d &

Each of his background jobs are listed, along with the job number. The most recently handled job is referredto as the current job, and is decorated by the jobs command with a “+”.

Bringing a Job to the Foreground with fg

A backgrounded job can be brought back to the foreground with the fg builtin command. The fg commandexpects a job number as an argument, or if none is supplied, will foreground the current job.

[einstein@station einstein]$ fg 3./sim_c

The job sim_c is now running in the foreground. As a consequence, the shell will not issues another promptwhile the process is still running.

Suspending the Foreground Job with CTRLZ

We had previously introduced the CTRL+Z control sequence as a method of suspending processes. Now,by watching the output of the bash shell closely as einstein suspends the foreground command, we seethat the bash shell treats any suspended foreground process as a job.

Page 52: Part Workbook 9. Managing Processes

Job Control

52

[einstein@station einstein]$ fg 3./sim_cCTRL+Z

[3]+ Stopped ./sim_c[einstein@station einstein]$ jobs[1] Running ./sim_a &[2] Running ./sim_b &[3]+ Stopped ./sim_c[4]- Running ./sim_d &[einstein@station einstein]$ ps uUSER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMANDeinstein 6987 0.0 0.3 4316 976 pts/8 S 15:56 0:00 -basheinstein 7309 0.0 0.3 4112 1004 pts/8 S 16:20 0:00 /bin/bash ./sim_aeinstein 7311 0.0 0.3 4112 1004 pts/8 S 16:20 0:00 /bin/bash ./sim_beinstein 7313 0.0 0.3 4112 1004 pts/8 T 16:20 0:00 /bin/bash ./sim_ceinstein 7315 0.0 0.3 4112 1000 pts/8 S 16:20 0:00 /bin/bash ./sim_deinstein 7446 0.0 0.2 2616 660 pts/8 R 16:34 0:00 ps u

When suspended (or, to use the shell's terminology, stopped), the process is assigned a job number (if itdid not already have one) and backgrounded. The jobs command reports the job as a "Stopped" job, andthe ps command confirms that the process is in the stopped state.

Restarting a Stopped Job in the Background

A stopped job can be restarted in the background with the bg builtin command. Like the fg command, thebg command expects a job number as an argument, or, if none is provided, uses the current job.

In the following, einstein restarts his stopped job in the background.

[einstein@station einstein]$ bg 3[3]+ ./sim_c &[einstein@station einstein]$ jobs[1] Running ./sim_a &[2] Running ./sim_b &[3]- Running ./sim_c &[4]+ Running ./sim_d &

Now job number 3 is again in the running state.

Killing JobsThe kill command, which is used to deliver signals to processes, is implemented as a shell builtin command.(Confusingly, another version is also found in the filesystem, /bin/kill. You are probably using theshell builtin version instead). As a result, it is aware of any jobs that the shell is managing.

When specifying which process should receive a signal, the process's job number (if it has one) can bespecified in lieu of its process ID. To distinguish the two, job numbers are preceded by a percent character(“%”), as in the following example.

[einstein@station einstein]$ jobs[1] Running ./sim_a &[2] Running ./sim_b &[3]- Running ./sim_c &[4]+ Running ./sim_d &

[einstein@station einstein]$ kill 2 -bash: kill: (2) - Operation not permitted

[einstein@station einstein]$ kill %2 [einstein@station einstein]$[2] Terminated ./sim_b[einstein@station einstein]$ jobs

Page 53: Part Workbook 9. Managing Processes

Job Control

53

[1] Running ./sim_a &[3]- Running ./sim_c &[4]+ Running ./sim_d &

Here, einstein mistakenly used the syntax for specifying a process ID, instead of a job number.Because he does not own the process with process ID number 2, the command failed.Here, einstein used the correct syntax for specifying a job number, and the signal was delivered tothe sim_b process.

SummaryThe following table summarizes commands and techniques for managing jobs within the bash shell.

Table 5.1. Job Management in the bash Shell

Command Action

jobs List all jobs

fg [N] Bring background job N to the foreground (by default, the "current"background job).

CTRL+Z Suspend and background the current foreground command

bg [N] Start stopped background job N (by default, the "current" background job).

kill %N Terminate background job N (by sending the SIGTERM signal).

Examples

Deciding to Background a Command Running in theForeground

The discussion mentioned that commands can be started in the background, by appending an ampersand(“&”) to the command line. In the following, einstein starts a command in the foreground, and then,realizing that it will take a while to complete, wishes he had started it in the background.

[einstein@station einstein]$ ./sim_a

In order to background the command, he first suspends it with a CTRL+Z control sequence, which leavesit as a stopped background job. He then restarts the job in the background using bg.

[einstein@station einstein]$ ./sim_aCTRL+Z

[1]+ Stopped ./sim_a[einstein@station einstein]$ bg[1]+ ./sim_a &[einstein@station einstein]$

Using CTRLC to Kill a Background JobAs an alternative to the kill command, the following technique is used to cancel backgrounded jobs. First,the job is foregrounded with the fg command, and then killed with the CTRL+C sequence.

[einstein@station einstein]$ jobs[1] Running ./sim_a &[2]- Running ./sim_b &[3]+ Running ./sim_c &

Page 54: Part Workbook 9. Managing Processes

Job Control

54

[einstein@station einstein]$ fg 2./sim_bCTRL+C[einstein@station einstein]$ jobs[1]- Running ./sim_a &[3]+ Running ./sim_c &

Online ExercisesLab Exercise

Objective: Use bash job control to manage multiple tasks.

Estimated Time: 10 mins.

Specification1. Start the following four commands, placing each in the background.

ls -R / | grep "*.conf" > lsconf 2>/dev/nullcat /dev/zero > /dev/nullfind / -name "[Aa]*[Cc]*[Ff]*"sleep 100000

2. Using job control commands and common control sequences, stop (suspend) the ls and find jobs.

Deliverables1.

1. Four background jobs managed by the bash shell. The cat and sleep jobs should jobs should berunning, while the find and ls jobs should be suspended.

Clean UpAfter you have graded your exercise, use the kill command (or the fg/ CTRL+C combination) to kill allfour jobs.

Questions1. What control sequence can be used to terminate the foreground command?

a. CTRL+Z

b. CTRL+D

c. CTRL+G

d. CTRL+C

e. None of the above

2. What control sequence can be used to terminate the current background job?

a. CTRL+G

b. CTRL+D

Page 55: Part Workbook 9. Managing Processes

Job Control

55

c. CTRL+Z

d. CTRL+C

e. None of the above

3. What control sequence can be used to suspend the foreground command?

a. CTRL+Z

b. CTRL+G

c. CTRL+D

d. CTRL+C

e. None of the above

Use the following transcript to answer the next 4 questions.

[student@station student]$ jobs[1] Running sleep 2000 &[2]+ Stopped vim[3]- Running ls --color=tty -R / >/dev/null 2>/dev/null &[4] Stopped find / -name "*.conf" 2>/dev/null

[student@station student]$ ps -U student PID TTY TIME CMD 1312 pts/0 00:00:00 bash 1349 pts/0 00:00:00 sleep 1350 pts/0 00:00:00 vim 1351 pts/0 00:00:00 ls 1355 tty4 00:00:00 bash 1395 tty4 00:00:00 find 1397 pts/0 00:00:00 ps

4. If using the terminal pts/0, which of the following will terminate the sleep process?

a. CTRL+C

b. kill %1

c. kill 1349

d. CTRL+Z

e. B or C

5. Which of the following would restore the vim editor to the foreground?

a. fg

b. fg 2

c. bg 2

d. bg

e. A or B

6. If using the terminal pts/0, which of the following would terminate the ls process?

Page 56: Part Workbook 9. Managing Processes

Job Control

56

a. CTRL+C

b. kill %3

c. kill 3

d. CTRL+Z

e. B or C

7. Which of the following would restart the find command, but keep it in the background?

a. bg 4

b. fg 4 followed by CTRL+Z

c. bg

d. kill -HUP %4

e. None of the above

8. If you are becoming impatient for the command you started in the foreground to complete, whataction(s) could you take to background the job?

a. CTRL+C, followed by the bg command.

b. CTRL+Z, followed by the fg command.

c. CTRL+Z, followed by the bg command.

d. CTRL+C, followed by the fg command.

e. None of the above

9. Which of the following could be used to kill the most recently backgrounded job?

a. The bg command, followed by CTRL+C.

b. The fg command, followed by CTRL+C.

c. kill %%

d. kill

e. None of the above

10. You notice that every time your friend starts the firefox web browser from the terminal, thecommand runs in the foreground, and your friend cannot use the terminal again until quitting thebrowser. What advice can you give him?

a. Only start graphical applications using menus, not terminals.

b. When starting graphical applications from a terminal, background the job by appending anampersand (“&”) to the command line.

c. When starting graphical applications from a terminal, background the job by appending twoexclamation points (“!!”) to the command line.

Page 57: Part Workbook 9. Managing Processes

Job Control

57

d. Just get used to it. She can always open up another terminal.

e. The question is not valid. Graphical applications can only be started from menus.

Page 58: Part Workbook 9. Managing Processes

58

Chapter 6. Scheduling Delayed Tasks:at

Key Concepts

• The at command can submit commands to run at a later time.

• The batch command can submit commands to run when the machines load is low.

• Commands can either be entered directly, or submitted as a script.

• stdout from at jobs is mailed to the user.

• atq and atrm are used to examine and remove currently scheduled jobs.

Discussion

DaemonsBefore discussing the at command directly, we begin with a short discussion of a common Unix concept:daemons.

With a name inspired by the physicist Maxwell's Daemon [http://ei.cs.vt.edu/~history/Daemon.html],Unix daemons are processes that run in the background, detached from any terminal, performing tasksthat are usually not related to a user at the keyboard. Daemons are often associated with network services,such as the web server (httpd) or the FTP server (vsftpd). Other daemons handle system tasks, such as thelogging daemon (syslogd) and the power management daemon (apmd). This Lesson, and the followingLesson, discuss two daemons that allow users to delay tasks (atd), or run commands at fixed intervals(crond). By now, you have probably noticed a naming convention as well: programs meant to be run asdaemons usually end in the letter d.

Daemons are processes like any other process. They are usually started as part of the system's boot upsequence, or by the administrative user root, so unless you look for them, you might never know that theyare there.

[elvis@station elvis]$ ps aux | grep crondroot 890 0.0 0.0 1572 132 ? S 09:57 0:00 crondelvis 5035 0.0 0.2 3572 640 pts/4 S 16:17 0:00 grep crond[elvis@station elvis]$ ps aux | grep atddaemon 4730 0.0 0.2 1420 532 ? S 15:42 0:00 /usr/sbin/atdelvis 5037 0.0 0.2 3572 640 pts/4 S 16:17 0:00 grep atd

Some daemons run as the user root, while others take on the identity of another system user for securityconcerns. Above, the crond daemon is running as root, but the atd daemon is running as the user daemon.

The atd DaemonThe atd daemon allows users to submit jobs to be performed at a later time, such as "at 2:00am". In orderto use the atd daemon, it must be running. Users can confirm that atd is running simply by examininga list of running processes:

[madonna@station madonna]$ ps aux | grep atddaemon 4730 0.0 0.2 1420 532 ? S 15:42 0:00 /usr/sbin/atdmadonna 5570 0.0 0.2 3572 640 pts/2 S 16:43 0:00 grep atd

Page 59: Part Workbook 9. Managing Processes

Scheduling Delayed Tasks: at

59

Notice that the seventh column specifies what terminal a process is associated with. For blondie's grepcommand, the terminal is pts/2, which probably refers to a network shell or a graphical terminal withinan X session. Notice that the atd daemon has no associated terminal. One of the defining characteristicsof a daemon is that it drops it's association with the terminal that started it.

Submitting Jobs with atThe at command is used to submit jobs to the atd daemon to be run at a specific time. The commandsto be run are either submitted as a script (with the -f command line switch), or entered directly via stdin.Standard out from the command is mailed to the user.

at [[-f filename] | [-m]] TIME

Switch Effect

-f filename run the script specified by filename

-m Notify the user by email when done, even if there is no output.

The time of day can be specified using HH:MM, suffixed by "am" or "pm". The terms "midnight", "noon",and "teatime" can also be used. (You read correctly, "teatime".) A date can also be specified using severalformats, including MM/DD/YY. The at(1) man page provides many more details.

The wrestler hogan would like to print a file containing all of the fan mail that he has received,fanmail.txt. He's a little concerned, though, because he shares the printer with ventura, who uses theprinter a lot as well. Wanting to avoid a fight, hogan decides to delay his printing until 2:00 in the morning.

[hogan@station hogan]$ at 2:00 amwarning: commands will be executed using (in order) a) $SHELL b) login shell c)/bin/shat> lpr fanmail.txtat> CTRL+Djob 7 at 2003-06-17 02:00

Because hogan did not use the -f command line switch, the at command prompted hogan to type in hiscommands using stdin (the keyboard). Fortunately, hogan knows that CTRL+D, when entered directlyfrom a terminal, indicates an "end of file". Alternately, he could have piped the command into stdindirectly:

[hogan@station hogan]$ echo "lpr fanmail" | at 2:00 amwarning: commands will be executed using (in order) a) $SHELL b) login shell c)/bin/shjob 7 at 2003-06-17 02:00

Next, hogan confirms that his job has been registered using atq.

[hogan@station hogan]$ atq7 2003-06-17 02:00 a hogan

Lastly, hogan remembers that ventura is on vacation, so he can print his fan mail without incident. Hedecides to cancel his at job, and print the file directly.

[hogan@station hogan]$ atrm 7[hogan@station hogan]$ atq[hogan@station hogan]$ lpr fanmail.txt

Delaying Tasks with batchThe batch command, like the at command, is used to defer tasks until a later time. Unlike the at command,batch does not run the command at a specific time, but instead waits until the system is not busy with

Page 60: Part Workbook 9. Managing Processes

Scheduling Delayed Tasks: at

60

other tasks, whenever that time might be. If the machine is not busy when the job is submitted, the jobmight run immediately. The atd daemon monitors the system's loadavg, and waits for it to drop beneath0.8 before running the job.

The batch command has a syntax identical to the at command, where jobs can either be specified usingstdin, or submitted as a batch file with the -f command line switch. If a time is specified, batch will delayobserving the machine until the specified time. At that time, batch will begin monitoring the system'sloadavg, and run the job when the system is not otherwise busy.

Summary of at CommandsThe following table summarizes the command used when registering jobs with the atd daemon.

Table 6.1. Commands related to the at service

command use

atd The daemon that runs submitted jobs. Users do not use the atd command directly.

at Submits jobs to the atd daemon, to be run at a specific time.

batch Submits jobs to the atd daemon, to be run when the system is not otherwise busy.

atq List jobs queued with the atd daemon.

atrm Cancel a job queued with the atd daemon, before it runs.

Examples

Submitting an at Job as a FileThe user hogan has now developed a habit of printing his fanmail at 2:00 in the morning. He has alsodiscovered the enscript command, which reformats text files, printing them "2up" (two text pages perprinted page), and decorates the printed page with informative headers and footers. After a little experience,he develops the following command line to format the page to his liking: enscript -r2 -G --header="FanMail" --borders fanmail.txt.

Because hogan does not want to type this command and all of its switches every time, he creates a shortscript that contains the command(s) he would like run, in a file called fanmail.at:

[hogan@station hogan]$ cat fanmail.atenscript -r2 -G --header="Fan Mail" --borders fanmail.txt

Now, on days when hogan wants to submit his fanmail to be printed, he can simply specify the script toat with the -f command line switch.

[hogan@station hogan]$ at -f fanmail.at 2:00 amwarning: commands will be executed using (in order) a) $SHELL b) login shell c)/bin/shjob 11 at 2003-06-18 02:00[hogan@station hogan]$ atq11 2003-06-18 02:00 a hogan

Examining the at Spool SyntaxThe user ventura has observed hogan's habit of printing his fanmail at 2:00 am, and as a practical jokedecides to submit a job of his own. He composes the file bogus_fanmail.txt, which fakes not-so-

Page 61: Part Workbook 9. Managing Processes

Scheduling Delayed Tasks: at

61

flattering mail received from not-so-adoring fans. He submits a job which will print the file at 1:59 am,hoping that hogan will not notice the insertion as he picks up the papers at the printer in the morning.

[ventura@station ventura]$ echo "lpr bogus_fanmail.txt" | at 1:59 amwarning: commands will be executed using (in order) a) $SHELL b) login shell c)/bin/shjob 12 at 2003-06-18 01:59[ventura@station ventura]$ atq12 2003-06-18 01:59 a ventura

At some point later, the machine's system administrator, acting as root, observers the two at jobs queued.

[root@station at]# atq11 2003-06-18 02:00 a hogan12 2003-06-18 01:59 a ventura

Curious about what ventura is up to, root goes snooping into at's spooling directory, /var/spool/at.He examines the contents of at's cryptically named spooling file, a0000c010c8887, which is ownedby the user ventura.

[root@station at]# ls -ltotal 12-rwx------ 1 hogan hogan 1480 Jun 17 12:37 a0000b010c8888-rwx------ 1 ventura ventura 1459 Jun 17 13:08 a0000c010c8887drwx------ 2 daemon daemon 4096 Jun 16 17:24 spool[root@station at]# cat /var/spool/at/a0000c010c8887

#!/bin/sh # atrun uid=511 gid=511# mail ventura 0

umask 2

HOSTNAME=bowe-lt.rdu.redhat.com; export HOSTNAME HISTSIZE=1000; export HISTSIZEUSER=ventura; export USERLOGNAME=ventura; export LOGNAME...LESSOPEN=\|/usr/bin/lesspipe.sh\ %s; export LESSOPENG_BROKEN_FILENAMES=1; export G_BROKEN_FILENAMESXAUTHORITY=/home/ventura/.xauthqEj97Q; export XAUTHORITY

cd /home/ventura || { echo 'Execution directory inaccessible' >&2 exit 1}

lpr bogus_fanmail.txt

(In this long listing, several lines have been deleted and replaced with "...".)

root notices the following aspects of the script's behavior:

The file is a shell script, and because of it's permissions, it could be executed directly.The first action of the script is to set the process's umask to the umask of the shell which submittedthe job.The second action is to initialize a collection of environment variables to mimic the environment ofthe shell which submitted the job.The third action is change the process's current working directory to the current working directoryof the shell which submitted the job.Finally, after all of this initialization, the script is ready to run the submitted job, in this case, lprbogus_fanmail.txt.

By storing all of this information with the submitted job, the atd daemon is able to reconstruct ventura'senvironment when the job was submitted. If the submitted job created new files, the files would have theexpected permissions, because the script created the appropriate umask. If any of the commands startedby the script relied on environment variables, the environment would be set as expected, and so on.

Page 62: Part Workbook 9. Managing Processes

Scheduling Delayed Tasks: at

62

Online Exercises

Submitting a job for Delayed Execution

Exercise

Objective: Use the atd service to delay a task for later execution

Estimated Time: 10 mins.

Specification

You have had a hard time remembering what day it is, so you would like to mail yourself a copy of thecurrent calendar, so that you see it the first thing in the morning. Submit an at job, that simply runs the calcommand, for 3:45 in the morning. Make sure that it is your only job scheduled with the at facility.

Deliverables

1.1. A queued at job, which will generate the output of the cal command at 3:45 in the morning.

Questions1. Which of the following commands can be used to delay the execution of a command until a specific

time?

a. at

b. later

c. batch

d. A and C

2. What is the name of the daemon that manages tasks submitted for later execution?

a. batchd

b. laterd

c. atd

d. crond

3. Why might a user be tempted to submit a job using the batch command, instead of the at command?

a. The user wants to ensure that the job executes at exactly the specified time.

b. There is no at command.

c. The user wants to minimize the job's impact on other users on the system.

d. The job consists of several steps, which should be executed as a single unit.

4. In order to delete a job submitted to the at service, what information must a user need to know?

Page 63: Part Workbook 9. Managing Processes

Scheduling Delayed Tasks: at

63

a. The time when the job was scheduled to run.

b. The job id of the submitted job.

c. The terminal the job was submitted from.

d. Once submitted, a job may not be deleted.

5. How may a user specify what commands to run when submitting an at job?

a. By entering the commands directly from the keyboard.

b. By redirecting the commands to standard in of the at command.

c. By specifying a script that contains the commands on the command line.

d. All of the above.

6. Why will the following command fail? echo cal | at -f 2:00

a. The time specification needs to include either AM or PM.

b. The token cal needs to be in double quotes.

c. The -f command line switch requires a filename as an argument.

d. There is no at command.

7. When executing a spooled job, why does the script first change directories to the directory fromwhere the job was submitted?

a. To ensure that the $OLDDIR environment variable is appropriately set.

b. For security, the user must own the directory from where the job was submitted.

c. To make sure that one user's at job does not influence another user's at job.

d. So that if the job contains relative file references, they will resolve appropriately.

[root@station root]# ls -al /var/spool/at/total 28drwx------ 3 daemon daemon 4096 Jun 17 16:07 .drwxr-xr-x 15 root root 4096 Jan 24 18:52 ..-rwx------ 1 hogan hogan 1480 Jun 17 12:37 a0000b010c8888-rwx------ 1 ventura ventura 1459 Jun 17 13:08 a0000c010c8887-rw------- 1 daemon daemon 6 Jun 17 16:07 .SEQdrwx------ 2 daemon daemon 4096 Jun 16 17:24 spool

[ventura@station ventura]$ cat /var/spool/at/a0000c010c8887cat: /var/spool/at/a0000c010c8887: Permission denied

8. In the above listing, ventura owns the file a0000c010c8887. Why can't ventura examine thefile's contents?

a. The user ventura does not have read permissions.

b. ventura is also a member of the group ventura, and the group ventura has no read permissions.

c. The user ventura does not have permissions to access the /var/spool/at directory.

d. The user ventura does not have permissions to execute the cat command.

Page 64: Part Workbook 9. Managing Processes

Scheduling Delayed Tasks: at

64

[hogan@station hogan]$ at 2:00warning: commands will be executed using (in order) a) $SHELL b) login shell c)/bin/shat> lpr big_report.psat> <EOT>job 15 at 2003-06-18 02:00

9. In the above listing, what is implied by the reference to $SHELL?

a. If the user were to set SHELL=/bin/csh at the first at> prompt, the rest of the job would berun using /bin/csh as the interpreter.

b. If the user were to set SHELL=/bin/csh as an environment variable before running the atcommand, the job would be run using /bin/csh as the interpreter.

c. The job will be run three times, the first time using the contents of the SHELL environmentvariable as the interpreter.

d. The job will only be run if the user has the SHELL environment variable is set to her loginshell.

10. As far as the at command is concerned, what time is teatime?

a. 2:30 pm

b. 3:00 pm

c. 3:30 pm

d. 4:00 pm

Page 65: Part Workbook 9. Managing Processes

65

Chapter 7. Scheduling Periodic Tasks:cron

Key Concepts

• The cron facility is used to schedule regularly recurring tasks.

• The crontab command provides a front end to editing crontab files.

• The crontab file uses 5 fields to specify timing information.

• stdout from cron jobs is mailed to the user.

Discussion

Performing Periodic TasksOften, people find that they are (or aught to be) performing tasks on a regular basis. In systemadministration, such tasks might include removing old, unused files from the /tmp directory, or checkingto make sure a file that's collecting log messages hasn't grown to large. Other users might find they'reown tasks, such as checking for large files that they aren't using anymore, or checking a website to seeif anything new has been posted.

The cron service allows users to configure commands to be run on a regular basis, such as every 10 minutes,once every thursday, or twice a month. Users specify what commands should be run at what times by usingthe crontab command to configure their "cron table". The tasks are managed by a traditional Linux (andUnix) daemon, the crond daemon.

The cron ServiceThe crond daemon is the daemon that performs periodic tasks on behalf of the system or individual users.Usually, the daemon is started as the system boots, so most users can take it for granted. By listing allprocesses and searching for crond, you can confirm that the crond daemon is running.

[hogan@station hogan]$ ps aux | grep crondroot 891 0.0 0.0 1572 128 ? S Jun17 0:00 crondhogan 8118 0.0 0.2 3576 644 pts/2 S 10:15 0:00 grep crond

If the crond daemon is not running, your system administrator would need to start the crond service as root.

crontab SyntaxUsers specify which jobs to run, and when to run them, by configuring a file known as the "cron table",more often abbreviated "crontab". An example crontab file is listed below.

# set the default language to be englishLOCALE=en_US

05 * * * * procinfo15 04 * * * find $HOME -size +100k25 04 1 * * echo "Pay your bills"35 04 14 3 * echo "Beware the Ides of March" | mail -s "a warning" julius

Page 66: Part Workbook 9. Managing Processes

Scheduling Periodic Tasks: cron

66

45 04 * * 1 find $HOME -atime +30 | lpr

A crontab file is a line based configuration file, with each line performing one of three functions:

Comments All lines who first (non-space) character is a # areconsidered comments, and are ignored.

Environment variables All lines that have the form name = value are used todefine environment variables.

Cron commands Any other (non blank) line is considered a croncommand, which is made up of six fields describedbelow.

Cron command lines consist of six whitespace separated fields. The first 5 fields are used to specify whento run the command, and the remaining sixth field (composed of everything after the fifth field) specifiesthe command to run. The first five fields specify the following information:

,----------------> minute | ,-------------> hour | | ,----------> day of month | | | ,-------> month (1=January, 2=February, ...) | | | | ,----> day of week (0=Sunday, 1=Monday, ...) | | | | | ,-> command to run | | | | | | 25 04 1 * * echo "Pay your bills"

Each of the first five fields must be filled with a token using the following syntax:

token meaning example interpretation (if used in the first field)

* every time * every minute

n at the specified time 10 every hour at 10 minutes past the hour

n, n, ... at any of the specified times 22,52 every hour at 22 and 52 minutes past thehour

*/n every nth time */15 every 15 minutes (at 0, 15, 30 and 45minutes past the hour)

Using the crontab CommandUsers seldom manage their crontab file directly (or even know where it is stored), but instead use thecrontab command to edit, list, or remove it.

crontab {[-e] | [-l] | [-r]}

crontab FILE

Edit, list, or remove the current crontab file, or replace the current crontab file with FILE.

Switch Effect

-e edit current file

-l list current file

-r remove current file

Page 67: Part Workbook 9. Managing Processes

Scheduling Periodic Tasks: cron

67

In the following sequence of commands, hogan will use the crontab command to manage his crontabconfiguration. He first lists his current crontab configuration to the screen, then he lists the current fileagain, storing the output into the file mycopy.

[hogan@station hogan]$ crontab -l# DO NOT EDIT THIS FILE - edit the master and reinstall.# (/tmp/crontab.9212 installed on Wed Jun 18 11:46:36 2003)# (Cron version -- $Id: 040_crontab.dbk,v 1.1 2005/03/21 05:24:29 brads Exp $)# set the default language to be english.LOCALE=en_US 05 * * * * procinfo15 04 * * * find $HOME -size +100k25 04 1 * * echo "Pay your bills"35 04 14 3 * echo "Beware the Ides of March" | mail -s "a warning" julius45 04 * * 1 find $HOME -atime +30 | lpr[hogan@station hogan]$ crontab -l > mycopy

Next, hogan removes his current crontab configuration. When he next tries to list the configuration, he isinformed that no current configuration exists.

[hogan@station hogan]$ crontab -r[hogan@station hogan]$ crontab -lno crontab for hogan

In order to restore his cron configuration, hogan uses the crontab command once again, this timespecifying the mycopy file as an argument. Upon listing his configuration again, he finds that his currentconfiguration was read from the mycopy file.

A little annoyingly, the banner has been duplicated in the process. Can you out why?

The original banner was stored in mycopy. When mycopy was resubmitted, cron treated the originalbanner as a user comment, and prepended a new banner.

[hogan@station hogan]$ crontab mycopy[hogan@station hogan]$ crontab -l# DO NOT EDIT THIS FILE - edit the master and reinstall.# (mycopy installed on Wed Jun 18 11:47:00 2003)# (Cron version -- $Id: 040_crontab.dbk,v 1.1 2005/03/21 05:24:29 brads Exp $)# DO NOT EDIT THIS FILE - edit the master and reinstall.# (/tmp/crontab.9212 installed on Wed Jun 18 11:46:36 2003)# (Cron version -- $Id: 040_crontab.dbk,v 1.1 2005/03/21 05:24:29 brads Exp $)# set the default language to be english.LOCALE=en_US 05 * * * * procinfo15 04 * * * find $HOME -size +100k25 04 1 * * echo "Pay your bills"35 04 14 3 * echo "Beware the Ides of March" | mail -s "a warning" julius45 04 * * 1 find $HOME -atime +30 | lpr[hogan@station hogan]$ rm mycopy

Editing crontab Files in PlaceOften, users edit their crontab files in place, using crontab -e. The crontab command will open the currentcrontab configuration into the user's default editor. When the user has finished editing the file, and exitsthe editor, the modified contents of the file are installed as the new crontab configuration.

The default default editor is /bin/vi, however crontab, like many other commands, examines the EDITORenvironment variable. If the variable has been set, it will be used to specify which editor to open. Forexample, if hogan prefers to use the nano editor, he can first set up the EDITOR environment variable to/usr/bin/nano (or simply nano), and then run crontab -e.

Page 68: Part Workbook 9. Managing Processes

Scheduling Periodic Tasks: cron

68

If hogan wanted to use nano as his editor, he could use one of the following approaches:

[hogan@station hogan]$ export EDITOR=nano[hogan@station hogan]$ crontab -e

or

[hogan@station hogan]$ EDITOR=nano crontab -e

or, even better, hogan could add the line "export EDITOR=nano" to his .bash_profile file, and theenvironment variable would be set automatically every time he logged in.

In summary, there are two ways someone could go about creating or modifying their crontab configuration.

1. Create a text file containing their desired configuration, and then install it with crontab FILENAME.

2. Edit their configuration in place with crontab -e.

Where does the output go?How does the user receive output from commands run by cron? The crond daemon will mail stdout andstderr from any commands run to the local user. Suppose ventura had set up the following cron job:

05 * * * * cal

Once an hour, at five minutes past the hour, he could expect to receive new mail that looks like thefollowing:

Date: Wed, 18 Jun 2003 14:24:00 -0400From: [email protected] (Cron Daemon)To: [email protected]: Cron <ventura@station> calX-Cron-Env: <SHELL=/bin/sh>X-Cron-Env: <HOME=/home/ventura>X-Cron-Env: <PATH=/usr/bin:/bin>X-Cron-Env: <LOGNAME=ventura> June 2003Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 1415 16 17 18 19 20 2122 23 24 25 26 27 2829 30

The mail message contains the output of the command in the body, and all defined environment variablesin the message headers.

Optionally, ventura could have set the special MAILTO environment variable to a destination emailaddress, and mail would be sent to that address instead:

[email protected]

05 * * * * cal

Environment Variables and cronWhen configuring cron jobs, users should be aware of a subtle detail. When the crond daemon starts theuser's command, it does not run the command from a shell, but instead forks and execs the commanddirectly. This has an important implication: Any environment variables or aliases that are configured by the

Page 69: Part Workbook 9. Managing Processes

Scheduling Periodic Tasks: cron

69

shell at startup, such as any defined in /etc/profile or ~/.bash_profile, will not be availablewhen cron executes the command.

If a user wants an environment variable to be defined, they need to explicitly define the variable in theircrontab configuration.

Examples

Monitoring a Web Siteventura wants to ensure that he doesn't miss out on any new training opportunities from his favoritecompany, Red Hat Inc. In order to keep an eye on new opportunities, he configures a cron job that willdownload Red Hat's training web page, and send it to him as an email once per morning. He uses thecrontab -e command to edit his crontab configuration in place.

[ventura@station ventura]$ crontab -eno crontab for ventura - using an empty one

(... while in the text editor, ventura adds the following line...)

00 05 * * * links -dump http://www.redhat.com/training

(... and quits the text editor...)

crontab: installing new crontab

He next confirms that his cron job is in place.

[ventura@station ventura]$ crontab -l# DO NOT EDIT THIS FILE - edit the master and reinstall.# (/tmp/crontab.10296 installed on Wed Jun 18 14:17:43 2003)# (Cron version -- $Id: 010_text.dbk,v 1.1 2005/03/21 05:24:29 brads Exp $) 00 05 * * * links -dump http://www.redhat.com/training

ventura can now expect to receive email containing a text formatted version of Red Hat's training webpage, each morning at 5:00.

Monitoring Large FilesNext, ventura realizes he has a bad habit of creating very large files, and then forgetting about them. Inorder to help remind him of the large files he has laying around, he sets up a cron job which will mail hima list of all files larger than 100k every Sunday.

[ventura@station ventura]$ crontab -eno crontab for ventura - using an empty one

(... within the text editor, ventura adds the following line...)

05 05 * * 0 find $HOME -size +100k

(... and exits the text editor ...)

crontab: installing new crontab[ventura@station ventura]$ crontab -l# DO NOT EDIT THIS FILE - edit the master and reinstall.# (/tmp/crontab.10678 installed on Wed Jun 18 14:50:39 2003)# (Cron version -- $Id: 010_text.dbk,v 1.1 2005/03/21 05:24:29 brads Exp $)

Page 70: Part Workbook 9. Managing Processes

Scheduling Periodic Tasks: cron

70

00 05 * * * links -dump http://www.redhat.com/training10 05 * * 0 find $HOME -size +100k

Now, Sunday mornings at 5:10, ventura can expect to receive an email listing all files under his homedirectory greater than 100k in size.

Running scripts from cronContinuing his efforts to not waste disk space, ventura would like to regularly create a separate list of filesthat he has not accessed recently, and he would like to receive this list at the same time as he receives hislist of large files. Rather than overly complicate his cron file, he creates a script called cron.weekly,places it in the bin subdirectory of his home directory, and makes it executable.

[ventura@station ventura]$ cat cron.weekly#!/bin/bash echo "===== files larger than 100k ====="find $HOME -size +100k echoecho "==== files not used in the past month ====="find $HOME -atime +30

[ventura@station ventura]$ mkdir bin[ventura@station ventura]$ mv cron.weekly bin/[ventura@station ventura]$ chmod 755 bin/cron.weekly

Next, he modifies his crontab configuration, so that his script gets run as a cron job once per week:

[ventura@station ventura]$ crontab -e

(... within the text editor, ventura edits the following line ...)

05 05 * * 0 find $HOME -size +100k

(... so that it reads ...)

05 05 * * 0 bin/cron.weekly

(... and exits ...)

crontab: installing new crontab[ventura@station ventura]$ crontab -l# DO NOT EDIT THIS FILE - edit the master and reinstall.# (/tmp/crontab.11252 installed on Wed Jun 18 15:45:53 2003)# (Cron version -- $Id: 010_text.dbk,v 1.1 2005/03/21 05:24:29 brads Exp $)00 05 * * * links -dump http://www.redhat.com/training05 05 * * 0 bin/cron.weekly

Now, rather than maintaining his crontab configuration, ventura can just modify the scriptcron.weekly.

Printing fanmailThe user hogan has gotten to the point where he is printing the file where he collects his new fanmail,fanmail.txt, every Monday, Wednesday, and Friday. He decides to use the cron service to automatethe process.

Since this is the first time he has used cron, he doesn't need to worry about possibly replacing alreadyexisting jobs. He configures a cron file locally. Using a text editor, he creates the file crontab.hogan,in his home directory. He then installs his file with the crontab command.

Page 71: Part Workbook 9. Managing Processes

Scheduling Periodic Tasks: cron

71

[hogan@station hogan]$ cat crontab.hogan

45 13 * * 1,3,5 lpr fanmail.txt[hogan@station hogan]$ crontab crontab.hogan

He now expects his fanmail to be printed Monday, Wednesday, and Friday afternoons at 1:45. Whenthe first job prints, however, it gets sent to the wrong printer. hogan realizes that his .bash_profileconfigures his PRINTER environment variable to specify the local print queue, hp-color, automaticallywhen he logs in. Because cron doesn't use a shell to run cron jobs, however, the environment variableis not getting configured. He edits crontab.hogan to explicitly define PRINTER, and resubmits thefile with crontab.

[hogan@station hogan]$ cat crontab.hogan

PRINTER=hp-color45 13 * * 1,3,5 lpr fanmail.txt[hogan@station hogan]$ crontab crontab.hogan

Now his job prints to the correct printer.

Online Exercises

Monitoring Who is on the System.

Online Exercise

Objective: Configure a cron job

Estimated Time: 10 mins.

Specification

You are a little paranoid, and want to monitor who is using your computer in the middle of the night.Configure a cron job which will mail you the output of the who command daily at 4:35 in the morning.

Deliverables

1.1. A cron configuration which mails the output of the who command daily at 4:35 am.

Questions1. What command is used to submit cron jobs?

a. cron

b. crons

c. crond

d. crontab

2. What is the quickest rate of repetition that can be used with cron?

a. once per second

Page 72: Part Workbook 9. Managing Processes

Scheduling Periodic Tasks: cron

72

b. once per minute

c. one per hour

d. once per day

3. What command is used to view current cron configuration?

a. crond

b. crontab -l

c. crontab -r

d. crons

4. What command is used to remove any current cron configuration?

a. crond

b. crontab -l

c. crontab -r

d. crons

The user hogan has set up the following crontab configuration. Use it to answer the following questions.

[hogan@station hogan]$ crontab -l# DO NOT EDIT THIS FILE - edit the master and reinstall.# (/tmp/crontab.11698 installed on Wed Jun 18 16:12:57 2003)# (Cron version -- $Id: 010_text.dbk,v 1.1 2005/03/21 05:24:29 brads Exp $) [email protected] 05 * * * * who10 04 * * * find / -perm +400015 04 5 * * bin/cron.weekly20 04 * * 3 bin/cron.monthly | mail -s "monthly report" [email protected]

5. How often will the who command be run?

a. once every hour

b. once every day

c. once every eek

d. once every month

6. When will the find command be run?

a. Once per day, at 10:04 in the morning.

b. Once per day, at 4:10 in the morning.

c. Once per year, on October 4th.

d. Once per year, on April 10th.

Page 73: Part Workbook 9. Managing Processes

Scheduling Periodic Tasks: cron

73

7. When will the script cron.weekly be run?

a. On the 5th of each month, at 4:15 in the morning.

b. On the 15th of each month, at 5:04 in the morning.

c. Every Thursday, at 3:04 in the afternoon.

d. Every Friday, at 4:15 in the morning.

8. When will the script cron.monthly be run?

a. On the 5th of each month, at 4:15 in the morning.

b. On the 15th of each month, at 5:04 in the morning.

c. Every Wednesday, at 4:20 in the morning.

d. Every Tuesday, at 8:04 in the evening.

9. Where will stdout of the cron.weekly script be mailed?

a. [email protected]

b. hogan

c. [email protected]

d. root

10. What is the absolute path to the script cron.weekly?

a. /bin/cron.weekly

b. /home/hogan/bin/cron.weekly

c. /home/cron/bin/cron.weekly

d. /var/cron/bin/cron.weekly