歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux 上常見的例行性工作

Linux 上常見的例行性工作

日期:2017/2/28 15:32:08   编辑:Linux教程

一 、僅執行一次的工作調度單一工作排程的運作,那就是 at 這個指令的運作!

1.atd 啟動與 at 運作的方式
[root@localhost ~]# /etc/init.d/atd restart
Stopping atd: [ OK ]
Starting atd: [ OK ]
2. 設置一下開機啟動這個服務
[root@localhost ~]# chkconfig atd on
3.實際運行單一工作調度
《1》再過5分鐘後,將/root/.basrc 寄給root自己
[root@localhost ~]# at now +5 minutes
at> /bin/mail root -s "testing at job" < /root/.bashrc
at> <EOT> ß這裡輸入 ctrl +d 就會出現<EOT> 的字樣.
job 7 at 2012-09-24 19:50
[root@localhost ~]# atq ß查詢目前主機上面有多少的at工件調度 如果關閉 atrm +工作號
6 2012-09-25 23:00 a root
7 2012-09-24 20:05 a root
[root@localhost ~]# at -c 6
#!/bin/sh
# atrun uid=0 gid=0
# mail root 0
《2》將上述的第5項工作內容列出來查閱
如果 在23:00停電 關機 命令如下
[root@localhost ~]# at 23:00 2012-9-25
at> /bin/sync
at> /bin/sync
at> /sbin/shutdown -h now
at> <EOT>
job 6 at 2012-09-25 23:00

事實上,當我們使用at時進入at shell 的環境讓用戶執行工作命令,你最好使用絕對路徑來執行
8 .batch : 系統有空時才進行後台任務 ,他會在 CPU 工作負載小亍 0.8 的時候,才運行你所下達的工作任務
[root@localhost ~]# batch 23:00 2012-9-25
at> sync
at> sync
at> shutdown -h now
at> <EOT> ß這裡輸入 ctrl +d 就會出現<EOT> 的字樣.
job 8 at 2012-09-25 23:00
[root@localhost ~]# atq
6 2012-09-25 23:00 a root
8 2012-09-25 23:00 a root

二、循環執行的例行性工作調度
用戶想要新建環形工作調度時,使用的是crontab 這個命令。不過,為了安全性的問題,與at類似,我們可以限制使用crontab 的用戶賬號。使用限制數據有。
1.用戶的設置
(1)將可以使用 crontab 的賬號寫入其中,若不在這個檔案內的使用者則不可使用/etc/cron.allow ;

vi /etc/cron.allow 比如我在裡面加入 root dg 這兩個用戶 注:每個用戶各占一行
[dg@localhost ~]$ crontab -e
no crontab for dg - using an empty one
crontab: installing new crontab
(2)將不可以使用 crontab 的賬號寫入其中,若未記錄到這個檔案當中的使用者,可以使用/etc/cron.deny
/etc/cron.deny 比如我在裡面加入 dg1
[root@localhost ~]# useradd dg1
[dg1@localhost ~]$ crontab -e
You (dg1) are not allowed to use this program (crontab)
See crontab(1) for more information
默認情況下只要不被列入/etc/cron.deny 當中就能直接執行crontab -e
(3)進入後輸入如下
0 12 * * * mail dmtsai -s "at 12:00"</home/dmtsai/.bashrc
分時 日月周 命令串
[root@localhost ~]# crontab –l 查詢 crontab –r 刪除
1 12 * * * mail dmtsai -s "at 12:00"< /home/dmtsai/.bashrc
假如每5分鐘需要執行 /home/dmtsai/test.sh
*/5 * * */home/dmtsai/test.sh
系統的配置文件 : /etc/crontab
[root@localhost ~]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# run-parts
分時日月周執行者 命令串
01 * * * * root run-parts /etc/cron.hourly <--每小時
02 4 * * * root run-parts /etc/cron.daily 《--每天
22 4 * * 0 root run-parts /etc/cron.weekly 《--每周
42 4 1 * * root run-parts /etc/cron.monthly《--每月

Copyright © Linux教程網 All Rights Reserved