實驗:管理進程及計劃任務
實驗環境
爲了更好地瞭解和控制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]#