crond服務(wù) Linux一般自帶安裝cron
安裝crontab:
查看服務(wù)狀態(tài)
/sbin/service crond status
yum install crontabs服務(wù)操作說(shuō)明:
/sbin/service crond start //啟動(dòng)服務(wù)
/sbin/service crond stop //關(guān)閉服務(wù)
/sbin/service crond restart //重啟服務(wù)
/sbin/service crond reload //重新載入配置查看crontab服務(wù)是否已設(shè)置為開(kāi)機(jī)啟動(dòng),執(zhí)行命令:
ntsysv
加入開(kāi)機(jī)自動(dòng)啟動(dòng):
chkconfig –level 35 crond on
Linux按天執(zhí)行SH腳本 方式一
- 編輯當(dāng)前用戶定時(shí)執(zhí)行腳本
crontab -e (注意:編輯腳本存在/var/spool/cron/用戶名)
加入腳本: 04 2 * * * /usr/local/bin/tomcatcron.sh - 創(chuàng)建sh執(zhí)行腳本
cd /usr/local/bin //腳本目錄,可隨意
vi tomcatcron.sh //執(zhí)行的腳本
加入腳本如,執(zhí)行方式二logrotate計(jì)劃:
#! /bin/sh
/usr/sbin/logrotate -f /etc/logrotate.conf
執(zhí)行腳本日志備份策略 方式二
- 在/etc/logrotate.d/ 目錄下新建一個(gè)文件,命名隨意。
cd /etc/logrotate.d/
vi tomcat
/usr/local/apache-tomcat-8.5.20/logs/catalina.out{
copytruncate
daily
rotate 15
dateext
compress
missingok
size 5M
lastaction
mv /usr/local/apache-tomcat-8.5.20/logs/catalina.out-*.gz /data1/tomcat_log
endscript
}
- 查看定時(shí)任務(wù)執(zhí)行日志
cat /var/log/cron - log執(zhí)行記錄,控制執(zhí)行次數(shù):
cat /var/lib/logrotate.status
Linux按天執(zhí)行目錄
- /etc/crontab文件包括下面幾行:
cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=HOME=/
# run-parts
51 * * * * root run-parts /etc/cron.hourly
04 2 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
- 按天執(zhí)行腳本
cat /etc/cron.daily/logrotate
#!/bin/sh
/usr/sbin/logrotate /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
/usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0
報(bào)錯(cuò)1
查看cron運(yùn)行日志
tail -f /var/log/cron
root) FAILED to authorize user with PAM (Module is unknown)
解決方法:
- 禁用SELinux,可以使用getenforce命令查看是否禁用。
- 修改/etc/cron.allow,添加
root
myuser - 修改/etc/pam.d/crond,把把所有required改成sufficient,這個(gè)可能對(duì)非root用戶管用。
# The PAM configuration file for the cron daemon
#
#
# No PAM authentication called, auth modules not needed
account required pam_access.so
account include password-auth
session required pam_loginuid.so
session include password-auth
auth include password-auth
- 重啟crond服務(wù)
/etc/init.d/crond restart
logrotate.conf配置講解
see "man logrotate" for details //可以查看幫助文檔
weekly //設(shè)置每周轉(zhuǎn)儲(chǔ)一次(daily、weekly、monthly當(dāng)然可以使用這些參數(shù)每天、星期,月 )
rotate 4 //最多轉(zhuǎn)儲(chǔ)4次
create //當(dāng)轉(zhuǎn)儲(chǔ)后文件不存在時(shí)創(chuàng)建它
compress //通過(guò)gzip壓縮方式轉(zhuǎn)儲(chǔ)(nocompress可以不壓縮)
include /etc/logrotate.d //其他日志文件的轉(zhuǎn)儲(chǔ)方式配置文件,包含在該目錄下
/var/log/wtmp { //設(shè)置/var/log/wtmp日志文件的轉(zhuǎn)儲(chǔ)參數(shù)
monthly //每月轉(zhuǎn)儲(chǔ)
create 0664 root utmp //轉(zhuǎn)儲(chǔ)后文件不存在時(shí)創(chuàng)建它,文件所有者為root,所屬組為utmp,對(duì)應(yīng)的權(quán)限為0664
rotate 1 //最多轉(zhuǎn)儲(chǔ)一次
cron使用者權(quán)限
- 文件:/etc/cron.deny
說(shuō)明:該文件中所列用戶不允許使用crontab命令 - 文件:/etc/cron.allow
說(shuō)明:該文件中所列用戶允許使用crontab命令 - 文件:/var/spool/cron/
說(shuō)明:所有用戶crontab文件存放的目錄,以用戶名命名
注意事項(xiàng):
時(shí)間記錄:/var/spool/anacron下的文件
執(zhí)行不重復(fù)狀態(tài)記錄:
使root用戶的crontab生效 crontab -u root /var/spool/cron/root
(1)、04 2 * * * root run-parts /etc/cron.daily后要空一行,
(2)、新創(chuàng)建的cron job,不會(huì)馬上執(zhí)行,至少要過(guò)2分鐘才執(zhí)行。如果重啟cron則馬上執(zhí)行。
當(dāng)crontab突然失效時(shí),可以嘗試/etc/init.d/crond restart解決問(wèn)題。或者查看日志看某個(gè)job有沒(méi)有執(zhí)行/報(bào)錯(cuò)tail -f /var/log/cron。
千萬(wàn)別亂運(yùn)行crontab -r。它從Crontab目錄(/var/spool/cron)中刪除用戶的Crontab文件。刪除了該用戶的所有crontab都沒(méi)了。
在crontab中%是有特殊含義的,表示換行的意思。如果要用的話必須進(jìn)行轉(zhuǎn)義%,如經(jīng)常用的date ‘+%Y%m%d’在crontab里是不會(huì)執(zhí)行的,應(yīng)該換成date ‘+%Y%m%d’。