Unit 3.2
Manage processes

Presenter Notes

本节幻灯片


Unit objectives

After completing this unit, you should be able to:

  • Explain the purpose of a shell
  • Describe the relationship between parent and child processes
  • Monitor the processes
  • Start foreground and background processes
  • Explain the concept of signals and use them to terminate processes
  • Explain the concept of priorities and manage them
  • Use crontab files to schedule jobs on a periodic basis
  • Use the at command to schedule a job or series of jobs at some time in the future

The shell

  • The shell is the command line user interface to the kernel.
    • configruable
    • command interpreter
    • program environment
  • Available shells:
    • bash
    • ksh
    • csh

Presenter Notes

Shell是系统的用户界面,提供了用户与内核进行交互操作的一种接口。它接收用户输入的命令并把它送入内核去执行。 实际上Shell是一个命令解释器,它解释由用户输入的命令并且把它们送到内核。不仅如此,Shell有自己的编程语言用于对命令的编辑,它允许用户编写由shell命令组成的程序。Shell编程语言具有普通编程语言的很多特点,比如它也有循环结构和分支控制结构等,用这种编程语言编写的Shell程序与其他应用程序具有同样的效果。


The process

  • A program is an executable file.
  • A process is a program that is being executed.
  • Each process has its own environment.

  • The unique identifier of a process is PID.
  • To see the PID of your current shell process, type:
    $ echo $$

Presenter Notes

进程是程序的一个具体实现。进程是执行程序的过程,同一个程序可以执行多次,每次都可以在内存中开辟独立的空间来装载,从而产生多个进程。不同的进程还可以拥有各自独立的IO接口。


Starting and stopping a process

  • All processes are started by other processes.
  • This is called a parent/child relationship.
    $ ls –l

  • Here, bash is the parent, ls is the child.
  • A process can be terminated for two reasons.
    • The process terminates itself when done.
    • The process is terminated by a signal from another process.

Presenter Notes

操作系统需要有一种方式来确定所有必须的进程都存在。在通用的系统中有四种原因导致创建进程: 1)系统初始化 2)执行了从事创建进程的一个系统周期,该系统调用被正在运行的进程所调用 3)用户请求创建一个进程 4)一个批处理作业的初始化 上一节已经说明进程终止方式。多数进程是由于完成了它们的工作而终止。当编译器完成了所给定程序的编译之后,编译器执行一个系统调用,通知操作系统它的工作已经完成。


Parents and children

$ echo $$  # parent shell's pid
$ bash

### in child shell ###
$ echo $$  # child shell's pid
$ date
$ exit
### return to parent shell ###

$ echo $$  # parent shell's pid

Presenter Notes

而在Linux系统中,只能一个系统调用可以用来创建新进程,使用fork()函数来创建一个进程。在调用了fork()后,这两个进程拥有相同的存储映像、同样的环境字符串和同样的打开文件。进程创建之后,父进程和子进程都有各自不同的地址空间。如果其中某个进程在其地址空间修改一个字,这个修改对其他进程而言是不可见的。


Monitoring processes

  • The ps command displays process status information.

  • ps supports a large number of options; you typically use :
    ps aux
    a: All processes attached to a terminal
    x: All other processes
    u: Provides more columns

  • Another common usage for ps:
    ps -ef

Presenter Notes

监控系统的各方面的性能,保障各类服务的有序运行,是运维工作的重要组成部分,下面是一些常用的系统监控命令。 具体包含以下几个部分: 1、进程管理基础 2、进程管理类工具(ps、top、htop、kill) 3、内存监控类工具(vmstat、pmap) 4、系统监控类工具(glances、dstat)


Foreground and background processes

  • Foreground processes

    • Foreground processes are invoked by simply typing a command at the command line.
      $ find / -name README
  • Background processes

    • Background processes are invoked by putting an ampersand (&) at the end of the command line.
      $ find / -name README &

Presenter Notes

& 放在启动参数后面表示设置此进程为后台进程 默认情况下,进程是前台进程,这时就把Shell给占据了,我们无法进行其他操作,对于那些没有交互的进程,很多时候,我们希望将其在后台启动,可以在启动参数的时候加一个'&'实现这个目的。


Controlling processes

  • Processes can be controlled in two ways.
    • From the shell that started it, using its job number
    • From anywhere on the system, using its PID
  • The following actions can be performed on a running process:
    • Terminate
    • Kill
    • Stop/continue
  • These actions are performed by sending signals.

Job control in the shell

jobs: Lists background or suspended jobs

For foreground:
<ctrl-z>: Suspends foreground task
<ctrl-c>: Terminate this task

For background:
fg: Put the task to the foreground

For suspended:
fg: Resumes suspended task in the foreground
bg: Resumes suspended task in the background

For all:
kill: Terminate or kill the task.

Specify a job number for bg, fg, and kill using %job

Presenter Notes

在自己的bash中,如果有多个工作需要处理,就需要进行job control,把其中一些工作可以放进背景中。 直接将指令放到背景中执行 :& 将目前的工作放到背景中暂停:ctrl+z 观察目前背景工作状态:jobs [-l (job number,PID) -r (run) -s (stop)] 将背景工作拿到前景来处理:fg %jobnumber 让工作在背景下的状态变成运行状态:bg %jobnumber 管理北京当中的工作:kill -signal %jobnumber 查看signal使用kill -l 脱机管理问题:以上的工作,是与终端有关的。


Jobs

Presenter Notes

Job的几种切换状态、


Killing processes

  • Several signals can be sent to a process.
    • Using keyboard interrupts (if foreground process)
    • Using the kill command
      • Synopsis: kill -signal PID
    • Using the killall command to kill all named apps
      • Synopsis: killall -signal application

Presenter Notes

kill格式为kill -signal %jobnumber 注意:这里的kill也可以结束进程,区别在于如果不加%直接接一个数字,那么毁直接kill掉PID为该数字的进程 signal代表一个信号,通常进程之间可以互相控制的,比如你可以关闭,或者重新启动软件。那么程序是如何互相管理的呢? 其实就是通过给予该进程一个信号(signal)去告知进程让它做什么。 'kill %1'中的'%'告诉系统,后面的数字是一个job number。默认情况下,kill将会向程序发送一个termination signal(-TERM)。如果这个信号不起作用,考虑使用'kill -kill %job number'来发送一个kill signal(-KILL)。 'kill'命令的使用,就看man把,info也可以。常用的也就'kill %job number'、'kill -kill %job number'、'kill [-kill] PID'。


nohup

The nohup command stops a process from being killed if you log off from the system before it completes by intercepting and ignoring the SIGHUP and SIGINTR (hangup and interrupt) signals and redirecting STDOUT and STDERR to a file.

$ nohup find / -name README &
nohup: appending output to `nohup.out´
$ logout

Presenter Notes

使用&命令后,作业被提交到后台运行,当前控制台没有被占用,但是一但把当前控制台关掉(退出帐户时),作业就会停止运行。nohup命令可以在你退出帐户之后继续运行相应的进程。nohup就是不挂起的意思( no hang up)


nice and renice

  • Processes are scheduled according to priority.

    • Nice value : -20 ~ 19 , default : 0
    • Bigger value means lower priority.
    • Negative values are allowed only for root!
  • The nice command is used to start a process with a user-defined priority.
    nice [-n <value>] <original command>

  • The renice command is used to change the priority of a currently running process.
    renice <new_value> <PID>

Presenter Notes

  • 内核根据进程的nice值决定进程需要多少处理器时间. nice值的取值范围是是: -20到20. 一个具有-20 的 nice 值的进程有很高的优先级. 一个 nice 值为 20 的进程的优先级则很低.用 ps axl 显示所有正在运行的进程的 nice 值
  • renice 可以给正在运行的进程设置调度优先级.

Daemon

The word daemon refers to a never-ending process, usually a system process, that controls a system resource, such as the printer queue, or performs a network service.

Presenter Notes

先简单介绍下是服务(service)。Linux系统可以提供很多系统或网络功能(例如http,ftp,mysql等),提供这些功能当然需要运行一些程序,这些运行的程序我们称为进程,也就是说系统中运行的进程提供了功能,这些进程就是服务。例如:提供了http功能的进程就是http服务。 服务可以说是某一进程,而进程是需要程序去运行产生,也就是说程序运行提供某种服务,这些程序就称为daemon。简单地说,daemon是静态的,就是某一程序,daemon运行后会提供某种服务,服务是动态的,是程序运行产生的进程。但通常情况下我们不需要是详细区分daemon和服务,可以将两者理解为等同,即daemon就是服务。(通常说的Linux守护进程就是指daemon或者服务)


Scheduling automates routine tasks

  • Scheduling allows you to run commands at a specific moment in the future.
  • The crond daemon performs the scheduling for the crontab files.
  • The atd daemon is responsible for execution of jobs submitted by the at and batch command.

Presenter Notes

有些工作需要周而复始的工作,有些任务需要在某个特定的时间启动工作,这就可以通过crontab来进行设置


Cron

  • Cron is used for repeating tasks (jobs).
  • Jobs are configured by adding them to a crontab file. A crontab entry has the following syntax:
    [minute] [hour] [day] [month] [weekday] [command]
    0 8 * * * Once_a_day
    0,30 9 * * * Twice_a_day
    0,30 8-18 * * * Twenty_Two_times_a_day
    */5 * * * * Every_five_minutes
    12 13 1 * * Once_a_month
    49 23 16 9 * Once_a_year
    0 15 * * 1 Every_monday
    1 1 1 1 1 ??? (caveat!)
  • Three usage methods:
    crontab -l : List your crontab file
    crontab -r : Remove your crontab file
    crontab -e : Edit your crontab file using $EDITOR

Presenter Notes

cron是一个linux下的定时执行工具,可以在无需人工干预的情况下运行作业。 cron 是Linux的内置服务,但它不自动起来,可以用以下的方法启动、关闭这个服务: /sbin/service crond status //查看服务当前状态 /sbin/service crond start //启动服务 /sbin/service crond stop //关闭服务 /sbin/service crond restart //重启服务 /sbin/service crond reload //重新载入配置

你也可以将这个服务在系统启动的时候自动启动: 在/etc/rc.d/rc.local这个脚本的末尾加上:/sbin/service crond start 现在cron这个服务已经在进程里面了,我们就可以用这个服务了.


At

  • At runs a command once in the future

    # at 4am
    ps aux
    ^d (CTRL+D)

    # at -f bshfile 16:00 + 3 days
    # echo "mail -s report < rep.txt boss" | at now +2min

  • To control at jobs, you can also use atq (or at -l) to list all jobs and atrm (or at -r) to delete a given job.

Presenter Notes

at命令用于在指定时间执行命令。at允许使用一套相当复杂的指定时间的方法。它能够接受在当天的hh:mm(小时:分钟)式的时间指定。假如该时间已过去,那么就放在第二天执行。当然也能够使用midnight(深夜),noon(中午),teatime(饮茶时间,一般是下午4点)等比较模糊的 词语来指定时间。用户还能够采用12小时计时制,即在时间后面加上AM(上午)或PM(下午)来说明是上午还是下午。也能够指定命令执行的具体日期,指定格式为month day(月 日)或mm/dd/yy(月/日/年)或dd.mm.yy(日.月.年)。指定的日期必须跟在指定时间的后面


Unit review

  • All processes are started by a parent process (except for init, which is started by the kernel).
  • Every process is identified with a process identifier (PID).
  • A special process is the shell, which can interpret user commands.
  • Processes can terminate by themselves or upon reception of a signal.
  • Signals can be sent by the shell, using a keyboard sequence, or by the kill and killall commands.
  • Processes are started with equal priority, but this can be changed using the nice and renice commands.
  • A daemon is a background process that typically controls a system resource or offers a network service.
  • Use crontab files to schedule jobs on a periodic basis
  • Use the at command to schedule a job or series of jobs at some time in the future

References

  • Unit 9: Working with processes, Linux Basics and Installation , ERC 7.2, IBM
  • Unit 12: Scheduling , Linux System Administratio, ERC 7.2, IBM

results matching ""

    No results matching ""