天天看點

任務計劃、chkconfig工具、systemd管理服務、unit、target

比如備份資料或者重新開機服務。

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

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,如需轉載請自行聯系原作者