rha030-workbook09-student-3.0-0

82
Workbook 9. Managing Processes Red Hat, Inc.

description

RedHat Academy

Transcript of rha030-workbook09-student-3.0-0

Page 1: rha030-workbook09-student-3.0-0

Workbook 9. Managing Processes

Red Hat, Inc.

Page 2: rha030-workbook09-student-3.0-0

Workbook 9. Managing Processesby Red Hat, Inc.Copyright © 2003-2005 Red Hat, Inc.

Revision HistoryRevision rha030-2.0-2003_11_12-en 2003-11-12First RevisionRevision rha030-3.0-0-en-2005-08-17T07:23:17-0400 2005-08-17First Revision

Red Hat, Red Hat Network, the Red Hat "Shadow Man" logo, RPM, the RPM logo, PowerTools, and all Red Hat-based trademarks and logos aretrademarks or registered trademarks of Red Hat, Inc. in the United States and other countries.Linux is a registered trademark of Linus Torvalds.Motif and UNIX are registered trademarks of The Open Group.Windows is a registered trademark of Microsoft Corporation.Intel and Pentium are a registered trademarks of Intel Corporation. Itanium and Celeron are trademarks of Intel Corporation.SSH and Secure Shell are trademarks of SSH Communications Security, Inc.All other trademarks and copyrights referred to are the property of their respective owners.

Published 2005-08-17

Page 3: rha030-workbook09-student-3.0-0

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

Discussion .......................................................................................................................................... 6Processes are How Things Get Done ....................................................................................... 6What is a Process? .................................................................................................................... 6Viewing Processes with the ps Command................................................................................ 8Monitoring Processes with the top Command ......................................................................... 9Monitoring Processes with the gnome-system-monitor Application ...................................11Locating processes with the pgrep Command. ...................................................................... 13

Examples.......................................................................................................................................... 14Example 1. Viewing All Processes with the "User Oriented" Format ...................................14Example 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 ....................................................................................................................................... 22

Discussion ........................................................................................................................................ 22A Process’s Life Cycle ........................................................................................................... 22The 5 Process States ............................................................................................................... 25Viewing Process States ........................................................................................................... 26

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

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

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

Discussion ........................................................................................................................................ 32Process Scheduling Nomenclature ......................................................................................... 32Process Scheduling, in Essence.............................................................................................. 32Changing a Process’s Niceness .............................................................................................. 34

Examples.......................................................................................................................................... 36Example 1. Viewing Priorities................................................................................................ 36Example 2. Changing Priorities with renice .......................................................................... 37

Online Exercises............................................................................................................................... 38Specification ........................................................................................................................... 38Deliverables ............................................................................................................................ 38Cleaning Up............................................................................................................................ 39

Questions.......................................................................................................................................... 39

iii

Page 4: rha030-workbook09-student-3.0-0

4. Sending Signals .................................................................................................................................... 43Discussion ........................................................................................................................................ 43

Signals .................................................................................................................................... 43Why Are Signals Sent?........................................................................................................... 44Sending Signals: the kill Command....................................................................................... 45Receiving Signals ................................................................................................................... 45Using Signals to Terminate Processes .................................................................................... 46Alternatives to the kill Command .......................................................................................... 47

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

Online Exercises............................................................................................................................... 50Specification ........................................................................................................................... 50Deliverables ............................................................................................................................ 50

Questions.......................................................................................................................................... 515. Job Control........................................................................................................................................... 55

Discussion ........................................................................................................................................ 55Running Commands in the Foreground.................................................................................. 55Running Commands in the Background as Jobs ....................................................................55Managing Multiple Jobs ......................................................................................................... 56Killing Jobs............................................................................................................................. 58Summary................................................................................................................................. 58

Examples.......................................................................................................................................... 59Example 1. Deciding to Background a Command Running in the Foreground.....................59Example 2. Using CTRL-C to Kill a Background Job..........................................................59

Online Exercises............................................................................................................................... 60Specification ........................................................................................................................... 60Deliverables ............................................................................................................................ 60Clean Up................................................................................................................................. 60

Questions.......................................................................................................................................... 606. Scheduling Delayed Tasks: at ............................................................................................................. 64

Discussion ........................................................................................................................................ 64Daemons ................................................................................................................................. 64The atd Daemon..................................................................................................................... 64Submitting Jobs with at.......................................................................................................... 65Delaying Tasks with batch..................................................................................................... 66Summary of at Commands..................................................................................................... 66

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

Online Exercises............................................................................................................................... 68Online Exercise 1. Submitting a job for Delayed Execution..................................................69

Questions.......................................................................................................................................... 69

rha030-3.0-0-en-2005-08-17T07:23:17-0400Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red HatAcademy. Any other use is a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated,stored in a retrieval system, or otherwise duplicated whether in electronic or print format without prior written consent of Red Hat, Inc.If you believe Red Hat course materials are being used, copied, or otherwise improperly distributed please email [email protected] phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

iv

Page 5: rha030-workbook09-student-3.0-0

7. Scheduling Periodic Tasks: cron......................................................................................................... 72Discussion ........................................................................................................................................ 72

Performing Periodic Tasks...................................................................................................... 72The cron Service..................................................................................................................... 72crontab Syntax..................................................................................................................... 72Using the crontab Command................................................................................................. 73Editing crontab Files in Place.............................................................................................. 75Where does the output go? ..................................................................................................... 75Environment Variables and cron............................................................................................. 76

Examples.......................................................................................................................................... 76Example 1. Monitoring a Web Site ........................................................................................ 77Example 2. Monitoring Large Files........................................................................................ 77Example 3. Running scripts from cron................................................................................... 78Example 4. Printing fanmail................................................................................................... 79

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

Questions.......................................................................................................................................... 80

rha030-3.0-0-en-2005-08-17T07:23:17-0400Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use is a violationof U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whether in electronic or printformat without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributed please [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

v

Page 6: rha030-workbook09-student-3.0-0

Chapter 1. An Introduction to Processes

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 aweb browser, that browser is running as a process. If you are typing at a bash shell’s command line, thatshell is running as a process. If you are using the chmod command to change a file’s permissions, thechmod command operates as a separate process. Processes are how things get done, and the primaryresponsibility of the Linux kernel is to provide a place for processes to do their stuff without stepping oneach 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,and date) were started, did their thing, and exited.

6

Page 7: rha030-workbook09-student-3.0-0

Chapter 1. An Introduction to Processes

What is a Process?By this point, you could well be tired of hearing the answer: a process in an instance of a runningprogram. 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. BecauseLinux (and Unix) is designed to be a multiuser environment, the memory allocated to a process isprotected, and no other process can access it. In its memory, a process loads a copy of its executableinstructions, and stores any other dynamic information it is managing. A process also carriesparameters associated with how often it gets the opportunity to access the CPU, such as itsexecution state and its niceness value (more on these soon).

I/O Context

Every process interacts to some extent with the filesystem in order to read or write information thatexists before or will exist after the lifespan of the process. Elements of a process’s input/outputcontext include the following.

Open File Descriptors

Almost every process is reading information from or writing information to external sources,usually both. In Linux, open file descriptors act as sources or sinks of information. Processesread information from or write information to file descriptors, which may be connected toregular files, device nodes, network sockets, or even each other as pipes (allowing interprocesscommunication).

Memory Mapped Files

Memory mapped files are files whose contents have been mapped directly into the process’smemory. Rather than reading or writing to a file descriptor, the process just accesses theappropriate memory address. Memory maps are most often used to load a process’s executablecode, but may also be used for other types of non-sequential access to data.

Filesystem Context

We have encountered several pieces of information related to the filesystem that processesmaintain, such as the process’s current working directory (for translating relative filereferences) and the process’s umask (for setting permissions on newly created files). 1

Environment Variables

Every process maintains its own list of name-value pairs, referred to as environment variables, orcollectively as the process’s environment. Processes generally inherit their environment on startup,and may refer to it for information such as the user’s preferred language or favorite editor.

Heritage Information

Every process is identified by a PID, or process id, which it is assigned when it is created. In a laterLesson, we will discover that every process has a clearly defined parent and possibly well defined

rha030-3.0-0-en-2005-08-17T07:23:17-0400

Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any otheruse is a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwiseduplicated whether in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, orotherwise improperly distributed please email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

7

Page 8: rha030-workbook09-student-3.0-0

Chapter 1. An Introduction to Processes

children. A process’s own identity, the identity of its children, and to some extent the identity of itssiblings are maintained by the process.

Credentials

Every process runs under the context of a given user (or, more exactly, a given user id), and underthe context of a collection of group id’s (generally, all of the groups that the user belongs to). Thesecredentials limit what resources a process can access, such as which files it can open or with whichother processes it is allowed to communicate.

Resource Statistics and Limits

Every process also records statistics to track the extent to which system resources have beenutilized, such as its memory size, its number of open files, its amount of CPU time, and others. Theamount of many of these resources that a process is allowed to use can also be limited, a conceptcalled resource limits.

Viewing Processes with the ps CommandWe have already encountered the ps command many times. Now, we will attempt to familiarizeourselves with a broader selection of the many command line switches associated with it. A quick ps--help will display a summary of over 50 different switches for customizing the ps command’s behavior.To complicate 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 commandtries 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 SelectionBy 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. Thefollowing command line switches can be used to expand (or reduce) the processes which the pscommand 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

rha030-3.0-0-en-2005-08-17T07:23:17-0400Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any otheruse is a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwiseduplicated whether in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, orotherwise improperly distributed please email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

8

Page 9: rha030-workbook09-student-3.0-0

Chapter 1. An Introduction to Processes

Output SelectionAs 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 fields

for str can be listed with ps L, or by consulting the ps(1) manpage.)

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 hierarchyf, --forest Show process hierarchy including ASCII decorationsh Do not print header lines-w "wide" output (include longer command names)

Oddities of the ps CommandThe 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 allprocesses, 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 samestyle can 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,providing a snapshot of an instance in time. In contrast, the top command is useful for monitoring thegeneral state of affairs of processes on the machine.

rha030-3.0-0-en-2005-08-17T07:23:17-0400Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any otheruse is a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwiseduplicated whether in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, orotherwise improperly distributed please email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

9

Page 10: rha030-workbook09-student-3.0-0

Chapter 1. An Introduction to Processes

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

17:46:38 up 4:28, 7 users, load average: 0.07, 0.02, 0.00101 processes: 100 sleeping, 1 running, 0 zombie, 0 stoppedCPU states: 3.4% user 1.0% system 0.0% nice 0.0% iowait 95.4% idleMem: 255232k av, 232796k used, 22436k free, 0k shrd, 32404k buff

146208k actv, 33884k in_d, 4716k in_cSwap: 522104k av, 88368k used, 433736k free 104280k cached

PID USER PRI NI SIZE RSS SHARE STAT %CPU %MEM TIME CPU COMMAND1150 einstein 15 0 4468 4108 2252 S 2.9 1.6 0:51 0 metacity1186 einstein 15 0 3132 2112 1436 S 0.9 0.8 0:04 0 battstat-appl3057 einstein 15 0 19596 18M 12356 S 0.9 7.5 0:06 0 galeon-bin3622 maxwell 22 0 1088 1088 856 R 0.9 0.4 0:00 0 top

1 root 15 0 108 76 52 S 0.0 0.0 0:04 0 init2 root 15 0 0 0 0 SW 0.0 0.0 0:00 0 keventd3 root 15 0 0 0 0 SW 0.0 0.0 0:00 0 kapmd4 root 34 19 0 0 0 SWN 0.0 0.0 0:00 0 ksoftirqd_CPU9 root 25 0 0 0 0 SW 0.0 0.0 0:00 0 bdflush5 root 15 0 0 0 0 SW 0.0 0.0 0:00 0 kswapd6 root 15 0 0 0 0 SW 0.0 0.0 0:00 0 kscand/DMA7 root 15 0 0 0 0 SW 0.0 0.0 0:06 0 kscand/Normal8 root 15 0 0 0 0 SW 0.0 0.0 0:00 0 kscand/HighMe

10 root 15 0 0 0 0 SW 0.0 0.0 0:00 0 kupdated11 root 25 0 0 0 0 SW 0.0 0.0 0:00 0 mdrecoveryd15 root 15 0 0 0 0 SW 0.0 0.0 0:01 0 kjournald

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 Commandq quith or ? helps set the delay between updates (in seconds)space update displayM Sort processes by Memory SizeP Sort processes by CPU (Processor) Activityu Reduce display to processes owned by a specific userk 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 moredetail in a later Lesson.

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

rha030-3.0-0-en-2005-08-17T07:23:17-0400

Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any otheruse is a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwiseduplicated whether in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, orotherwise improperly distributed please email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

10

Page 11: rha030-workbook09-student-3.0-0

Chapter 1. An Introduction to Processes

line 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-monitorApplicationIf 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 from the default Applications menu, selecting SystemTools:System Monitor.

Figure 1-1. The GNOME System Monitor

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.

In the upper right-hand corner of the window, a pop-up menu allows the user to choose betweendisplaying all processes, processes that belong to the user (default), or just processes in the "running"state.

rha030-3.0-0-en-2005-08-17T07:23:17-0400

Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any otheruse is a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwiseduplicated whether in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, orotherwise improperly distributed please email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

11

Page 12: rha030-workbook09-student-3.0-0

Chapter 1. An Introduction to Processes

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

Lastly, the System Monitor provides a second panel, which displays graphs of total CPU utilization andMemory utilization versus time, and a table of disk usage (essentially the output of the df command).

rha030-3.0-0-en-2005-08-17T07:23:17-0400

Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any otheruse is a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwiseduplicated whether in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, orotherwise improperly distributed please email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

12

Page 13: rha030-workbook09-student-3.0-0

Chapter 1. An Introduction to Processes

Figure 1-4. The System Monitor panel of the GNOME System Monitor

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 toreduce the information. In the following, maxwell first looks for all instances of the sshd daemon, andthen for all processes owned by the user maxwell.

[maxwell@station maxwell]$ ps aux | grep sshdroot 829 0.0 0.0 3436 4 ? S 09:13 0:00 /usr/sbin/sshdmaxwell 2200 0.0 0.2 3572 640 pts/8 S 10:10 0:00 grep sshd[maxwell@station maxwell]$ ps aux | grep maxwellroot 2109 0.0 0.3 4108 876 pts/8 S 10:05 0:00 su - maxwellmaxwell 2112 0.0 0.4 4312 1268 pts/8 S 10:05 0:00 -bashmaxwell 2146 1.4 8.3 89256 21232 pts/8 S 10:05 0:04 /usr/lib/mozilla-maxwell 2201 0.0 0.2 2676 724 pts/8 R 10:10 0:00 ps auxmaxwell 2202 0.0 0.2 3576 644 pts/8 S 10:10 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 becauseit was 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]

rha030-3.0-0-en-2005-08-17T07:23:17-0400

Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any otheruse is a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwiseduplicated whether in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, orotherwise improperly distributed please email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

13

Page 14: rha030-workbook09-student-3.0-0

Chapter 1. An Introduction to Processes

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 Process Selection 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 Output Formatting

Switch Effect-d delimiter Use delimiter to delimit each process ID (by default, a newline

is used).-l List process name as well as process ID.

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

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

[maxwell@station maxwell]$ pgrep -l sshd829 sshd[maxwell@station maxwell]$ pgrep -lu maxwell2112 bash2146 mozilla-bin2155 mozilla-bin2156 mozilla-bin2157 mozilla-bin

Examples

Example 1. 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]

rha030-3.0-0-en-2005-08-17T07:23:17-0400Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use isa violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whetherin electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributedplease email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

14

Page 15: rha030-workbook09-student-3.0-0

Chapter 1. An Introduction to Processes

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 roughestimate of the amount of CPU and memory the process is consuming, as well as the state of the process.(Process states will be discussed in the next Lesson).

Example 2. 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 --no0 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 -bash

rha030-3.0-0-en-2005-08-17T07:23:17-0400Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use isa violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whetherin electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributedplease email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

15

Page 16: rha030-workbook09-student-3.0-0

Chapter 1. An Introduction to Processes

0 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,which will be discussed in a later Lesson.

Example 3. Viewing A Particular Command with the "joboriented" FormatIn 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 jPPID PID PGID SID TTY TPGID STAT UID TIME COMMAND1184 2311 2311 2311 pts/4 2311 S 2291 0:01 bash1184 2565 2565 2565 pts/0 2565 S 2291 0:04 bash1184 2757 2757 2757 pts/2 2757 S 2291 0:00 bash1184 3024 3024 3024 pts/3 3052 S 2291 0:00 bash1184 3348 3348 3348 pts/6 3348 S 2291 0:00 bash1184 6033 6033 6033 pts/5 13414 S 2291 0:00 bash1184 6534 6534 6534 pts/8 6534 S 2291 0:00 bash

13163 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.

Example 4. 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...

rha030-3.0-0-en-2005-08-17T07:23:17-0400Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use isa violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whetherin electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributedplease email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

16

Page 17: rha030-workbook09-student-3.0-0

Chapter 1. An Introduction to Processes

vsize VSZvsz VSZwchan WCHAN

Curious about the alarm field, maxwell wants to see what processes have pending alarms. He uses the -ocommand line switch to list all pending alarms, and commands.

[maxwell@station maxwell]$ ps -e -o alarm,cmdALARM CMD

- init [- [keventd]- [kapmd]

...30.00 syslogd -m 0...

Noticing that all of the "interesting" entries start with numbers, he uses the grep command to reduce hisoutput to lines that start with any number of spaces, but whose first non-space character is a digit.

[maxwell@station maxwell]$ ps -e -o alarm,cmd | grep "^ *[0-9]"30.00 syslogd -m 015.00 /usr/sbin/automount --timeout 60 /misc file /etc/auto.misc3600 /usr/sbin/sshd3600 sendmail: accepting connections3600 sendmail: Queue runner@01:00:00 for /var/spool/clientmqueue300 /usr/bin/gdm-binary -nodaemon

10.00 /usr/bin/ssh-agent /etc/X11/xinit/Xclients10.00 /usr/bin/ssh-agent /etc/X11/xinit/Xclients

These are various utilities which are asking to be notified every 15 seconds, every hour, etc., presumablyto poll for some activity.

Online Exercises

Lab ExerciseObjective: 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.

rha030-3.0-0-en-2005-08-17T07:23:17-0400Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use isa violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whetherin electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributedplease email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

17

Page 18: rha030-workbook09-student-3.0-0

Chapter 1. An Introduction to Processes

[student@station student]$ ps aux | head -1 > snapshot[student@station student]$ ps aux | tail +2 | 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.

1. In the file ~/biggest.pid, store the process ID of the process with the largest size of virtualmemory (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 arerunning as 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.

Deliverables

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

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

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

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

5. A running instance of the top command.

rha030-3.0-0-en-2005-08-17T07:23:17-0400

Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use is a violationof U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whether in electronic or printformat without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributed please [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

18

Page 19: rha030-workbook09-student-3.0-0

Chapter 1. An Introduction to Processes

Questions

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

( ) 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

( ) 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

rha030-3.0-0-en-2005-08-17T07:23:17-0400Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use is a violationof U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whether in electronic or printformat without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributed please [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

19

Page 20: rha030-workbook09-student-3.0-0

Chapter 1. An Introduction to Processes

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 are sorted?

( ) a. By pressing the S key.

( ) 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’s output?

( ) 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

rha030-3.0-0-en-2005-08-17T07:23:17-0400Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use is a violationof U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whether in electronic or printformat without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributed please [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

20

Page 21: rha030-workbook09-student-3.0-0

Chapter 1. An Introduction to Processes

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

( ) a. -o( ) b. -c( ) c. --custom( ) d. -f( ) e. None of the above

Notes1. Additionally, processes carry around their own concept of the system’s root directory, for translating

absolute file references. While this is almost aways the root directory /, sometimes it is desirable tochange a processes concept of the root directory. This is referred to as a "chrooted" process.

2. Why did ps show one mozilla-bin process, and pgrep four? The Mozilla web browser is amulti-threaded process. As an artifact of how Linux implements threads, a single multi-threadedprocesses appears in many respects to be multiple processes. The ps command automaticallysuppresses the redundant listings, while pgrep does not. Multi-threaded processes are beyond thescope of this course, and this detail was hoped to be avoided.

rha030-3.0-0-en-2005-08-17T07:23:17-0400Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any otheruse is a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwiseduplicated whether in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, orotherwise improperly distributed please email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

21

Page 22: rha030-workbook09-student-3.0-0

Chapter 2. Process States

Key Concepts• In Linux, the first process, /sbin/init, is started by the kernel on bootup. All other processes are the

result of 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 andresource usage 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 StartedIn 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 a technique called forking. When a process forks, it creates aduplicate of itself. Immediately after a fork, the newly created process (the child) is an almost exactduplicate of the original process (the parent). The child inherits an identical copy of the originalprocess’s memory, any open files of the parent, and identical copies of any parameters of the parent,such as the current working directory or umask. About the only difference between the parent andthe child is the child’s heritage information (the child has a different process ID and a differentparent process ID, for starters), and (for the programmers in the audience) the return value of thefork() system call.

As a quick aside for any programmers in the audience, a fork is usually implemented using astructure similar to the following.

22

Page 23: rha030-workbook09-student-3.0-0

Chapter 2. Process States

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 new process, it calls the fork() system call (with no arguments).Though only one process enters the fork() call, two processes return from in. For the newly createdprocess (the child), the return value is 0. For the original process (the parent), the return value is theprocess ID of the child. By branching on this value, the child may now go off to do whatever it wasstarted to do (which often involves exec()ing, see next), and the parent can go on to do its own thing.

Executing a New Command (Exec-ing)

New commands are run through a technique called execing (short for executing). When execing anew command, the current process wipes and releases most of its resources, and loads a new set ofinstructions from the command specified in the filesystem. Execution starts with the entry point ofthe new program.

After execing, the new command is still the same process. It has the same process ID, and many ofthe same parameters (such as its resource utilization, umask, current working directory, and others).It merely forgets its former command, and adopts the new one.

Again for any programmers, execs are performed through one of several variants of the 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 all goes well, theexecl(...) call never returns. Instead, execution picks up at the entry point (i.e., main()) of the newprogram. If for some reason execl(...) does return, it must be an error (such as not being able tolocate the command’s executable in the filesystem).

Combining the Two: Fork and Exec

Some programs may fork without execing. Examples include networking daemons, who fork a newchild to handle a specific client connection, while the parent goes back to listen for new clients.Other programs might exec without forking. Examples include the login command, which becomesthe user’s login shell after successfully confirming a user’s password. Most often, and for shell’s inparticular, however, forking and execing go hand and hand. When running a command, the bashshell first forks a new bash shell. The child then execs the appropriate command, while the parentwaits for the child to 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

rha030-3.0-0-en-2005-08-17T07:23:17-0400Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any otheruse is a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwiseduplicated whether in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, orotherwise improperly distributed please email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

23

Page 24: rha030-workbook09-student-3.0-0

Chapter 2. Process States

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]$ pstreeinit-+-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

...

How a Process DiesWhen 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 laterLesson.

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 thereturn code (and some resource utilization accounting information). It is the responsibility of theprocess’s parent to collect this information, and free up the last remaining resources of the dead child.For example, when the bash shell forks and execs the chmod command, it is the parent bash shell’sresponsibility to collect the return value from the exited chmod command.

rha030-3.0-0-en-2005-08-17T07:23:17-0400Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any otheruse is a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwiseduplicated whether in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, orotherwise improperly distributed please email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

24

Page 25: rha030-workbook09-student-3.0-0

Chapter 2. Process States

OrphansIf 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 firstprocess has a disproportionately large number of children. Most of these were adopted as the orphans ofother processes).

ZombiesIn 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 ZombiesOccasionally, 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,enter the zombie state, and stay there. This is usually the case when users witness zombie processesusing, for example, 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 cluesdiscussed in this section, can you figure out how to get rid of long lived zombies?

The 5 Process StatesThe 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.

Runnable (R)

Processes in the Runnable state are processes that, if given the opportunity to access the CPU,would take it. Multiple processes can (and often are) in the runnable state, but because only oneprocess may be executing on the CPU at any given time, only one of these processes will actually berunning at any given instance. Because runnable processes are switched in and out of the CPU soquickly, however, the Linux system gives the appearance that all of the processes are runningsimultaneously. 2

rha030-3.0-0-en-2005-08-17T07:23:17-0400Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any otheruse is a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwiseduplicated whether in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used,copied, or otherwise improperly distributed please email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

25

Page 26: rha030-workbook09-student-3.0-0

Chapter 2. Process States

Voluntary (Interruptible) Sleep (S)

As the name implies, a process which is in a voluntary sleep elected to be there. Usually, this is aprocess that has nothing to do until something interesting happens. A classic example is anetworking daemon, such as the httpd process that implements a web server. In between requestsfrom a client (web browser), the server has nothing to do, and elects to go to sleep. Anotherexample would be the top command, which lists processes every five seconds. While it is waitingfor five seconds to pass, it drops itself into a voluntary sleep. When something that the process ininterested in happens (such as a web client makes a request, or a five second timer expires), thesleeping process is kicked back into the Runnable state.

Involuntary (Non-interruptible) Sleep (D)

Occasionally, two processes try to access the same system resource at the same time. For example,one process attempts to read from a block on a disk while that block is being written to because ofanother process. In these situations, the kernel forces the process into an involuntary sleep. Theprocess did not elect to sleep, it would prefer to be runnable so it can get things done. When theresource is freed, the kernel will put the process back into the runnable state.

Although processes are constantly dropping into and out of involuntary sleeps, they usually do notstay there long. As a result, users do not usually witness processes in an involuntary sleep except onbusy systems.

Stopped (Suspended) Processes (T)

Occasionally, users decide to suspend processes. Suspended processes will not perform any actionsuntil they are restarted by the user. In the bash shell, the CTRL-Z key sequence can be used tosuspend a process. In programming, debuggers often suspend the programs the are debugging whencertain events happen (such as breakpoints occur).

Zombie Processes (Z)

As mentioned above, every dieing process goes through a transient zombie state. Occasionally,however, some get stuck there. Zombie processes have finished executing, and have freed all of theirmemory and almost all of their resources. Because they are not consuming any resources, they arelittle more than an annoyance that can show up in process 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

rha030-3.0-0-en-2005-08-17T07:23:17-0400

Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use isa violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whetherin electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributedplease email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

26

Page 27: rha030-workbook09-student-3.0-0

Chapter 2. Process States

Examples

Example 1. Identifying Process States[maxwell@station maxwell]$ ps -alx

F UID PID PPID PRI NI VSZ RSS WCHAN STAT TTY TIME COMMAND100 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 -bash004 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 updatedb000 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 top004 501 5486 1220 16 0 0 0 do_exi Z Í ? 0:00 [netstat000 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.

Online Exercises

Lab ExerciseObjective: Explore different process states

Estimated Time: 10 mins.

Specification

1. In order to explore process states, we must create processes which are competing for the sameresources. Use a simple text editor to create the following script, store it as ~/clobber_it.sh, andmake it executable.

rha030-3.0-0-en-2005-08-17T07:23:17-0400Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any otheruse is a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise dupli-cated whether in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or other-wise improperly distributed please email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

27

Page 28: rha030-workbook09-student-3.0-0

Chapter 2. Process States

[maxwell@station maxwell]$ cat clobber_it.sh#!/bin/bash

for i in $(seq 1000); doecho "hello world" > poor_overworked_file

done

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 overthe same 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 inyour own username).

4. When we say go, in a separate terminal (either another terminal window in an X graphicalenvironment, or a separate virtual console), run the script really_clobber_it.sh. This will startabout 1000 processes on your machine, which will obviously stress the system, but it should be ableto handle it. 3 The system may seem sluggish, but with patience, it should still be responsive. Ifthings 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 manyprocesses starting and stopping, as well as many processes in the “D” (involuntary sleep) state. If youare 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 thepoint, you can cancel the clobber_it_lots.sh script with a CTRL-C, and quit the top command.

Deliverables

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

rha030-3.0-0-en-2005-08-17T07:23:17-0400

Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use is a violationof U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whether in electronic or printformat without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributed please [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

28

Page 29: rha030-workbook09-student-3.0-0

Chapter 2. Process States

Questions

1. 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 hundredsof were to request information stored in the same file concurrently, which state would most of the httpd processesbe 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 process probably befound 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 the process be found in?

( ) a. runnable

( ) b. voluntary sleep

( ) c. involuntary sleep

( ) d. stopped

( ) e. zombie

rha030-3.0-0-en-2005-08-17T07:23:17-0400Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use is a violationof U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whether in electronic or printformat without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributed please [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

29

Page 30: rha030-workbook09-student-3.0-0

Chapter 2. Process States

5. You are running a complicated application (such as evolution), that uses many subprocesses to perform its varioustasks. Occasionally, some of the subprocesses seem to be finished (taking no memory or CPU time), but still showup 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 which show up. Whatadvice should you give her?

( ) a. Your friend should reboot the machine when its convenient, because the zombie processes will slowlybegin consuming more and more resources.

( ) b. Your friend should not be too concerned. They are not consuming resources, and will go away when theirparent process dies. Besides, they’re fun to talk about.

( ) c. Your friend should reboot the machine immediately, before the zombie processes begin infecting otherprocesses, turning them into zombies as well. What a ghoulish nightmare that would 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

rha030-3.0-0-en-2005-08-17T07:23:17-0400Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use is aviolation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whether inelectronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributedplease email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

30

Page 31: rha030-workbook09-student-3.0-0

Chapter 2. Process States

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

( ) 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

Notes1. Linux also includes kernel threads, which in special situations may fork and exec to become user

processes, confusing matters. This behavior is not usually found in traditional Unix, however, and isvery much the exception instead of the rule.

2. On a multiprocessor system, however, one process can be running on each processor simultaneously.

3. If multiple students are using the same machine simultaneously, you might want to coordinate so thatall students are not running the script at once.

rha030-3.0-0-en-2005-08-17T07:23:17-0400Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any otheruse is a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwiseduplicated whether in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, orotherwise improperly distributed please email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

31

Page 32: rha030-workbook09-student-3.0-0

Chapter 3. Process Scheduling: nice andrenice

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’spriority, 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 implementscheduling, and the technique has varied from kernel release to kernel release. While the followingdiscussion is not correct at a detailed level, it nevertheless conveys the essence of how the Linux kernelschedules processes.

In order to more easily illustrate scheduling, maxwell will start four versions of the cat command,running in the background. (Processes can be run in the background by appending an ampersand (“& ”),as will be discussed in a later Lesson). The cat commands read from /dev/zero (a pseudo device thatacts as an infinite source of binary zeros), and write to /dev/null (a pseudo device which throws awayeverything that 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 &

32

Page 33: rha030-workbook09-student-3.0-0

Chapter 3. Process Scheduling: nice and renice

[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.

00:25:43 up 11:07, 10 users, load average: 6.02, 5.08, 3.01128 processes: 121 sleeping, 7 running, 0 zombie, 0 stoppedCPU states: 8.5% user 3.2% system 4.6% nice 0.0% iowait 83.5% idleMem: 255232k av, 251560k used, 3672k free, 0k shrd, 36592k buff

162724k actv, 33644k in_d, 3952k in_cSwap: 522104k av, 145148k used, 376956k free 74780k cached

PID USER PRI NI SIZE RSS SHARE STAT %CPU %MEM TIME CPU COMMAND6698 maxwell 25 0 404 400 352 R 19.3 0.1 0:24 0 cat6699 maxwell 22 0 400 400 352 R 19.3 0.1 0:24 0 cat6700 maxwell 24 0 400 372 352 R 19.3 0.1 0:24 0 cat6757 einstein 21 0 25992 24M 3596 R 17.4 9.7 0:04 0 jade6701 maxwell 25 0 400 400 352 R 16.4 0.1 0:24 0 cat6686 maxwell 35 10 6608 4836 1108 R N 4.8 1.8 0:44 0 xlyap6758 maxwell 19 0 1120 1120 852 R 2.9 0.4 0:00 0 top1063 root 15 0 33304 16M 692 S 1.9 6.5 33:56 0 X

1 root 15 0 108 76 52 S 0.0 0.0 0:04 0 init2 root 15 0 0 0 0 SW 0.0 0.0 0:00 0 keventd

...

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 PrioritiesWhen 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 isreferred to as the end of a scheduling epoch, and at this point, the kernel starts all of the processes overagain with a 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 topcommand) have the scheduling advantage. In the output above, the cat commands, which are constantlyin the runnable state, are consuming their counters. The init process, however, who is sleeping quietly inthe background, is not.

rha030-3.0-0-en-2005-08-17T07:23:17-0400Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any otheruse is a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwiseduplicated whether in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, orotherwise improperly distributed please email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

33

Page 34: rha030-workbook09-student-3.0-0

Chapter 3. Process Scheduling: nice and renice

Process NicenessAs mentioned above, every process also has a static value referred to as its niceness value. This valuemay range from -20 to 19 for any process, starting at 0 by default. How does a process’s nicenessinfluence its scheduling? At the beginning of a scheduling epoch, you can think of the kernel subtractinga process’s niceness value from the number of counters the process is allocated. As a result, "nicer"processes (those with 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 theCPU. If a "nice" process is the only one running on the machine, however, it would get full access to theCPU.

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.

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

Using nice to Start a low Priority CommandThe 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 nice aspossible, 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, butinstead the numeric command line switch 19. The user maxwell then again monitors processes using thetop command. The first few processes listed are listed below.

PID USER PRI NI SIZE RSS SHARE STAT %CPU %MEM TIME CPU COMMAND7192 maxwell 25 0 400 400 352 R 22.7 0.1 0:35 0 cat7193 maxwell 25 0 400 400 352 R 22.7 0.1 0:35 0 cat7191 maxwell 25 0 400 400 352 R 21.9 0.1 0:36 0 cat7194 maxwell 25 0 404 404 352 R 21.9 0.1 0:35 0 cat1184 einstein 15 0 11140 8708 3144 R 4.7 3.4 1:28 0 gnome-termina7198 maxwell 39 19 404 404 352 R NÊ 2.1Ë 0.1 0:02 0 simulation4293 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 about2.1% of the CPU.

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

rha030-3.0-0-en-2005-08-17T07:23:17-0400Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use isa violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whetherin electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributedplease email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

34

Page 35: rha030-workbook09-student-3.0-0

Chapter 3. Process Scheduling: nice and renice

[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 COMMAND7198 maxwell 39 19 404 404 352 R N 94.4 0.1 0:51 0 simulation7210 maxwell 20 0 1120 1120 852 R 2.8 0.4 0:00 0 top1063 root 15 0 33516 17M 832 S 1.9 7.0 47:01 0 X4212 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 witha niceness value of 10, the following command line would result in the simulation running with aniceness value of 15.

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

Using renice to Alter a Running ProcessThe 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.

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 COMMANDmaxwell 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 COMMAND0 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.

rha030-3.0-0-en-2005-08-17T07:23:17-0400Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any otheruse is a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwiseduplicated whether in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, orotherwise improperly distributed please email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

35

Page 36: rha030-workbook09-student-3.0-0

Chapter 3. Process Scheduling: nice and renice

He decides to be more polite to other people who might be using the machine, and uses the renicecommand to bump up the process’s niceness value. In the absence of any command line switches, therenice command expects 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 ProcessAs mentioned in the previous unit, the top command uses the r key to renice a process. Whilemonitoring processes 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 GreedierWhat 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.

Examples

Example 1. 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

rha030-3.0-0-en-2005-08-17T07:23:17-0400Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use isa violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whetherin electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributedplease email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

36

Page 37: rha030-workbook09-student-3.0-0

Chapter 3. Process Scheduling: nice and renice

[maxwell@station maxwell]$ ps -alxF 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 bash000 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 -alxF 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 tail000 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 bash000 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.

rha030-3.0-0-en-2005-08-17T07:23:17-0400

Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use isa violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whetherin electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributedplease email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

37

Page 38: rha030-workbook09-student-3.0-0

Chapter 3. Process Scheduling: nice and renice

Example 2. Changing Priorities with reniceUsing the -u command line switch, the user maxwell can change the niceness values for all of hisprocesses at once.

[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 Exercises

Lab ExerciseObjective: Change the scheduling priorities of processes.

Estimated Time: 10 mins.

Specification

1. 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 byyou to 5. (You might want to consider using the pgrep command in conjunction with the xargscommand for 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.

rha030-3.0-0-en-2005-08-17T07:23:17-0400Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any otheruse is a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwiseduplicated whether in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, orotherwise improperly distributed please email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

38

Page 39: rha030-workbook09-student-3.0-0

Chapter 3. Process Scheduling: nice and renice

Deliverables

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.

16:41:18 up 7:33, 6 users, load average: 4.05, 1.63, 1.07107 processes: 95 sleeping, 12 running, 0 zombie, 0 stoppedCPU states: 18.7% user 68.7% system 12.5% nice 0.0% iowait 0.0% idleMem: 255232k av, 230320k used, 24912k free, 0k shrd, 18116k buff

140940k actv, 33000k in_d, 4528k in_cSwap: 522104k av, 124000k used, 398104k free 112256k cached

PID USER PRI NI SIZE RSS SHARE STAT %CPU %MEM TIME CPU COMMAND9311 maxwell 24 0 400 400 352 R 15.9 0.1 0:03 0 sim_a9312 maxwell 24 0 400 400 352 R 15.9 0.1 0:03 0 sim_a9313 maxwell 24 0 400 400 352 R 14.3 0.1 0:03 0 sim_a9308 maxwell 30 5 400 400 352 R N 11.1 0.1 0:07 0 sim_b9307 maxwell 30 5 400 400 352 R N 10.5 0.1 0:07 0 sim_b9305 maxwell 30 5 516 516 452 R N 9.7 0.2 0:18 0 sim_b9306 maxwell 30 5 404 404 352 R N 9.7 0.1 0:08 0 sim_b2648 maxwell 16 0 9752 8496 2980 R 6.5 3.3 2:00 0 gnome-termina9309 maxwell 37 15 404 404 352 R N 2.7 0.1 0:01 0 sim_c9310 maxwell 37 15 400 400 352 R N 2.7 0.1 0:01 0 sim_c9314 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 init2 root 15 0 0 0 0 SW 0.0 0.0 0:00 0 keventd3 root 15 0 0 0 0 SW 0.0 0.0 0:00 0 kapmd4 root 34 19 0 0 0 SWN 0.0 0.0 0:00 0 ksoftirqd_CPU

rha030-3.0-0-en-2005-08-17T07:23:17-0400Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use is a violationof U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whether in electronic or printformat without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributed please [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

39

Page 40: rha030-workbook09-student-3.0-0

Chapter 3. Process Scheduling: nice and renice

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 theoutput 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_a commands to19?

( ) a. renice -c sim_a 19

( ) b. renice 19 9311

( ) c. renice -c sim_a -19

( ) d. renice -19 9311

( ) e. None of the above

rha030-3.0-0-en-2005-08-17T07:23:17-0400Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use is a violationof U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whether in electronic or printformat without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributed please [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

40

Page 41: rha030-workbook09-student-3.0-0

Chapter 3. Process Scheduling: nice and renice

5. If you were to look at the output of the same top command in another few seconds, what would you expect to seefor the priority of the sim_b simulations (assuming machine activity does not alter 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 his sim_c processes to10?

( ) 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

rha030-3.0-0-en-2005-08-17T07:23:17-0400Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use is aviolation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whether inelectronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributedplease email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

41

Page 42: rha030-workbook09-student-3.0-0

Chapter 3. Process Scheduling: nice and renice

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

( ) 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 to 10, includingthe 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 the niceness value ofits 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.

rha030-3.0-0-en-2005-08-17T07:23:17-0400

Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any otheruse is a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwiseduplicated whether in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, orotherwise improperly distributed please email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

42

Page 43: rha030-workbook09-student-3.0-0

Chapter 4. Sending Signals

Key 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 commandname, or the user who owns them.

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

Discussion

SignalsLinux (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 ofabnormal behavior, such as if the process tries to divide a number by zero, or tries to access memory thatdoes not belong 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

bash xclock15

43

Page 44: rha030-workbook09-student-3.0-0

Chapter 4. Sending Signals

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 listedusing the kill -l command, or examined in the signal(7) man page.

[einstein@station einstein]$ kill -l1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2

13) SIGPIPE 14) SIGALRM 15) SIGTERM 17) SIGCHLD18) 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 32through 63 (which are not standard among the various versions of Unix) are "real time" signals, andbeyond the scope 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 some erroneous operation. For example, the kernel willsend a process a SIGFPE (signal number 8) if it performs a divide by 0.

Software Conditions

Processes may need to be notified of some abnormal software condition. For example, whenever aprocess dies, the kernel sends a SIGCHLD (signal number 17) to the process’s parent. As anotherexample, X graphical applications receive a SIGWINCH (signal number 28) whenever theirwindow is resized, so that they can respond to the new geometry.

Terminal Interrupts

Various terminal control key sequences send signals to the bash shell’s foreground process. Forexample, CTRL-C sends a SIGINT (signal number 2), while CTRL-Z sends a SIGTSTP (signalnumber 20).

rha030-3.0-0-en-2005-08-17T07:23:17-0400

Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any otheruse is a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwiseduplicated whether in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, orotherwise improperly distributed please email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

44

Page 45: rha030-workbook09-student-3.0-0

Chapter 4. Sending Signals

Other Processes

Processes may elect to send any signals to any other process which is owned by the same user. Thekill command 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,which specifies which process should receive it. As an example, the following commands deliver aSIGCHLD (signal number 17) to the xclock process, process ID number 8060.

[einstein@station einstein]$ psPID TTY TIME CMD

7985 pts/5 00:00:00 bash8060 pts/5 00:00:01 xclock8061 pts/5 00:00:00 ps

[einstein@station einstein]$ kill -17 8060[einstein@station einstein]$ kill -CHLD 8060[einstein@station einstein]$ kill -SIGCHLD 8060

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 default response which is implemented by the kernel. Eachsignal is mapped to one of the following behaviors.

• Terminate: The receiving process is killed.

• Ignore: The receiving process ignores the signal

• Core: The receiving process terminates, but first dumps an image of its memory into a file namedcore in the process’s current working directory. The core file can be used by developers to helpdebug the program. 1

• Stop: Stop (suspend) the process.

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

Choose to Ignore the Signal

Programmers may elect for their application to simply ignore specified signals.

Choose to Implement a Custom Signal Handler

Programmers may elect to implement their own behavior when a specified signal is received. Theresponse of the program is completely determined by the programmer.

rha030-3.0-0-en-2005-08-17T07:23:17-0400Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any otheruse is a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwiseduplicated whether in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, orotherwise improperly distributed please email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

45

Page 46: rha030-workbook09-student-3.0-0

Chapter 4. Sending Signals

Unless a program’s documentation says otherwise, you can usually assume that a process will respondwith the 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 Action2 SIGINT Interrupt (request termination) of the process.

This is the signal generated by the CTRL-Ccontrol sequence.

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

15 SIGTERM Request Termination of the process.20 SIGTSTP Stop (suspend) the process. This is the signal

generated 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 aprocess.

As an example, einstein will start a cat command that would in principle run forever. He then tracksdown the 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 CMD7985 pts/5 00:00:00 bash8375 pts/5 00:00:01 cat8376 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 usedkill 8375 to the same effect. In the following, einstein repeats the sequence, this time sending aSIGKILL.

[einstein@station einstein]$ cat /dev/zero > /dev/null &[1] 8387[einstein@station einstein]$ ps

PID TTY TIME CMD7985 pts/5 00:00:00 bash

rha030-3.0-0-en-2005-08-17T07:23:17-0400Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use isa violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whetherin electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributedplease email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

46

Page 47: rha030-workbook09-student-3.0-0

Chapter 4. Sending Signals

8387 pts/5 00:00:01 cat8388 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 CommandUsing signals to control processes is such a common occurrence, alternatives to using the kill commandabound. The following sections mention a few.

The pkill CommandIn 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 processesselected by 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.

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]$ psPID TTY TIME CMD

10353 pts/4 00:00:00 bash10396 pts/4 00:00:00 xclock10397 pts/4 00:00:03 mozilla-bin10422 pts/4 00:00:00 ps[maxwell@station maxwell]$ pkill -u maxwell[1]- Terminated xclock[maxwell@station maxwell]$ ps

PID TTY TIME CMD10353 pts/4 00:00:00 bash10424 pts/4 00:00:00 ps[2]+ Exit 15 mozilla

rha030-3.0-0-en-2005-08-17T07:23:17-0400

Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any otheruse is a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwiseduplicated whether in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, orotherwise improperly distributed please email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

47

Page 48: rha030-workbook09-student-3.0-0

Chapter 4. Sending Signals

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

The killall CommandSimilar to pkill, the killall command delivers signals to processes specified by command name. Thekillall command 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 MonitorThe 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 EndProcess, which has the effect of delivering a SIGTERM to the process. What do you think the KillProcess menu selection does?

Figure 4-2. Terminating Processes using the System Monitor

The top CommandThe 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.

rha030-3.0-0-en-2005-08-17T07:23:17-0400Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use isa violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whetherin electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributedplease email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

48

Page 49: rha030-workbook09-student-3.0-0

Chapter 4. Sending Signals

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

Examples

Example 1. 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 Mozilla webbrowser, and the xclock graphical clock. He uses the pgrep command to locate their process ID’s, killsthe first using the kill command, and the second using the pkill command.

[maxwell@station maxwell]$ pgrep -lu maxwell2112 bash2146 mozilla-bin2155 mozilla-bin2156 mozilla-bin2157 mozilla-bin2329 mozilla-bin2597 xclock[maxwell@station maxwell]$ kill 2146[maxwell@station maxwell]$ pkill xclock[1]- Exit 15 mozilla[2]+ Terminated xclock[maxwell@station maxwell]$ pgrep -lu maxwell2112 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.

Example 2. 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 maxwell

rha030-3.0-0-en-2005-08-17T07:23:17-0400Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use isa violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whetherin electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributedplease email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

49

Page 50: rha030-workbook09-student-3.0-0

Chapter 4. Sending Signals

2112 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

Example 3. Make it Stop!The combination of the pkill’s ability to select all processes owned by a particular user, and theSIGKILL signal’s low level lethalness, makes a convenient technique for a user to kill all processes theymight be running on the local machine.

In the following, maxwell first notes how many processes he’s running on the local machine. He thenslays all processes.

[maxwell@station maxwell]$ pgrep -u maxwell | wc -l22

[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 Exercises

Lab ExerciseObjective: Effectively terminate running processes.Estimated Time: 10 mins.

Specification

1. 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

rha030-3.0-0-en-2005-08-17T07:23:17-0400

Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use is a violationof U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whether in electronic orprint format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributed please [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

50

Page 51: rha030-workbook09-student-3.0-0

Chapter 4. Sending Signals

Deliverables

1. A script shell script called ~/bin/kill_all_cats, which when executed, delivers a SIGTERM signal to allcurrently running instances of the cat command.

2. An executing cat process.

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

[maxwell@station maxwell]$ ps -U maxwellPID TTY TIME CMD

4785 ? 00:00:00 gnome-session4828 ? 00:00:00 ssh-agent

...4846 ? 00:00:00 xscreensaver5410 pts/8 00:00:00 bash5451 ? 00:00:00 same-gnome5452 ? 00:00:00 same-gnome5454 ? 00:00:01 gimp5455 ? 00:00:00 script-fu5463 pts/8 00:00:00 ps5907 pts/7 00:00:00 bash5942 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.

rha030-3.0-0-en-2005-08-17T07:23:17-0400Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use is a violationof U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whether in electronic or printformat without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributed please [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

51

Page 52: rha030-workbook09-student-3.0-0

Chapter 4. Sending Signals

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( ) 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 application a SIGTERMsignal?

( ) a. pkill same-gnome

( ) b. pkill -c same-gnome

( ) c. pkill -u same-gnome

( ) d. pkill -9 same-gnome

( ) e. None of the above

rha030-3.0-0-en-2005-08-17T07:23:17-0400Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use is a violationof U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whether in electronic or printformat without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributed please [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

52

Page 53: rha030-workbook09-student-3.0-0

Chapter 4. Sending Signals

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.

( ) 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.3.4 (#1, Feb 2 2005, 11:44:49)[GCC 3.4.3 20041212 (Red Hat 3.4.3-9.EL4)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>>

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

rha030-3.0-0-en-2005-08-17T07:23:17-0400Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use is a violation of U.S.and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whether in electronic or print format withoutprior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributed please email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

53

Page 54: rha030-workbook09-student-3.0-0

Chapter 4. Sending Signals

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 to terminate theprocess.

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

( ) e. None of the above

Notes1. This response if affectionately referred to as "puking" by many in the Unix community.

rha030-3.0-0-en-2005-08-17T07:23:17-0400Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any otheruse is a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwiseduplicated whether in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, orotherwise improperly distributed please email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

54

Page 55: rha030-workbook09-student-3.0-0

Chapter 5. Job Control

Key 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.When the bash shell backgrounds a command, the command is referred to as a job, and assigned a jobnumber.

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

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

55

Page 56: rha030-workbook09-student-3.0-0

Chapter 5. Job Control

After starting the job in the background, the bash shell reports two pieces of information back toeinstein. The first is the job number, reported in square brackets. The second is the process ID of thebackgrounded job. 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.txt2> /dev/null &[2] 7023[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), andredisplays the command line to remind einstein of what he had run. The bash shell does not reportimmediately when jobs 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 jobsThe 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 &

rha030-3.0-0-en-2005-08-17T07:23:17-0400Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use isa violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whetherin electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributedplease email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

56

Page 57: rha030-workbook09-student-3.0-0

Chapter 5. Job Control

[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 isreferred to as the current job, and is decorated by the jobs command with a “+”.

Bringing a Job to the Foreground with fgA backgrounded job can be brought back to the foreground with the fg builtin command. The fgcommand expects 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 anotherprompt while the process is still running.

Suspending the Foreground Job with CTRL-ZWe 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.

[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.

rha030-3.0-0-en-2005-08-17T07:23:17-0400

Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use isa violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whetherin electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributedplease email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

57

Page 58: rha030-workbook09-student-3.0-0

Chapter 5. Job Control

Restarting a Stopped Job in the BackgroundA stopped job can be restarted in the background with the bg builtin command. Like the fg command,the bg 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 builtincommand. (Confusingly, another version is also found in the filesystem, /bin/kill. You are probablyusing the shell 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 percentcharacter (“%”), 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[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.

rha030-3.0-0-en-2005-08-17T07:23:17-0400

Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any otheruse is a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwiseduplicated whether in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, orotherwise improperly distributed please email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

58

Page 59: rha030-workbook09-student-3.0-0

Chapter 5. Job Control

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

Table 5-1. Job Management in the bash Shell

Command Actionjobs List all jobsfg [N ] Bring background job N to the foreground (by default, the "current"

background job).CTRL-Z Suspend and background the current foreground commandbg [N ] Start stopped background job N (by default, the "current" background job).kill %N Terminate background job N (by sending the SIGTERM signal).

Examples

Example 1. Deciding to Background a Command Running inthe ForegroundThe 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, whichleaves it 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]$

Example 2. Using CTRL-C 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

rha030-3.0-0-en-2005-08-17T07:23:17-0400Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use isa violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whetherin electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributedplease email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

59

Page 60: rha030-workbook09-student-3.0-0

Chapter 5. Job Control

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

Online Exercises

Lab ExerciseObjective: Use bash job control to manage multiple tasks.Estimated Time: 10 mins.

Specification

1. 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.

Deliverables

1. Four background jobs managed by the bash shell. The cat and sleep jobs should jobs should be running, whilethe 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.

rha030-3.0-0-en-2005-08-17T07:23:17-0400

Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use is a violationof U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whether in electronic or printformat without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributed please [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

60

Page 61: rha030-workbook09-student-3.0-0

Chapter 5. Job Control

Questions

1. 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( ) 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 studentPID TTY TIME CMD

1312 pts/0 00:00:00 bash1349 pts/0 00:00:00 sleep1350 pts/0 00:00:00 vim1351 pts/0 00:00:00 ls1355 tty4 00:00:00 bash1395 tty4 00:00:00 find1397 pts/0 00:00:00 ps

rha030-3.0-0-en-2005-08-17T07:23:17-0400

Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use is a violationof U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whether in electronic or printformat without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributed please [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

61

Page 62: rha030-workbook09-student-3.0-0

Chapter 5. Job Control

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?

( ) 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, what action(s) couldyou 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.

rha030-3.0-0-en-2005-08-17T07:23:17-0400Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use is a violationof U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whether in electronic or printformat without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributed please [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

62

Page 63: rha030-workbook09-student-3.0-0

Chapter 5. Job Control

( ) 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 mozilla web browser from the terminal, the command runs in theforeground, and your friend cannot use the terminal again until quitting the browser. 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 an ampersand(“&”) to the command line.

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

( ) 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.

rha030-3.0-0-en-2005-08-17T07:23:17-0400

Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any otheruse is a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwiseduplicated whether in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, orotherwise improperly distributed please email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

63

Page 64: rha030-workbook09-student-3.0-0

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 networkservices, such as the web server (httpd) or the FTP server (vsftpd). Other daemons handle system tasks,such as the logging daemon (syslogd) and the power management daemon (apmd). This Lesson, and thefollowing Lesson, discuss two daemons that allow users to delay tasks (atd), or run commands at fixedintervals (crond). By now, you have probably noticed a naming convention as well: programs meant tobe run as daemons 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 userdaemon.

64

Page 65: rha030-workbook09-student-3.0-0

Chapter 6. Scheduling Delayed Tasks: at

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 examining alist 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

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 commands tobe 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 specifiedusing several formats, 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 themorning.

[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:

rha030-3.0-0-en-2005-08-17T07:23:17-0400Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use isa violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whetherin electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributedplease email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

65

Page 66: rha030-workbook09-student-3.0-0

Chapter 6. Scheduling Delayed Tasks: at

[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 atcommand, batch does not run the command at a specific time, but instead waits until the system is notbusy with other tasks, whenever that time might be. If the machine is not busy when the job is submitted,the job might run immediately. The atd daemon monitors the system’s loadavg, and waits for it to dropbeneath 0.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 useatd 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.

rha030-3.0-0-en-2005-08-17T07:23:17-0400

Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any otheruse is a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwiseduplicated whether in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, orotherwise improperly distributed please email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

66

Page 67: rha030-workbook09-student-3.0-0

Chapter 6. Scheduling Delayed Tasks: at

Examples

Example 1. 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 littleexperience, he develops the following command line to format the page to his liking: enscript -r2 -G--header="Fan Mail" --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

Example 2. 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 fakesnot-so-flattering mail received from not-so-adoring fans. He submits a job which will print the file at 1:59am, 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 owned bythe user ventura.

rha030-3.0-0-en-2005-08-17T07:23:17-0400Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use isa violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whetherin electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributedplease email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

67

Page 68: rha030-workbook09-student-3.0-0

Chapter 6. Scheduling Delayed Tasks: at

[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 0umask 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 XAUTHORITYcd /home/ventura || { Í

echo ’Execution directory inaccessible’ >&2exit 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.

rha030-3.0-0-en-2005-08-17T07:23:17-0400

Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any otheruse is a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwiseduplicated whether in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, orotherwise improperly distributed please email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

68

Page 69: rha030-workbook09-student-3.0-0

Chapter 6. Scheduling Delayed Tasks: at

Online Exercises

Online Exercise 1. Submitting a job for Delayed Execution

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

Estimated Time: 10 mins.

SpecificationYou 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 thecal command, for 3:45 in the morning. Make sure that it is your only job scheduled with the at facility.

Deliverables

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

Questions

1. 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

rha030-3.0-0-en-2005-08-17T07:23:17-0400Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use is a violationof U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whether in electronic or printformat without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributed please [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

69

Page 70: rha030-workbook09-student-3.0-0

Chapter 6. Scheduling Delayed Tasks: at

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?

( ) 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 from where the job wassubmitted?

( ) 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 .

rha030-3.0-0-en-2005-08-17T07:23:17-0400Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use is a violation of U.S.and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whether in electronic or print format withoutprior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributed please email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

70

Page 71: rha030-workbook09-student-3.0-0

Chapter 6. Scheduling Delayed Tasks: at

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 the file’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.

[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 be run using/bin/csh as the interpreter.

( ) b. If the user were to set SHELL=/bin/csh as an environment variable before running the at command, thejob 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 environment variable as theinterpreter.

( ) d. The job will only be run if the user has the SHELL environment variable is set to her login shell.

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

rha030-3.0-0-en-2005-08-17T07:23:17-0400

Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any otheruse is a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwiseduplicated whether in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, orotherwise improperly distributed please email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

71

Page 72: rha030-workbook09-student-3.0-0

Chapter 7. Scheduling Periodic Tasks: cron

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

• The crontab command provides a frontend 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 see ifanything new has been posted.

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

The cron ServiceThe crond daemon is the daemon that performs periodic tasks on behalf of the system or individualusers. Usually, the daemon is started as the system boots, so most users can take it for granted. By listingall processes 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 asroot.

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.

72

Page 73: rha030-workbook09-student-3.0-0

Chapter 7. Scheduling Periodic Tasks: cron

# 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" julius45 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 # are considered comments, and are ignored.

Environment variables

All lines that have the form name = value are used to define environment variables.

Cron commands

Any other (non blank) line is considered a cron command, 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 firstfield)

* every time * every minuten at the specified time 10 every hour at 10 minutes past the hourn, n, ... at any of the specified times 22,52 every hour at 22 and 52 minutes past the

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

minutes past the hour)

rha030-3.0-0-en-2005-08-17T07:23:17-0400

Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any otheruse is a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwiseduplicated whether in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, orotherwise improperly distributed please email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

73

Page 74: rha030-workbook09-student-3.0-0

Chapter 7. Scheduling Periodic Tasks: cron

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

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?

[hogan@station hogan]$ crontab mycopy[hogan@station hogan]$ crontab -l# DO NOT EDIT THIS FILE - edit the master and reinstall.

rha030-3.0-0-en-2005-08-17T07:23:17-0400Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use isa violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whetherin electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributedplease email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

74

Page 75: rha030-workbook09-student-3.0-0

Chapter 7. Scheduling Periodic Tasks: cron

# (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 thecurrent crontab configuration into the user’s default editor. When the user has finished editing the file,and exits the 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.

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 crontabconfiguration.

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

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:

rha030-3.0-0-en-2005-08-17T07:23:17-0400Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use isa violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whetherin electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributedplease email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

75

Page 76: rha030-workbook09-student-3.0-0

Chapter 7. Scheduling Periodic Tasks: cron

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 Sa1 2 3 4 5 6 78 9 10 11 12 13 14

15 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 bythe 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.

rha030-3.0-0-en-2005-08-17T07:23:17-0400

Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any otheruse is a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwiseduplicated whether in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, orotherwise improperly distributed please email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

76

Page 77: rha030-workbook09-student-3.0-0

Chapter 7. Scheduling Periodic Tasks: cron

Examples

Example 1. 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.

Example 2. 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 mailhim a 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 ...)

rha030-3.0-0-en-2005-08-17T07:23:17-0400Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use isa violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whetherin electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributedplease email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

77

Page 78: rha030-workbook09-student-3.0-0

Chapter 7. Scheduling Periodic Tasks: cron

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 $)

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.

Example 3. Running scripts from cronContinuing his efforts to not waste disk space, ventura would like to regularly create a separate list offiles that he has not accessed recently, and he would like to receive this list at the same time as hereceives his list of large files. Rather than overly complicate his cron file, he creates a script calledcron.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.

rha030-3.0-0-en-2005-08-17T07:23:17-0400Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use isa violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whetherin electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributedplease email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

78

Page 79: rha030-workbook09-student-3.0-0

Chapter 7. Scheduling Periodic Tasks: cron

# (/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 script cron.weekly.

Example 4. 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.

[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. When thefirst 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 variable isnot getting configured. He edits crontab.hogan to explicitly define PRINTER, and resubmits the filewith 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

Online Exercise 1. Monitoring Who is on the System.

Online ExerciseObjective: Configure a cron jobEstimated Time: 10 mins.

rha030-3.0-0-en-2005-08-17T07:23:17-0400

Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any otheruse is a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwiseduplicated whether in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, orotherwise improperly distributed please email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

79

Page 80: rha030-workbook09-student-3.0-0

Chapter 7. Scheduling Periodic Tasks: cron

SpecificationYou 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. A cron configuration which mails the output of the who command daily at 4:35 am.

Questions

1. 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

( ) 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

rha030-3.0-0-en-2005-08-17T07:23:17-0400Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use is a violationof U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whether in electronic or printformat without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributed please [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

80

Page 81: rha030-workbook09-student-3.0-0

Chapter 7. Scheduling Periodic Tasks: cron

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.

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.

rha030-3.0-0-en-2005-08-17T07:23:17-0400Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use is a violationof U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whether in electronic or printformat without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributed please [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

81

Page 82: rha030-workbook09-student-3.0-0

Chapter 7. Scheduling Periodic Tasks: cron

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

rha030-3.0-0-en-2005-08-17T07:23:17-0400

Copyright (c) 2003-2005 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any otheruse is a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwiseduplicated whether in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, orotherwise improperly distributed please email [email protected] or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.

82