Download presentation
Presentation is loading. Please wait.
1
Chapter 6: Managing Processes
The Complete Guide to Linux System Administration
2
Objectives Define processes as used by the Linux kernel
View and control running processes using command-line and graphical utilities Understand and manage how memory is used by Linux processes Schedule delayed execution of processes The Complete Guide to Linux System Administration
3
Defining Processes Should be familiar with process definition to better understand How Linux operates How to manage its resources The Complete Guide to Linux System Administration
4
The Linux Multitasking Model
Process Program running on system Started from: Command line Graphical desktop Kernel itself Another process The kernel maintains a collection of information about that program, including the most recent part of the program that was executed, where it stores its data, what resources it is trying to use, and so forth. By maintaining this information about all processes that are running on Linux, the kernel can maintain efficiency and security. The Complete Guide to Linux System Administration
5
The Linux Multitasking Model (continued)
Linux is multitasking operating system Single microprocessor can really only perform one task at a time Time slice A few microseconds Allocated to each process by kernel Common to all operating systems that don’t have multiple microprocessors The Complete Guide to Linux System Administration
6
Creating Processes Kernel starts process called init Process id (PID)
When Linux first started Every process running on Linux is “descendant” of init process Process id (PID) Unique number identifying process within Linux kernel The init process is assigned a process ID number of 1. The Complete Guide to Linux System Administration
7
Creating Processes (continued)
Fork Create new process in Linux Exit When process ends Information about process that kernel was maintaining discarded Resources that kernel had allocated to process released The Complete Guide to Linux System Administration
8
Process States Running : Sleeping : Stopped :
The process is executing, though it must wait for its assigned time slices and must pause for a few microseconds between steps in its execution. Sleeping : The process is waiting for something to happen that doesn't depend on the kernel giving it another time slice. For example, it might be waiting on some input from a device. When a process is sleeping, it doesn't consume any CPU processing time, though it still has memory allocated to it by the kernel and the program itself. Stopped : The process has been halted before it exited normally; this is typically only seen when you are debugging programs that you are writing. Zombie : A zombie process is a process that is no longer active-the program itself has been closed, for example-but the Linux kernel is still maintaining information about the process and allocating resources to it. The kernel regularly clears out any zombie processes and frees up their resources. The Complete Guide to Linux System Administration
9
Swap Space Used when system runs out of RAM for currently running programs Separate hard disk partition called swap partition Also called virtual memory Acts like extension of system’s RAM Swapped-out process cannot run until swapped again The Complete Guide to Linux System Administration
10
Swap Space (continued)
Thrashing Kernel spends so much time moving processes to and from swap space that kernel and processes bog down and work inefficiently The Complete Guide to Linux System Administration
11
Managing Linux Processes
View information about Linux processes Control status of processes The Complete Guide to Linux System Administration
12
Starting Processes from the Shell
When you start program Program takes control of command line man ls , gimp graphical program from a terminal window Parent process pauses to wait for new process to finish Some commands don't display screen output like the man page viewer, but they still leave you without an active prompt to enter additional commands. Type ampersand (&) after name of command Shell forks new process without pausing itself Called placing process in background Can start another command immediately The Complete Guide to Linux System Administration
13
This is called placing the process in the background.
$ gimp If you type an ampersand (&) after the name of a command, the shell forks a new process without pausing itself. This is called placing the process in the background. In other words, both the program you start and the shell itself continue to operate-the shell is not "busy“ running it any longer. This means you can start another command immediately. For example. if you are working in a graphical command-line window and enter the command: $ gimp & the GIMP graphical program appears, but the command prompt also reappears in the terminal window, so you can enter other commands in the same shell where you started GIMP. The Complete Guide to Linux System Administration
14
Starting Processes from the Shell (continued)
Multiple processes started from a single shell are called jobs Job Process associated with shell jobs command Lists all jobs or processes running as descendants of current shell Also called child processes Ctrl+Z key combination Suspend job that shell is running The Complete Guide to Linux System Administration
15
Starting Processes from the Shell (continued)
bg command Run suspended program in background (restart it without displaying output to the current console) fg command Place suspended job in foreground (allow it to take control of the screen again) Must specify: Job number using % Or PID in order to recall a job to the foreground. The Complete Guide to Linux System Administration
16
Practical practise 1. Open a Linux command line within a graphical environment. 2. Start the gedit program: $ gedit This command runs in the foreground and occupies the command line so you can‘t enter other commands. 3. Click on the command line window to make it active, then press Ctrl+z to pause the foreground command (gedit). 4. Enter the jobs command. You see gedit listed, probably as job number 1. 5. gedit is currently paused or suspended. Resume it as a background process by entering this command: $ bg %1 The command line is now free to run other programs. 6. Enter the command man Is. The man page for the Is command appears. 7 . Press Ctrl+z to pause the man command. The Complete Guide to Linux System Administration
17
8. Enter the command man ps. The man page for the ps command appears. 9. Press Ctrl +z to pause the second man command. 10. Enter the command jobs. You see output showing both man commands and the gedit command. 11. Note the job number of the first man command. Enter the command fg %2. (Use the number for the first man command in place of 2 if necessary.) The man ls page appears again. 12. Press q to end the man Is command. 13. Enter the jobs command again. You see that the man Is command is no longer listed. The Complete Guide to Linux System Administration
18
Note Some commands (including the man command) are only used to display information. Starting a man page with an ampersand or using bg while viewing a man page automatically suspends the command. It only runs when in the foreground. process ID (PID) number of a running process. You can use this number instead of the job number when executing fg or bg. PID might be useful both because it is unique for the whole system (where the job number is unique only within a single shell) For example, suppose a process you have started is job number 3 in the current shell and has a PID of 725. You can bring the process to the foreground with either of these commands: $ fg %3 $ fg 725 The Complete Guide to Linux System Administration
19
Starting Processes from the Shell (continued)
Virtual console One type of login screen Start multiple text-based login sessions on same computer Assigned to function keys, typically F1 through F6 Logged in using text-mode console instead of graphical login screen Start working in first virtual console Alt+F2 Access second virtual console The Complete Guide to Linux System Administration
20
Learning about Processes via the Command Line
ps command Lists processes currently running on system Output: PID Terminal that process is using for output CPU time that process has used so far Command that started process a and x options show Processes started by all users Processes started by system at boot time Other processes that have no controlling terminal The Complete Guide to Linux System Administration
21
Learning about Processes via the Command Line (continued)
Daemon Background process Doesn’t have any screen output but waits for certain system activity then acts on it ps command u option shows how each process is using system f option Displays relationship between different processes Shows which processes start other processes Processes presented in tree diagram The Complete Guide to Linux System Administration
22
The first line of the output contains column headings that indicate which user started the process, the percentage of CPU time and memory used by the process, the terminal on which the process is running, current status, and other infornlation. Again, to manage a process, you want to note the PID of the process in the second column from the left. The Complete Guide to Linux System Administration
23
Learning about Processes via the Command Line (continued)
Administrators track how processes consume system resources, particularly: CPU time Memory %CPU field Compares amount of CPU time used by process with total time elapsed since previous computation of %CPU field Form of percentage The Complete Guide to Linux System Administration
24
Learning about Processes via the Command Line (continued)
%CPU field Does not show average amount of CPU time used by process since it was started %WCPU field Weighted to show 30-second average of percentage of CPU time used by process Helpful for showing overall usage pattern The Complete Guide to Linux System Administration
25
Learning about Processes via the Command Line (continued)
TIME field Provides cumulative measure of amount of CPU time consumed by process /proc file system Collection of information about Linux kernel Accessed as if it were data files stored in subdirectories of /proc Need PID to access information Accessed using /proc/PID The Complete Guide to Linux System Administration
26
Learning about Processes via the Command Line (continued)
top utility Displays list of running processes Arranged by how much CPU time each is using Process consuming greatest amount of CPU time shown at top of list Updated regularly Normally started without any options Cannot run in background The Complete Guide to Linux System Administration
27
Learning about Processes via the Command Line (continued)
The Complete Guide to Linux System Administration
28
Controlling Processes
kill command Controls any process Sends signals to processes -l option views all signals Examples: kill kill -SIGTERM 1482 The Complete Guide to Linux System Administration
29
Controlling Processes (continued)
Signals Messages sent between processes About 30 different signals available Has name and number associated with it Software developer decides to which signals program responds SIGTERM signal Number 15 Requests that program end Almost all programs respond The Complete Guide to Linux System Administration
30
Controlling Processes (continued)
SIGKILL signal Number 9 Handled by Linux kernel Shuts down indicated process Unsaved data in program lost Renegade program virtually never crashes Linux kernel The Complete Guide to Linux System Administration
31
Controlling Processes (continued)
killall command Sends signal to all processes started by given command Useful when program makes copies of itself faster than you can locate PIDs and use kill to shut them down Each process assigned priority determines how much CPU time granted to process Normally all processes have same priority The Complete Guide to Linux System Administration
32
Controlling Processes (continued)
Nice level Another name for process priority Standard level is 0 Highest level 20 System administrator can make any process nicer User can raise nice level of process that he or she started The Complete Guide to Linux System Administration
33
Controlling Processes (continued)
Alter process priority nice command nice -5 analyze renice command renice Can issue nice and renice while top running The Complete Guide to Linux System Administration
34
Controlling Processes within top
The Complete Guide to Linux System Administration
35
The Complete Guide to Linux System Administration
36
Controlling Processes (continued)
The Complete Guide to Linux System Administration
37
Using Graphical Process Management Tools
KDE System Guard utility Graphically displays process list Interact with list to rearrange or kill processes GNOME System Monitor Manage processes graphically Gkrellm View system status information on desktop continuously The Complete Guide to Linux System Administration
38
Using Graphical Process Management Tools (continued)
Manage Linux system effectively Keep CPU load monitor visible on Panel Check for processes that might need attention Reduce CPU load Raise nice level of numerous user processes Are not all competing at same level Add second microprocessor Move some tasks to different computer Add memory The Complete Guide to Linux System Administration
39
Managing Memory Other key system resources
Physical memory (RAM) Virtual memory (swap space) More RAM always leads to better performance on busy system The Complete Guide to Linux System Administration
40
Understanding Shared Libraries
Library Contains prewritten functionality that any program can use Statically linked applications Include library functions in main program Dynamically linked applications Assume that needed library files are available on Linux system Use shared libraries ldd command Lists all libraries that program requires $ ldd /bin/ls The Complete Guide to Linux System Administration
41
Understanding Paged Memory
Information transferred to and from swap space in units known as pages Page of memory Block of 4 KB of RAM Kernel keeps track of which pages of memory are moved to swap space The Complete Guide to Linux System Administration
42
Tracking Memory Usage Free command Buffer
Displays information about both RAM and virtual memory All information displayed is in kilobytes Buffer Memory used by application for data storage Several fields of ps command output also provide information about memory usage The Complete Guide to Linux System Administration
43
The Complete Guide to Linux System Administration
44
Viewing Virtual Memory Information
vmstat command View detailed information about how swap space is being used Displayed information based on information averaged over time since system was started Can also run to be continuously updated Like top The Complete Guide to Linux System Administration
45
Scheduling Processes at command crontab command
Define one or more commands to be executed at some future time Command relies on background process called atd crontab command Define one or more commands to be executed repeatedly at intervals that you designate Relies on background process called crond The Complete Guide to Linux System Administration
46
Automating One-Time Tasks
Using at command enter commands Directly at command line at now + 5 hours Or listed in file at -f <filename> <time specification> atd daemon checks once per minute for any jobs that scheduled using at command Output of commands ed to user The Complete Guide to Linux System Administration
47
Automating One-Time Tasks (continued)
The Complete Guide to Linux System Administration
48
Automating One-Time Tasks (continued)
tty command Sends output from command to terminal in which you are currently logged in batch command Similar to at Runs commands when system load average drops below 0.8 The Complete Guide to Linux System Administration
49
Automating Recurring Tasks
Use crontab command To execute commands at regular intervals Red Hat Linux /etc directory contains subdirectories named cron.Hourly, cron.Daily, cron.Weekly, and cron.Monthly Place file subdirectory Commands in file executed hourly, daily, weekly, or monthly The Complete Guide to Linux System Administration
50
Uses of crontab The Complete Guide to Linux System Administration
You might use crontab when you need to regularly: • Create a list of files that have not been accessed in a long time to check whether they can be deleted or archived. • Create a backup copy of all active files (those recently accessed). • Compile a list of all directories on the system, sorted by size, to help you identify areas that are using a lot of hard disk space. • Remove core files or other unused files that are using a lot of hard disk space. • Delete files in the /tmp directory that have not been used recently. • Rotate log files to keep them from becoming too large . • Run security scanning software (for example, check attachnlents for viruses or search system log files for multiple login failures). • Store the results of the ps or df command to make a snapshot of the system's state at different times The Complete Guide to Linux System Administration
51
Automating Recurring Tasks (continued)
Every Linux system should include /etc/crontab file Illustrates format of standard entry for crontab command crontab specification begins with five fields that identify time All five fields must match current time and date for command to execute Cannot enter cron job commands interactively The Complete Guide to Linux System Administration
52
crontab command specification
The /etc/crontab file in Red Hat Linux and Fedora includes numbers and asterisks that define when the commands on that line (run-parts for most of /etc /crontab) are executed. Each crontab specification begins with five fields, described here as they appear on each cromab line from left to right (see also Figure 6-7): • Minute of the hour: This field can range from 0 to 59. • Hour of the day: This field can range from 0 to 23. • Day of the month: This field can range from 0 to 31, but be careful about using the days 29, 30, or 31: nothing will happen in the month of February; and nothing will happen in February, April,June, September, or November if you use 31. • Month of the year: This field can range from 0 to 12. You can also use the first three letters of a month's name (in upper- or lowercase). Day of the week: This field can range from 0 to 7 (0 and 7 are both Sunday; 1 is Monday). You can also use the first three letters of a day's name (in upper- or lowercase). The Complete Guide to Linux System Administration
53
The crond daemon examines all five of these fields once per minute.
If all five fields match the current time, crond executes the command on that line. Using an asterisk in a field means "execute this comnand no matter what the current value of this field is." For the command to be executed, all five fields must match the current time and date. For example, based on the following time specification, crond runs the command on this line at 10 minutes after every hour, every day. The specification is not limited by certain days of the week or month. 10 * * * * du /home > /tmp/disk_usage The Complete Guide to Linux System Administration
54
Cont. If you do not include a user name after the time specification, crond executes the command as whichever user submits the crond job. Only root should bother including a user name, as regular users can't submit a job Based on the following time specification, crond runs the command on this line on the first day of every month at 2:15 in the morning: * * du /home > /tmp/disk_usageb to be run as another user. Any field can contain multiple values, either separated by commas (without spaces after the commas) or defined by ranges with a hyphen separating them. For example, this time specification causes crond to execute the command on this line on the 1st, 10th, and 20th of every month at 1 :00 a.m.: o 1 1,10,20 * * du /home > /tmp/disk_usage The Complete Guide to Linux System Administration
55
for example, you wanted to run the du command on /home and sort the results every morning at 1 :00 a.m. You do this by following these steps: 1. Create a text file containing the time specification and the command you want to execute. For this example, the file contains the following line (you need to submit this cron job as root so that the du command can access all home directories): o 1 * * * du /home I sort > /root/disk_usage 2. Submit the file to the crond daemon using the crontab command. If the file you created in Step 1 were called du_nightly, the command would look like this: # crontab du_nightly Notice that the command shown in Step 1 fits on a single line. The Complete Guide to Linux System Administration
56
Cont. The Complete Guide to Linux System Administration for example,
that you had developed a series of shell scripts that check the security of the Linux system by reviewing log files, checking network activity, and watching user login activity. If the script that performed all of these activities were called secure_system, you could run that script every morning at 1 :30 a.m. by adding this line to your du_nightly file before submitting the file using the crontab command: 30 1 * * * /sbin/secure_system The Complete Guide to Linux System Administration
57
In the / etc/ crontab file shown previously, you saw environment variables at the beginning of the file. You can also include environment variables in crontab specification files that you create. For example, suppose you want the du_nightly cron job to be executed via the root user account so that it can access all home directories, but you want an message sent to your regular user account so you won't have to log in as root to read the message. To do this, you can include a MAILTO variable at the top of the du_nightly file. The du_nightly file now looks like this: MAILTO=nwells o 1 * * * du /home I sort > /root/disk_usage 30 1 * * * /sbin/secure_system crontab -u jtaylor du_nightly The Complete Guide to Linux System Administration
58
Managing Automated Tasks
Daemons atd and crond started when Linux booted Commands submit using at or crontab stored in subdirectory of /var/spool atq and atrm commands Manage at jobs awaiting execution atq command lists all queued at jobs The Complete Guide to Linux System Administration
59
atq command The Complete Guide to Linux System Administration
The atq command lists all queued at jobs awaiting execution by atd. For each job, this command lists a job number and the date and time when the job will be executed: # atq ,00 a root ,15 a root ,45 P root You can also use the at command with the -I option (for list) to see the same output as the atq command. at –l same as atq The Complete Guide to Linux System Administration
60
Managing Automated Tasks (continued)
atrm command Cancel command submitted using at Can also use -d option with at command Must include job number You obtain the job number using atq or at -l. For example, to remove job 11, you can use either of these commands: # atrm 11 # at -d 11 Use similar set of commands to manage cron jobs Options to crontab command The Complete Guide to Linux System Administration
61
For example, when you have submitted a single cron job, the file looks something like this: # DO NOT EDIT THIS FILE - edit the master and reinstall. # (secure_cron installed on Mon Oct 20 18,12, ) # (Cron version - $Id, crontab.c,v /01/17 03 ,20,37 # vixie Exp $) 30 1 * * * /sbin/security_scan The Complete Guide to Linux System Administration
62
Cont. The Complete Guide to Linux System Administration
As with the contents of the /var/spool/at directory, you should not directly edit a crontab file in /var/spool/cron. Instead, use the options provided by the crontab command, as summarized here: • crontab -I lists cron jobs for your user account . • crontab –r removes your crontab flie. Use this option carefully, as it removes the entire file; any cron jobs you have submitted are lost. crontab -e opens your crontab file in a text editor, so you can make changes in the times or commands defined for your cron jobs. It's important that you use the –e option on the crontab command rather than using a regular editor session (such as vi /var/spool/cron/jtaylor) to change your crontab file. Using crontab -e prevents file-locking conflicts that can cause problems with the crond daemon. The Complete Guide to Linux System Administration
63
Controlling Access to at and crontab
Default settings allow any user to submit commands using either at or crontab Can restrict access to only certain users The Complete Guide to Linux System Administration
64
Cont. The Complete Guide to Linux System Administration
The files that enforce this control are: • / etc/cron.allow: Contains user names (one per line) that are allowed to use the crontab command • / etc/ cron.deny: Contains user names (one per line) that are not allowed to use the crontab command • / etc/at.allow: Contains user names (one per line) that are allowed to use the at commnand • /etc/at. deny: Contains user names (one per line) that are not allowed to use the at command On some Linux systems, the files listed above might be located in the /var/ spool/cron and /var/spool/at subdirectories, in which case they are named simply allow and deny. The Complete Guide to Linux System Administration
65
Cont. The Complete Guide to Linux System Administration
On most Linux systems, none of these four files exists, meaning that any user can use both at and crontab. (On some systems, however, having none of these files can mean that only root can use at and crontab.) When you attempt to use the at or crontab command, the command checks the permission files in the following order: • If the /etcl/cron.allow file exists, a user must be listed in that file to use the crontab command. The same rule applies to /etc/at.allow for the at command. • If the cron.allow (or at.allow) file does not exist, but the cron.deny file does exist, any user listed in cron.deny (or at. deny) cannot use the cronab command (or the at command). By controlling access to the at and crontab commands on a busy Linux system, you can make it more difficult for regular users to consume system resources when you need to do system administration work or schedule business-critical tasks. The Complete Guide to Linux System Administration
66
Summary Linux is a multiprocessing operating system
New processes are created by forking existing process ps command displays many different fields of information about each process kill command sends signals to processes or to Linux kernel to end process The Complete Guide to Linux System Administration
67
Summary (continued) Each process in Linux assigned priority
Called nice level top command lists processes according to how much CPU time they are using free and vmstat commands display information about RAM and virtual memory usage The Complete Guide to Linux System Administration
68
Summary (continued) at command schedules task for execution by atd at future point batch command accepts tasks for future execution without fixed time crontab command schedules tasks to be executed on regular basis The Complete Guide to Linux System Administration
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.