实验:管理进程及计划任务
实验环境
为了更好地了解和控制Linux服务器的有序运行,需要管理员熟悉进程管理和计划任务 设置的相关操作,以完成各种运行维护任务。
需求描述
- 管理系统中的进程。
- 使用kill命令终止postfix服务的运行。
- 查找系统中CPU占用率超过80%的进程,并强行终止该进程。
- 设置计划运行的系统管理任务。
- 每周一的早上7:50自动清空FTP服务器公共目录“/var/ftp/pub”中的数据。
- 每天晚上的10:30自动执行任务,完成以下操作:显示当前的系统时间并查看 已挂载磁盘分区的磁盘使用情况,将输出结果追加保存到文件/var/log/df.log 中,以便持续观察硬盘空间的变化。
- 确认所设置的计划任务列表。
使用kill命令终止postfix服务的运行。
查看postfixpid
[root@test2 jason]# service postfix status
master (pid 6314) is running...
终止postfix进程
[root@test2 jason]# kill 6314
终止后查看postfix状态
[root@test2 jason]# service postfix status
master dead but pid file exists
查找系统中CPU占用率超过80%的进程,并强行终止该进程。
这里借助cpuburn-in来提升cpu占用率
运行
[root@test2 Desktop]# ./cpuburn-in 1
CPU Burn-in v1.00 - Linux
-------------------------
Michal Mienik (c) 2000. [email protected]
0 iterations complete.
5000 iterations complete.
10000 iterations complete.
15000 iterations complete.
20000 iterations complete.
25000 iterations complete.
30000 iterations complete.
35000 iterations complete.
40000 iterations complete.
... //中间过程省略
480000 iterations complete.
485000 iterations complete.
490000 iterations complete.
Test Complete. No errors were found after running 493240 tests.
[root@test2 Desktop]#
通过top查看

kill

检查crond的状态:
[root@test2 init.d]# /etc/init.d/crond status
crond (pid 2841) is running...
[root@test2 init.d]#
crond已经启动
每周一的早上7:50自动清空FTP服务器公共目录“/var/ftp/pub”中的数据。
[root@test2 init.d]# crontab -e
no crontab for root - using an empty one
50 7 * * 1 /bin/rm -rf /var/ftp/pub/*
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
:wq
检查计划是否设置成功:
[root@test2 init.d]# crontab -l
50 7 * * 1 /bin/rm -rf /var/ftp/pub/*
[root@test2 init.d]#
已经设置成功
每天晚上的10:30自动执行任务,完成以下操作:显示当前的系统时间并查看已挂载磁盘分区的磁盘使用情况,将输出结果追加保存到文件/var/log/df.log 中,以便持续观察硬盘空间的变化。
[root@test2 ~]# crontab -e
50 7 * * 1 /bin/rm -rf /var/ftp/pub/*
30 22 /1 * * /bin/date >> /var/log/df.log
30 22 /1 * * /bin/mount >> /var/log/df.log
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
:wq
退出来后:
crontab: installing new crontab
"/tmp/crontab.TnDEbR":2: bad day-of-month
errors in crontab file, can't install.
Do you want to retry the same edit?
Enter Y or N
Do you want to retry the same edit? y
50 7 * * 1 /bin/rm -rf /var/ftp/pub/*
30 22 */1 * * /bin/date >> /var/log/df.log
30 22 */1 * * /bin/mount >> /var/log/df.log
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
:wq
这里面提示出现错误,是因为每日的格式是“*/1”,而不是"/1".重新修改格式。然后确认。
确认所设置的计划任务列表:
[root@test2 ~]# crontab -l
50 7 * * 1 /bin/rm -rf /var/ftp/pub/*
30 22 */1 * * /bin/date >> /var/log/df.log
30 22 */1 * * /bin/mount >> /var/log/df.log
[root@test2 ~]#
验证date和mount计划:
[root@test2 log]# ls -lh | grep df
-rw-r--r-- 1 root root 779 Jun 23 22:30 df.log
[root@test2 log]#
[root@test2 log]# cat /var/log/df.log
Thu Jun 23 22:30:01 CST 2016
/dev/mapper/vg_jason-lv_root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
/dev/sdc1 on /home type ext4 (rw)
/dev/md1 on /mdata2 type ext4 (rw)
/dev/mapper/mail_store-mbox on /mailbox type ext4 (rw,usrquota,grpquota)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
vmware-vmblock on /var/run/vmblock-fuse type fuse.vmware-vmblock (rw,nosuid,nodev,default_permissions,allow_other)
/dev/sr0 on /media/CentOS_6.5_Final type iso9660 (ro,nosuid,nodev,uhelper=udisks,uid=507,gid=507,iocharset=utf8,mode=0400,dmode=0500)
gvfs-fuse-daemon on /root/.gvfs type fuse.gvfs-fuse-daemon (rw,nosuid,nodev)
[root@test2 log]#
截图:

推荐步骤
管理系统中的进程
- postfix服务的运行状态,通过ps或pgrep命令查看postfix服务的进程信息。 使用kill命令终止postfix服务的运行,再次确认服务状态。
- 使用CPU测试工具cpuburn-in.tar.gz,从另一个终端执行“cpuburn-in 10”命令, 进行10分钟的CPU高负载测试。使用ps或top命令查找到CPU占用率较高的进程的PID 号,并终止其运行。
- 练习各种进程管理命令的使用。
设置计划运行的系统管理任务
- 确认启动crond系统服务。
- 执行“crontab -e”并根据描述的需求设置计划任务(提示:向文件中追加信息可使 用重定向符号“>>”)b
- 执行“crontab -1”查看所设置的计划任务列表。
需求描述
- 管理系统中的进程。
- 使用kill命令终止postfix服务的运行。
- 查找系统中CPU占用率超过80%的进程,并强行终止该进程。
- 参照理论讲解部分中的相关示例,练习各种进程管理命令的使用。
- 设置计划运行的系统管理任务。
- 每周一的早上7:50自动清空FTP服务器公共目录“/var/ftp/pub”中的数据。
- 每天晚上的10:30自动执行任务,完成以下操作:显示当前的系统时间并查看 已挂载磁盘分区的磁盘使用情况,将输出结果追加保存到文件/var/log/df.log 中,以便持续观察硬盘空间的变化。
- 确认所设置的计划任务列表。