You are on page 1of 13

Lab 5 Service & Process Management

1. Overview

At any given moment, there are anywhere from dozens to thousands of process

running on a Unix system. The large majority of these processes, called daemons, run

in the background. Daemons are crucial to having a usable system and provide much

of a system’s core functionality, including the graphics server, sound server, and

networking services to name a few.

Today we’ll explore these background processes and create some of our own! Make

sure the following exercises are executed on your VM unless explicitly specified

otherwise.

在任何给定的时刻,Unix 系统上都有几十到几千个进程在运行。这些进程中的大部分
称为守护进程,在后台运行。守护进程对于拥有一个可用的系统至关重要,它提供了
系统的许多核心功能,包括图形服务器、声音服务器和网络服务等等。
今天我们将探索这些背景过程,并创建一些自己的!除非另有明确说明,否则请确保
在 VM 上执行以下练习。

2. Tutorial

**********************Which processes are running on my system?*************


1. Open up a terminal and run the ps command to check out what processes are

running. You should see something like this: PID TTY TIME CMD 3634 pts/2

00:00:00 ps 15015 pts/2 00:00:00 bash

2. 1.打开终端,运行 ps 命令,查看正在运行的进程。你应该看到这样的东西:

PID TTY TIME CMD 3634 pts/2 00:00:00 ps 15015 pts/2 00:00:00 bash
3. ps
4.
5. ps -aux

6. Now open up another terminal and run sleep 1000 in the background, you

should get an output like “[1] 29756”, which gives the process id. Then run

ps,in the same terminal.

7. 6.现在打开另一个终端,在后台运行 sleep 1000,您应该得到一个类似

“[1]29756”的输出,它给出了进程 id,然后在同一个终端上运行 ps。


8. sleep 1000 &
9. ps

10. In the first terminal run ps again. You should notice that the sleep process is

not showing up, even though the thousand seconds haven’t expired. (Exercise

1) Why do you think this behavior occurs (hint: TTY column)?

11. 10.在第一个终端中再次运行 ps。你应该注意到睡眠过程没有出现,即使千秒还

没有结束。(练习 1)为什么会发生这种行为(提示:TTY 列)?

12. We can get the process to display on the first terminal by running ps -u, which

displays all the processes running as your user. Notice the PID column; each

process has a unique ID assigned to it by the kernel. One thing we can do with

this PID is send signals to the process. “sleep 1000” is pretty useless, so go
ahead and kill it – kill 29756 (substitute 29756 with whatever PID ps outputted

for you).

13. 12.我们可以通过运行 ps-u 让进程显示在第一个终端上,它显示所有以用户身

份运行的进程。注意 PID 列;每个进程都有一个由内核分配给它的唯一 ID。我

们可以用这个 PID 做的一件事是向进程发送信号。“sleep 1000”是非常无用的,

所以继续杀掉它-杀掉 29756(用输出给你的任何 PID ps 替换 29756)。


14. Kill <pid of the process “sleep 1000”>

15. The most common use of ps is to run ps -ef to see all the processes running

on the system. Run ps -e and ps -f independently to see how the flags work

together.

16. 15.ps 最常见的用途是运行 ps-ef 来查看系统上运行的所有进程。独立运行 ps-e

和 ps-f 以查看标志如何协同工作。
17. ps –e
18. ps -f

****************htop: another command for viewing process *************

19. Make sure htop is installed by running sudo apt install htop.

20. 19.通过运行 sudo apt install htop 确保安装了 htop。


21. sudo apt install htop

22. Now, open up a terminal and run the htop command. htop can be thought of

as a more extensive version of ps -ef, whereby process stats are updated in

real-time.

23. 22.现在,打开一个终端并运行 htop 命令。htop 可以看作是 ps-ef 的一个更广


泛的版本,通过它可以实时更新进程统计信息。
24. htop

25. First press <F2>, scroll down to Display options, and check “Hide userland

process threads.” We won’t be dealing with those in this lab.

26. 首先按<F2>,向下滚动显示选项,并选中“隐藏用户和进程线程”。我们将不
会在这个实验室处理这些线程。
27. Now open up another terminal. Run the command yes. It uses a lot of

resources as it prints a continuous stream of y’s. (Exercise 2) What resource

specifically does the yes command exhaust? If you are having trouble

finding this, press < to choose which resource to order processes by. Make

sure to quit out of yes (^C) once you are finished.

28. 27.现在打开另一个终端。运行命令 yes。它在打印连续的 y 流时使用了大量资

源(练习 2)yes 命令具体耗尽了哪些资源?如果找不到,请按<选择要按哪个

资源订购流程。完成后,请确保退出“是”(^C)。

29. yes

30. ^C

**************************The process hierarchy*******************************

31. Run htop once more. This time click <F5> to enter Tree View. You should see a

visual representation of the process hierarchy on your system, with everything

stemming from /sbin/init (systemd).

32. 31.再次运行 htop。这次单击<F5>进入树状图。您应该可以看到系统上进程层

次结构的可视化表示,所有内容都源于/sbin/init(systemd)。
************************Orphan processes***********************************

33.Open a second terminal. Now run sleep 1000 &. You should see this new

process pop into your htop session on your first terminal. If not, press <F3> and

search for “sleep.” (Exercise 3) What is its parent?

34.33.打开第二个终端。现在运行 sleep 1000&amp;。您应该在第一个终端上看

到这个新进程弹出到您的 htop 会话中。如果没有,按<F3>并搜索“sleep”。

(练习 3)它的父对象是什么?

35.

36. Select this parent and press <F9> to kill it. Send the SIGTERM signal. The sleep

process now has init as its new parent, which is PID 1. What you just did is

manually orphan a process; when that happens said process is subsequently

re-parented by the init process.

37. 35.选择此父级并按<F9>将其杀死。发送信号。睡眠进程现在将 init 作为其新父

进程,即 PID 1。您刚才所做的是手动孤立一个进程;当发生这种情况时,所述

进程随后被 init 进程重新设置父级。

38. Now go through the same steps again. This time, send the parent

a SIGHUP (hangup) signal. Can you still find the sleep process? When SIGHUP is

sent to a parent shell, the parent subsequently sends hangup signals to any

child processes before terminating; all processes that receive SIGHUP from a


parent shell will terminate – this is one way to avoid creating orphan

processes.

39. 37.现在再重复同样的步骤。这一次,给家长发一个叹息(挂机)信号。你还能

找到睡眠过程吗?当 SIGHUP 发送到父 shell 时,父 shell 随后在终止之前向任

何子进程发送挂起信号;从父 shell 接收 SIGHUP 的所有进程都将终止-这是避


免创建孤立进程的一种方法。
If you are interested in learning about the different signals, run man 7 signal.

40. 如果你有兴趣了解不同的信号

41. man 7 signal

****************Cron: scheduling processes automatically*********************

42. Open the cron editor by running crontab -e (if the editor of your choice isn’t

being launched, set the EDITOR environment variable), which will create a

crontab for your user. Below is a sample task. Put this in your crontab:

43. 41.通过运行 crontab-e 打开 cron 编辑器(如果没有启动您选择的编辑器,请设

置 editor 环境变量),这将为您的用户创建 crontab。下面是一个示例任务。

把这个放到你的 crontab 中:

* * * * * date +"\%T" >> $HOME/timestamps.txt

28. Right now this runs every minute. Modify it to run every five minutes and

quit out of the editor. Make sure to leave this running while you complete the
remainder of the lab as you will be turning this file in. If you get stuck, visit

crontab.guru!

28 现在每分钟一次。修改为每五分钟运行一次并退出编辑器。请确保在完成实
验室的其余部分时保持此运行,因为您将要提交此文件。如果你被困住了,请
访问 crontab.guru!

**********************************Job Control**********************************

44. Download the file job.sh from https://chanpaul.github.io/Project/job.sh.

45. wget https://chanpaul.github.io/Project/job.sh

46. Open 2 terminals. In one panel, change to the directory including the file

job.sh, and run ./job.sh > ~/count. Then, in the other panel, run less +F

~/count. You should see the less command increments every half a second or

so. Now run Ctrl+Z in the panel with the job.sh command. This will suspend

the process. (Exercise 5) What happens when you suspend the job

command?

47.45.打开 2 个端子。在一个面板中,切换到包含文件 job.sh 的目录,然后运

行./job.sh>~/count。然后,在另一个面板中,运行更少的+F~/count。您应

该会看到 less 命令每半秒左右递增一次。现在使用 job.sh 命令在面板中运行

Ctrl+Z。这将暂停进程。(练习 5)暂停作业命令时会发生什么情况?

48. ./job.sh > ~/count //in the first terminal

49. less +F ~/count //in the second terminal

50. Ctrl+Z //in the first terminal


51. Now, let’s resume our suspended process. Since we only have one job, we can

just run bg. (Exercise 6) What happens after running the bg command?

52. 50.现在,让我们继续我们暂停的程序。既然我们只有一份工作,我们就可以运

行 bg。(练习 6)运行 bg 命令后会发生什么?

53. bg

54. Now let’s bring our job to the foreground. First run jobs -l. You should see

both the job number (in brackets) and the pid of the job. We’ll bring the job to

the foreground by running fg %i (where i is the number that showed up in the

brackets when you ran jobs -l) and kill it using Ctrl+C.

55. 53.现在让我们把工作摆在前台。首先运行 jobs-l。您应该同时看到作业编号

(在括号中)和作业的 pid。我们将通过运行 fg%i(其中 i 是运行 jobs-l 时括号

中显示的数字)将作业置于前台,并使用 Ctrl+C 将其终止。

56. jobs –l

57. fg %i ///here, you should replace i with real number

*******************************Install from source*******************************

Installing Percona SQL Server from Source, an enterprise version of MySQL

从源安装 Percona SQL Server,MySQL 的企业版

https://www.percona.com/downloads/Percona-Server-LATEST/
58. Download the Source

59. curl -O https://www.percona.com/downloads/Percona-Server-

LATEST/Percona-Server-5.7.18-14/source/tarball/percona-server-5.7.18-

14.tar.gz

60. Unpackage percona-server-5.7.18-14.tar.gz

61. tar -xzvf percona-server-5.7.18-14.tar.gz

62. Change Directories and review the INSTALL and README files

63. cd percona-server-5.7.18-14

64. more INSTALL

65. more README

Following the "Compiling Percona Server from Source" Instructions

按照“从源代码处编译 Percona 服务器”的说明

66. Configure the Software:

65.配置软件:

67. sudo cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo

-DBUILD_CONFIG=mysql_release -DFEATURE_SET=community

-DWITH_EMBEDDED_SERVER=OFF

68. Compile (Build) the Software:

69. make
70. Install the Software:

71. make install

72. Try to start Percona SQL Server

73. service mysql start

3. Exercise

(1) Answer the question in Part 2 (Tutorial) the 8th problem. “Why do you

think this behavior occurs (hint: TTY column)”

(2) (1) 回答第 2 部分(教程)中的问题第 8 个问题。“为什么会发生这种

行为(提示:TTY 列)”

(3) 每个终端只会显示到链接到当前的终端的进程。所以看不到,要见到这个

sleep 要用 ps -e

(2) Answer the question in Part 2 (Tutorial) the 19 th problem. (Exercise 2) What

resource specifically does the yes command exhaust?

(2) 回答第 2 部分(教程)第 19 题中的问题。(练习 2)yes 命令具体耗尽了哪些


资源?虚拟内存值物理内存值共享内存值 cpu
(3) Answer the question in Part 2 (Tutorial) the 23rd problem (Exercise 3) What is
its parent?
(3) 回答第 2 部分(教程)中的问题第 23 个问题(练习 3)它的父对象是什么?

(4) Answer the question in Part 2 (Tutorial) the 28th problem, (Exercise 5) What

happens when you suspend the job command?


(4) 回答第 2 部分(教程)第 28 个问题中的问题,(练习 5)挂起作业命令时会发生什么情况?
(5) What command is used to prepare the system for power off?
(5) 使用什么命令来准备系统断电?

(6) Name 2 commands to reboot the system.

(6) 命名 2 个重新启动系统的命令。

(7) How would you command the system to shutdown completely in 20 minutes, and to
display a message to all users saying “going down for system maintenance”?

(7) 如何命令系统在 20 分钟内完全关闭,并向所有用户显示一条消息,说明“要停机


进行系统维护”?(8) What program starts init? What is the PID of init?

(9) What runlevel are you currently at?

(https://www.ostechnix.com/check-runlevel-linux/)

你目前的运行级别是什么?

(10) what command would switch you to runlevel 3?( https://www.cyberciti.biz/tips/linux-


changing-run-levels.html)

什么命令会将您切换到运行级别 3?

(11)What is the primary difference between runlevels 3 and 5?

运行级别 3 和 5 之间的主要区别是什么?

(12) What kind of file is /etc/rc.d/rc.local?

什么样的文件是/etc/rc.d/rc.local?

(13) (optional) What link in the /etc/rc.d/rc5.d directory ensures that rc.local is run?

(可选)在/etc/rc.d/rc5.d 目录中的哪个链接确保 rc.local 运行?

(14) Use chkconfig to prevent ip6tables from starting for runlevel 5. What is the


syntax of the command you used? (https://www.tecmint.com/chkconfig-command-examples/)

使用 chkconfig 防止运行级别 5 的 ip6tables 启动。您使用的命令的语法是什么?


(15) How would you use chkconfig and grep to show only services which are 'on' at
some runlevel?

(15) 如何使用 chkconfig 和 grep 只显示在某个运行级别上“打开”的服务?

(16) What does the number following the K or S in a filename in /etc/rc.d/rc3.d/ indicate?

16) 文件名/etc/rc.d/rc3.d/中 K 或 S 后面的数字表示什么?

(17) What would the syntax be for a cronjob entry as follows:

(17) cronjob 条目的语法如下:

[1] run the script /root/ping.bash every day at 3:45 AM?

[1] 每天凌晨 3:45 运行脚本/root/ping.bash?

[2] run the script /root/ping.bash every day at 4 PM?

[2] 每天下午 4 点运行 script/root/ping.bash?

[3] running /root/ping.bash on every Monday, and Tuesday at noon

[3] 每周一运行/root/ping.bash,周二中午运行

[4] running /root/ping.bash on every Monday, Thursday and Saturday, at 9:00 PM

[4] 每周一、周四和周六晚上 9:00 运行/root/ping.bash

[5] running /root/ping.bash on the first day of every month, at 4 AM

[5] 每月的第一天,凌晨 4 点运行/root/ping.bash

[7] running /root/ping.bash on every thursday in May at 4 AM

[7] 运行/root/ping.bash,在 5 月的每个星期四凌晨 4 点

[8] running /root/ping.bash every day from the 5th to the 12th of every month, at 4 AM

[8] 每月 5 日至 12 日凌晨 4 点,每天跑步/root/ping.bash

[9] running /root/ping.bash on any Monday, Tuesday or Wednesday between the 2 nd


and 15th day of the month, at 2:30 PM:

[9] 运行/root/ping.bash,在每月 2 日至 15 日的任何星期一、星期二或星期三下午


2:30:
(18) What set of commands will run /root/ping.bash when the load average drops below
1.5?

(18) 当平均负载低于 1.5 时,将运行/root/ping.bash 的命令集是什么?

(19) Install all available software updates for your system. What command did you use?

(19) 为您的系统安装所有可用的软件更新。你用了什么命令?

You might also like