比如備份數據或者重啟服務。
crontab -u、-e、-l、-r(刪除)
?格式:分?時?日?月?周?user command
?文件/var/spool/cron/username
?分范圍0-59,時范圍0-23,日范圍1-31,月范圍1-12,周1-7
?可用格式1-5表示一個范圍1到5
?可用格式1,2,3表示1或者2或者3
?可用格式*/2表示被2整除的數字,比如小時,那就是每隔2小時
?要保證服務是啟動狀態
?systemctl start crond.service
cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
# For details see man 4 crontabs
?
# Example of job definition:
# .---------------- minute (0 - 59)
# | ?.------------- hour (0 - 23)
# | ?| ?.---------- day of month (1 - 31)
# | ?| ?| ?.------- month (1 - 12) OR jan,feb,mar,apr ...
# | ?| ?| ?| ?.---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | ?| ?| ?| ?|
# * ?* ?* ?* ?* user-name ?command to be executed
crontab –e??//進入編輯模式
0 2 * * * /bin/bash /usr/local/sbin/123.sh >>/tmp/123.txt 2>>12.txt
#每天凌晨2點 執行腳本123.sh?把正確的輸入到123.txt,把錯誤的輸入到12.txt
?
0 2 1-4 */2 2,5 /bin/bash /usr/local/sbin/123.sh >>/tmp/123.txt 2>>12.txt
#每天凌晨2點,1到4號,2,4,6,8,10,12月(能被2整除的)周2和周5,一個區間用-。和逗號隔開??執行腳本?123.sh?把正確的輸入到123.txt,把錯誤的輸入到12.txt
任務計劃沒執行
1.可能腳本里可能只是一個命令,不是絕對路徑?//(因為不在crond的PATH里。cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin)。最好的辦法就是寫絕對路徑
2.追加一個日志,正確的輸出或者錯誤的輸出。保證有據可查
chkconfig服務管理工具
chkconfig --list??//查看系統使用chkcongfig服務有哪些
chkconfig network on/off ?//分別表示開機開機還是關閉
?chkconfig --level 3 network off??//關閉3級別
?chkconfig --level 345 network off??//關閉345級別
?chkconfig --del network?//刪除
?chkconfig --add network??//增加
chkconfig --he
自己添加一個服務
要把文件放到/etc/init.d目錄下
systemd服務管理
systemctl list-units --all --type=service?//所有的服務都列出(包括未激活的)幾個常用的服務相關的命令
systemctl enable crond.service //讓服務開機啟動
systemctl disable crond //不讓開機啟動
?systemctl status crond //查看狀態
?systemctl stop crond //停止服務
?systemctl start crond //啟動服務
?systemctl restart crond //重啟服務
?systemctl is-enabled crond //檢查服務是否開機啟動
unit介紹
ls /usr/lib/systemd/system //系統所有unit,分為以下類型
?service?系統服務
?target?多個unit組成的組
ls -l runlevel*
device?硬件設備
?mount?文件系統掛載點
?automount?自動掛載點
?path?文件或路徑
?scope?不是由systemd啟動的外部進程
?slice?進程組
?snapshot systemd快照
?socket?進程間通信套接字
?swap ?swap文件
?timer?定時器
unit相關的命令
?systemctl list-units //列出正在運行的unit
?systemctl list-units --all //列出所有,包括失敗的或者inactive的
?systemctl list-units --all --state=inactive //列出inactive的unit
?systemctl list-units --type=service//列出狀態為active的service
?systemctl is-active crond.service //查看某個服務是否為active
target介紹
系統為了方便管理用target來管理unit
?systemctl list-unit-files --type=target?//列出所有的target
?systemctl list-dependencies multi-user.target //查看指定target下面有哪些unit
?systemctl get-default //查看系統默認的target
?systemctl set-default multi-user.target
?一個service屬于一種類型的unit
?多個unit組成了一個target
?一個target里面包含了多個service
?cat /usr/lib/systemd/system/sshd.service //看這個service屬于哪個unit。看[install]部分
本文轉自 蝦米的春天 51CTO博客,原文鏈接:http://blog.51cto.com/lsxme/2047099,如需轉載請自行聯系原作者