天天看點

linux任務計劃cron、chkconfig工具、systemd管理服務、unit介紹、target介紹

linux任務計劃cron

[root@test-7 ~]# crontab -e

no crontab for root - using an empty one

每天的淩晨3點 執行這個

0 3 * * * /usr/bin/touch /root/123.txt &>/dev/null

啟動服務

[root@test-7 ~]# systemctl start crond

基本格式 :

*  *  *  *  *  command

分 時 日 月 周 指令

第1清單示分鐘1~59 每分鐘用*或者 */1表示

第2清單示小時1~23(0表示0點)

第3清單示日期1~31

第4清單示月份1~12

第5列辨別号星期0~6(0表示星期天)

第6列要運作的指令

例子:

30 21 * * * /usr/local/etc/rc.d/lighttpd restart

上面的例子表示每晚的21:30重新開機apache。

45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart

上面的例子表示每月1、10、22日的4 : 45重新開機apache。

10 1 * * 6,0 /usr/local/etc/rc.d/lighttpd restart

上面的例子表示每周六、周日的1 : 10重新開機apache。

0,30 18-23 * * * /usr/local/etc/rc.d/lighttpd restart

上面的例子表示在每天18 : 00至23 : 00之間每隔30分鐘重新開機apache。

0 23 * * 6 /usr/local/etc/rc.d/lighttpd restart

上面的例子表示每星期六的11 : 00 pm重新開機apache。

* */1 * * * /usr/local/etc/rc.d/lighttpd restart

每一小時重新開機apache

* 23-7/1 * * * /usr/local/etc/rc.d/lighttpd restart

晚上11點到早上7點之間,每隔一小時重新開機apache

0 11 4 * mon-wed /usr/local/etc/rc.d/lighttpd restart

每月的4号與每周一到周三的11點重新開機apache

0 4 1 jan * /usr/local/etc/rc.d/lighttpd restart

一月一号的4點重新開機apache

chkconfig工具

[root@centos7 ~]# chkconfig --list

Note: This output shows SysV services only and does not include native

      systemd services. SysV configuration data might be overridden by native

      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.

      To see services enabled on particular target use

      'systemctl list-dependencies [target]'.

#表示centos6及以下版本用的是SysV,centos7用的是systemd。

netconsole      0:off 1:off 2:off 3:off 4:off 5:off 6:off

network         0:off 1:off 2:on 3:on 4:on 5:on 6:off

必要參數 

–add 開啟指定的服務程式 

–del 關閉指定的服務程式 

–list 列出chkconfig所知道的所有服務

[root@centos7 ~]# chkconfig --levels 235 network off #在235級别關閉

0:關機

1:單使用者模式

2,3,4:差不多,多使用者模式

5:圖形化界面

6:重新開機

chkconfig network on/off

systemd管理服務

CentOS 7 使用systemd替換了SysV。Systemd目的是要取代Unix時代以來一直在使用的init系統,相容SysV和LSB的啟動腳本,而且夠在程序啟動過程中更有效地引導加載服務。

列出所有可用單元 

[root@centos7 ~]# systemctl list-unit-files

列出所有運作中單元

systemctl list-units

列出所有失敗的單元

systemctl --failed

檢查某個單元是否啟用

systemctl is-enabled network.service

Executing /sbin/chkconfig network --level=5

enabled

檢視某個服務(單元)的狀态

[root@centos7 ~]# systemctl status network.service

 Active: active (exited)

# systemctl start httpd.service

# systemctl restart httpd.service

# systemctl stop httpd.service

# systemctl reload httpd.service

# systemctl status httpd.service

激活/禁止自動啟動

# systemctl enable httpd.service

# systemctl disable httpd.service

# systemctl kill httpd

unit介紹(單元)

unit的常見類型:

service unit:這類unit的檔案擴充名為.service,主要用于定義系統服務(其扮演了在centos6上/etc/init.d/目錄下的服務腳本的作用

target  unit:這類unit的檔案擴充名為.target,主要用于模拟實作"運作級别"的概念

device unit:這類unit檔案擴充名為.device,用于定義核心識别的裝置,然後udev利用systemd識别的硬體,完成建立裝置檔案名

mount unit:這類unit檔案擴充名為.mount,主要用于定義檔案系統挂載點

socket unit:這類unit檔案擴充名為.socket,用于辨別程序間通信用到的socket檔案

snapshot unit:這類unit檔案擴充名為.snapshot,主要用于實作管理系統快照

swap unit:這類unit檔案擴充名為.swap,主要用于辨別管理swap裝置

automount unit:這類unit檔案擴充名為.automount,主要用于檔案系統自動挂載裝置

path unit:這類unit檔案擴充名為.path,主要用于定義檔案系統中的檔案或目錄

systemctl list-units //列出正在運作的unit

systemctl list-units --all //列出所有,包括失敗的或者inactive的

systemctl list-units --all --state=inactive //列出狀态為inactive(列出所有沒有運作的)

systemctl list-units --type=service//列出狀态為active的service

systemctl is-active crond.service //檢視某個服務是否正則運作

target介紹

在systemd中有一個叫做target的單元,也叫作目标單元。這個單元沒有專用的配置選項,它隻是以.target結尾的檔案,它本身沒有具體功能,你可以了解為類别,它的作用就是将一些單元彙聚在一起。通過下面的指令可以檢視系統的target單元。

[root@centos7 ~]# cat /usr/lib/systemd/system/iptables.service

[Unit]

Description=IPv4 firewall with iptables

Before=ip6tables.service

After=syslog.target

AssertPathExists=/etc/sysconfig/iptables

[Service]

Type=oneshot

RemainAfterExit=yes

ExecStart=/usr/libexec/iptables/iptables.init start

ExecReload=/usr/libexec/iptables/iptables.init reload

ExecStop=/usr/libexec/iptables/iptables.init stop

Environment=BOOTUP=serial

Environment=CONSOLETYPE=serial

StandardOutput=syslog

StandardError=syslog

[Install]

WantedBy=basic.target

 systemctl list-dependencies multi-user.target //檢視指定target下面有哪些unit

systemctl get-default 

ncies multi-user.target //檢視指定target下面有哪些unit

 systemctl get-default //檢視系統預設的target

 systemctl set-default multi-user.target //設定系統的target

本文轉自方向對了,就不怕路遠了!51CTO部落格,原文連結:http://blog.51cto.com/jacksoner/1978674 ,如需轉載請自行聯系原作者

繼續閱讀