Lab 11 Process Control

Sequence 1: Job Control

1. [student@stationX ~]$ su -

2. Begin some jobs in the background:
[root@stationX ~]# tail -n0 -f /var/log/messages &
[root@stationX ~]# updatedb &

3. [root@stationX ~]# service syslog restart

4. [root@stationX ~]# jobs

5. [root@stationX ~]# kill %1
[root@stationX ~]# jobs

6. Next, start an instance of vim
[root@stationX ~]# vim

7. While in vim, press Ctrl-z to suspend the current program.

8. Run jobs again and note vim's job ID.

9. [root@stationX ~]# fg 3

Sequence 2: Conditional Execution

1. ping -c1 -w2 $TARGET &> /dev/null

2. vi ~/bin/reach.sh
#!/bin/bash
TARGET=$1
ping -c1 -w2 $TARGET &> /dev/null

3. [student@stationX ~]$ chmod a+x ~/bin/reach.sh

4. [student@stationX ~]$ reach.sh server1; echo $?
0
[student@stationX ~]$ reach.sh station100; echo $?
1

5. Now use the conditional operators && and || to report success or failure based on ping's
exit value.
#!/bin/bash
TARGET=$1
ping -c1 -w2 $TARGET &> /dev/null &&
echo "$TARGET is UP" ||
echo "$TARGET is DOWN"

6. [student@stationX ~]$ reach.sh server1; echo $?
server1 is UP
0
[student@stationX ~]$ reach.sh station100; echo $?
station100 is DOWN
0

7. The script returns 0 for both tests because the last command run is now echo, not ping.
Since echo does not fail, it returns success.

8. Modify the script again
#!/bin/bash
TARGET=$1
ping -c1 -w2 $TARGET &> /dev/null
RESULT=$?
if [ $RESULT -ne 0 ]
then
echo "$TARGET is DOWN"
else
echo "$TARGET is UP"
fi
exit $RESULT

9. Test the script.
[student@stationX ~]$ reach.sh server1; echo $?
server1 is UP
0
[student@stationX ~]$ reach.sh station100; echo $?
station100 is DOWN
1

10. [student@stationX ~]$ sudo mv ~/bin/reach.sh /usr/local/bin/

Sequence 3: Scheduling One-Time Jobs

Scenario: In this sequence you will schedule a job for one-time execution at a specified
time using the tool you developed in the previous sequence.

Instructions:

1. Start in a shell, either on a virtual console or a graphical shell, such as gnome-terminal. You should be signed in as student.

2. Schedule your reach.sh tool to check all stations five minutes from now:
[student@stationX ~]$ at now+5min
at> for x in $(seq 1 40); do
at> reach.sh station$x
at> done
at> Ctrl-d
job 7 at 2007-01-23 08:40

Note: Since your script only emits output when there is a problem, you do not have to worry
about redirecting STDOUT or STDERR in regular usage. Your job will only notify you of
unreachable stations!

3. The system responded with a job number, but list your scheduled jobs to see any others that
you may have created (or that root may have created for you!):

[student@stationX ~]$ at -l
job 7 at 2007-01-23 08:40 a student

4. For detailed information, cat the job:

[student@stationX ~]$ at -c 7

Read the output, observing that each at job stores the environment for the user that created
the job. The job's command is listed near the bottom of the output.

5. Optionally, watch the job list until your job executes.

[student@stationX ~]$ watch -n1 'at -l'

6. Check your mail after the job executes to review its output.

[student@stationX ~]$ mutt

Sequence 4: Finding Processes

Scenario: In this sequence you will find the process on your system that is using the most
CPU time. Finding it will require the use of ps and tail.

Instructions:

1. Start in a shell, either on a virtual console or a graphical shell, such as gnome-terminal. You should be signed in as student.

2. Review the ps man page to find the standard format specifier for displaying the PID,
program name, and percent CPU utilization.

man ps

Within the man page, search for the -o option:

/-o
Press n until you find the section on the -o option. Instead of listing the available columns,
it refers you to the STANDARD FORMAT SPECIFIERS section of the man page, so try
searching for that at this point:

/STANDARD FORMAT SPECIFIERS

Find the list of columns to determine which codes are appropriate.

3. List all processes on your system, limiting output to PID, program name, and percent CPU
utilization.

ps axo pid,comm,pcpu

You should see a long list of processes scroll by.

4. Now open up the man page for ps to determine if it has the ability to sort output.

man ps
/sort

5. Once you find the correct sort option, add it to your previous command:

ps axo pid,comm,pcpu --sort=pcpu

6. The output looks right, so now pipe it through another command to restrict output to a
single process:

ps axo pid,comm,pcpu --sort=pcpu | tail -n1

Sequence 5: Recurring Jobs

Scenario: In this sequence you will take the command you developed in the previous sequence and adapt it for use in a recurring job. You would like the output mailed to student's email complete with column headings.

Instructions:

1. Observing that ps automatically outputs column headings, review the man page to
determine how to reverse the sort order, such that the job with the most CPU time is at the
top of the ouput, along with the column headings.

Add the reverse sort indicator (-) in front of the sort column:

ps axo pid,comm,pcpu --sort=-pcpu

2. Now restrict output to the top two lines:

ps axo pid,comm,pcpu --sort=-pcpu | head -n2

The output is now suitable for job scheduling.

3. Review the man page for crontab to check the field order:

man crontab

4. Oops! There are two man pages for crontab, so open up the one in section 5, which deals
with configuration files:

man 5 crontab

5. Use information in the man page to determine how would you write a crontab entry that
should run every five minutes?

*/5 * * * *

6. Use your answer to the previous question to add a crontab entry that runs the ps command
from earlier every five minutes.

You can do this either by running crontab -e and using a text editor or by piping directly to
crontab like this:

echo '*/5 * * * * ps axo pid,comm,pcpu --sort=-pcpu | head -n2' | crontab

7. Once you have added the job, list your crontab to confirm:

crontab -l

8. Now add lines to run your reach.sh command on server1 and station100 every two minutes.
Because /usr/local/bin is not in the PATH used by cron, you will need to use an
absolute path to the script.

Your crontab should now look like this:

*/5 * * * * ps axo pid,comm,pcpu --sort=-pcpu | head -n2
*/2 * * * * /usr/local/bin/reach.sh server1
*/2 * * * * /usr/local/bin/reach.sh station100

9. Once a few minutes have passed, check your mail with the mutt command to see the output
of your jobs. See the instructions at the beginning of this lab if you are unfamiliar with
mutt.

Some observations about what you should see:

• You did not receive any mail regarding the reachability of server1; your script correctly
avoids output on successful completion

• You received at least one message regarding the failure to reach station100

10. For cleanup, remove your crontab:

crontab -r; crontab -l

RH033读书笔记(10)-Lab 11 Process Control的更多相关文章

  1. RH033读书笔记(9)-Lab 10 Understanding the Configuration Tools

    Lab 10 Understanding the Configuration Tools Sequence 1: Configuring the Network with system-config- ...

  2. RH033读书笔记(15)-Lab 16 The Linux Filesystem

    Lab 16 The Linux Filesystem Goal: Develop a better understanding of Linux filesystem essentials incl ...

  3. RH033读书笔记(16)-Lab 17 Installation and Administration Tools

    Lab 17 Installation and Administration Tools Goal: Become familiar with system configuration tools a ...

  4. RH033读书笔记(5)-Lab 6 Exploring the Bash Shell

    Lab 6 Exploring the Bash Shell Sequence 1: Directory and file organization 1. Log in as user student ...

  5. RH033读书笔记(3)-Lab 4 Browsing the Filesystem

    Lab 4 Browsing the Filesystem Sequence 1: Directory and File Organization 1. Log in as user student ...

  6. RH033读书笔记(7)-Lab 8 Introduction to String Processing

    Lab 8 Introduction to String Processing Sequence 1: Exercises in string processing 1. Other than the ...

  7. RH033读书笔记(8)-Lab 9 Using vim

    Lab 9 Using vim Sequence 1: Navigating with vim 1. Log in as user student 2. [student@stationX ~]$ c ...

  8. RH033读书笔记(13)-Lab 14 Network Clients

    Goal: Practice using a variety of tools to transfer files between your system and a remote system. S ...

  9. RH033读书笔记(14)-Lab 15 Switching Users and Setting a Umask

    Lab 15 Switching Users and Setting a Umask Goal: Become familiar with the use of several essential c ...

随机推荐

  1. python语言学习6——python基础

    Python是一种计算机编程语言. 以#开头的语句是注释,注释是给人看的,可以是任意内容 其他每一行都是一个语句,当语句以冒号:结尾时,缩进的语句视为代码块. Python程序是大小写敏感的,如果写错 ...

  2. KMP算法的Next数组详解(转)

    转载请注明来源,并包含相关链接. 网上有很多讲解KMP算法的博客,我就不浪费时间再写一份了.直接推荐一个当初我入门时看的博客吧: http://www.cnblogs.com/yjiyjige/p/3 ...

  3. http_load安装与测试参数分析 - 追求自由自在的编程 - ITeye技术网站

    http_load安装与测试参数分析 - 追求自由自在的编程 - ITeye技术网站 http_load -p 50 -s 120 urls

  4. android圆角View实现及不同版本号这间的兼容

    在做我们自己的APP的时候.为了让APP看起来更加的好看,我们就须要将我们的自己的View做成圆角的,毕竟主流也是将非常多东西做成圆角.和苹果的外观看起来差点儿相同,看起来也还不错. 要将一个View ...

  5. WIFI实时监控追踪小车演示视频——安卓端、小车

    文章由@超人爱因斯坦出品,转载请注明出处.         文章链接:          http://hpw123.net/a/qingsongyike/yingyinqu/2014/1019/59 ...

  6. Android中TweenAnimation四种动画切换效果

    点击每个按钮都会有对应的动画显示 activity代码: package com.tmacsky; import android.app.Activity; import android.os.Bun ...

  7. C++-传值与传引用的差别

    //值传递与引用传递的差别 #include <iostream> #include <iomanip> using namespace std; void fiddle(in ...

  8. Path相关评论的方法(一)

    以前的主要是关于Canvas的translate(平移) .scale(缩放) .rotate(旋转) .skew(错切).接下来几篇主要讲下android里的Path(封装了贝塞尔曲线)&  ...

  9. Linux日志清除

    因为数据要求.经常需要抓住和筛选过滤数据,大概花了7 8个月.改变了机旁数据.重新开始,发现"No space left on device" 解决方法: 直接删除日志(简单粗暴) ...

  10. Linux使用快捷键,who命令,rm命令,ps命令,cd,命令kill命令,find命令,grep命令,tar命令(gz、tar、bz2),用户管理,vim配置的一部分,相关命令

    1.进入Ubuntu开场后的终端窗口的快捷键是:           ctrl + alt+t:通过这个命令能够打开终端. ctrl + alt+t:通过这个命令能够打开终端. 再开一个tab选项卡式 ...