1. 程式人生 > >linux任務計劃cron、chkconfig工具、systemd管理服務、unit介紹、target介紹

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

linux基礎

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



本文出自 “探索發現新事物” 博客,請務必保留此出處http://shenj.blog.51cto.com/5802843/1978674

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