任務計劃crontab,服務管理工具chkconfig,系統管理服務systemd

任務計劃

crontab計劃任務文件
任務計劃文件保存路徑/var/spool/cron/username(若是用戶是root,那麼打開的就是/var/spool/cron/root)
系統任務調度的配置文件:cat /etc/crontab
shell

[root@jinkai ~]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
.# 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 executedvim

[root@jinkai ~]#windows

*前四行是用來配置crond任務運行的環境變量
第一行SHELL變量指定了系統要使用哪一個shell,這裏是bash
第二行PATH變量指定了系統執行命令的路徑
第三行MAILTO變量指定了crond的任務執行信息將經過電子郵件發送給root用戶
若是MAILTO變量的值爲空,則表示不發送任務執行信息給用戶
第四行的HOME變量指定了在執行命令或者腳本時使用的主目錄。
星號(
):表明全部可能的值
逗號(,):能夠用逗號隔開的值指定一個列表範圍,例如,「1,2,5,7,8,9」
中槓(-):能夠用整數之間的中槓表示一個整數範圍,例如「2-6」表示「2,3,4,5,6」
正斜線(/):能夠用正斜線指定時間的間隔頻率,例如「0-23/2」表示每兩小時執行一次。
格式:分鐘 小時 日 月 周 user-name command
範圍:
分鐘:0-59;
小時:0-23;
日:0-31;
月:0-12;
周:0-6,0爲週日、或7爲週日;
User:若是不指定用戶,默認就是當前用戶**centos

安裝:bash

yum install -y crontabsssh

建立或編輯計劃任務socket

crontab -e ide

查看當前計劃任務:
crontab -l
[root@jinkai logrotate.d]# crontab -l
0 3 1-10 /2 2,5 /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log
1 10
2 /usr/bin/find /tmp/ -type f -mtime +100 |xargs rm -f
[root@jinkai logrotate.d]# cat /var/spool/cron/root
0 3 1-10
/2 2,5 /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log
1 10 2 /usr/bin/find /tmp/ -type f -mtime +100 |xargs rm -f工具

刪除定時任務
crontab -r

從/var/spool/cron目錄中刪除用戶的crontab文件,若是不指定用戶,則默認刪除當前用戶的crontab文件,加個-i 刪除時提示確認rest

[root@jinkai ~]# crontab -r -i
crontab: really delete root's crontab? n
[root@jinkai ~]#

備份crontab文件
crontab -l > $HOME/mycron

[root@jinkai ~]# echo $HOME
/root
[root@jinkai ~]# crontab -l > $HOME/a.txt
[root@jinkai ~]# cat /root/a.txt
20 1 * echo " " /var/log/slow.log

  • 3 0 /bin/sh /usr/local/sbin/backup.sh
    10 4 14 /bin/sh /usr/local/sbin/backup_month.sh
    0 /8 /usr/sbin/ntpdate time.windows.com
    0 1,12,18 /bin/sh /usr/local/sbin/test.sh
    0 9-18
    /bin/sh /usr/local/sbin/test2.sh

注意1:用命令能夠執行,用crontab沒法執行的腳本
1,使用絕對路徑
2,在shell腳本開頭使用一下代碼:
#!/bin/bash
. /etc/profile
. ~/bash_profile
3,能夠在/etc/crontab中添加環境變量,在能夠執行命令前添加
. /etc/profile;/bin/sh
注意2:當crontab忽然失效時,能夠嘗試/etc/init.d/crond restart解決問題。或者查看日誌看某個job有沒有執行報錯 tail -f /var/log/cron。
注意3:在crontab中%是有特殊含義的,表示換行。若是要用的話必須進行轉義\% ,如常常用的date ‘+%Y%m%d’在crontab裏是不會被執行的,應該換成date ‘+\%Y\%m\%d’

**啓動服務:

systemctl enable crond 開機啓動
systemctl disable crond 開機不啓動
systemctl stop crond 關閉
systemctl start crond 開啓
systemctl status crond 服務狀態
systemctl is-enabled crond 檢查服務是否設置開機啓動**

服務管理chkconfig工具

CentOS 6上的服務管理工具爲chkconfig,Linux系統全部的預設置服務均可以經過查看/etc/init.d目錄獲得;

[root@jinkai ~]# ls /etc/init.d/
functions netconsole network README

查看服務以及每一個級別的開啓狀態
chkconfig --list

[root@jinkai ~]# chkconfig --list

注:該輸出結果只顯示 SysV 服務,並不包含
原生 systemd 服務。SysV 配置數據
可能被原生 systemd 配置覆蓋。

要列出 systemd 服務,請執行 'systemctl list-unit-files'。
  查看在具體 target 啓用的服務請執行
  'systemctl list-dependencies [target]'。

netconsole 0:關 1:關 2:關 3:關 4:關 5:關 6:關
network 0:關 1:關 2:開 3:開 4:開 5:開 6:關
[root@jinkai ~]#

級別(0~6):爲系統啓動級別,0、一、6被系統保留。能夠參照init命令;
0:爲shutdown動做;
1:做爲重啓到單用戶模式;
6:爲重啓;
2:無NFS支持的多用戶模式;
3:徹底多用戶模式;
4:保留給用戶自定義;
5:圖形界面;

更改某個服務對應級別下的狀態:
例如:開啓或者關閉network下的3級別(還能夠省略級別,默認就是針對2,3,4,5操做的)

chkconfig --level 3 network off/on

增長自定義服務
chkconfig --add 服務名

添加一個服務到系統服務中,必須讓這個服務在/etc/init.d中

[root@jinkai ~]# cd /etc/init.d/
[root@jinkai init.d]# cp network 123
[root@jinkai init.d]# ls
123 functions netconsole network README
[root@jinkai init.d]# chkconfig --add 123
[root@jinkai init.d]# chkconfig --list
123 0:關 1:關 2:開 3:開 4:開 5:開 6:關
netconsole 0:關 1:關 2:關 3:關 4:關 5:關 6:關
network 0:關 1:關 2:開 3:開 4:開 5:開 6:關
[root@jinkai init.d]#

刪除服務
chkconfig --del 服務名

刪除123服務

[root@jinkai init.d]# chkconfig --del 123
[root@jinkai init.d]# chkconfig --list
netconsole 0:關 1:關 2:關 3:關 4:關 5:關 6:關
network 0:關 1:關 2:開 3:開 4:開 5:開 6:關
[root@jinkai init.d]#

systemd自啓動管理服務

用於CentOS 7系統下、CentOS 6系統經常使用chkconfig工具;
列出系統全部的服務

systemctl list-units --all --type=service

查看某個服務

systemctl list-unit-files |grep sshd

讓服務開機啓動

systemctl enable crond.service

服務對應的啓動腳本文件

/usr/lib/systemd/system

上面目錄中的文件能夠歸爲下面幾類:
service:系統服務;
target:多個unit組成的組;
device:硬件設備;
mount:系統掛載點;
automount:自動掛載點;
path:文件或路徑;
scope:不是由systemd啓動的外部進程;
slice:進程組;
snapshot:systemd快照;
socket:進程間通訊套接字;
swap:swap文件;
timer:定時器;
以上每種類型的文件都爲一個unit

unit相關

列出正在運行的unit

systemctl list-units

列出全部的unit

systemctl list-units --all

列出全部inactive的unit

systemctl list-units --all --state=inactive

列出全部狀態的service

systemctl list-units --all --type=service

列出狀態爲active的service

systemctl list-units --type=service

查看某個unit是否active

systemctl is-active crond.service

target

target相似於centos6裏面啓動級別,target支持多個target同時啓動。target實際上是多個unit的組合,系統啓動說白了就是啓動了多個unit

查看當前系統的全部target

systemctl list-unit-files --type=target

查看一個target包含的全部unit,下面以multi-user.target爲例

systemctl list-dependencies multi-user.target

查看系統默認的target

systemctl get-default

設置默認的target
例如設置默認target爲multi-user.target

systemctl set-default multi-user.target

一個service屬於一種類型的unit
多個unit組成一個target
一個target裏面包含了多個service

查看service服務文件
查看文件的install,定義了這個service屬於哪一個target

[root@jinkai init.d]# vim /usr/lib/systemd/system/sshd.service
[Unit]
Description=OpenSSH server daemon
Documentation=man:sshd(8) man:sshd_config(5)
After=network.target sshd-keygen.service
Wants=sshd-keygen.service

[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/sshd
ExecStart=/usr/sbin/sshd -D $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]WantedBy=multi-user.target

相關文章
相關標籤/搜索