【CentOS 7筆記46】,crondtab任務計劃和chkconfig系統服務管理#

shallow丿ovebash


Linux任務計劃

  • crontab -u、-e、-l、-r
  • 格式:分 時 日 月 周 user command
  • 文件/var/spool/cron/username
  • 分範圍0-59,時範圍0-23,日範圍0-31,月範圍1-12,周0-6
  • 可用格式1-5表示一個範圍1到5
  • 可用格式1,2,3表示1或者2或者3
  • 可用格式*/2表示被2整除的數字,好比小時,那就是每隔2小時
  • 要保證服務是啓動狀態
  • systemctl start crond.service
[root@localhost ~]# 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 executed

天天凌晨3點鐘執行指定腳本,而且將正確重定向或錯誤的重定向到指定文件日誌裏dom

[root@localhost ~]# crontab -e
	0 3 * * * /bin/bash /usr/local/sbin/111.sh >> /tmp/123.log 2>> /tmp/123.log

每隔兩個月的頭10天,而且這十天內恰好爲週二和週五的時間執行指定腳本ide

[root@localhost ~]# crontab -e
	0 3 1-10 */2 2,5 /bin/bash /usr/local/sbin/111.sh >> /tmp/123.log 2>> /tmp/123.log

啓動服務日誌

[root@localhost ~]# systemctl start crond
[root@localhost ~]# systemctl start crond.service
[root@localhost ~]# ps aux | grep crond
	root        729  0.0  0.0 126344  1608 ?        Ss   Dec04   0:00 /usr/sbin/crond -n
	root       3014  0.0  0.0 112656   976 pts/0    S+   00:00   0:00 grep --color=auto crond

active (running)code

[root@localhost ~]# systemctl status crond
	● crond.service - Command Scheduler
	   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
	   Active: active (running) since Mon 2017-12-04 23:40:29 CST; 20min ago
	 Main PID: 729 (crond)
	   CGroup: /system.slice/crond.service
	           └─729 /usr/sbin/crond -n
	
	Dec 04 23:40:29 localhost.localdomain systemd[1]: Started Command Scheduler.
	Dec 04 23:40:29 localhost.localdomain systemd[1]: Starting Command Scheduler...
	Dec 04 23:40:29 localhost.localdomain crond[729]: (CRON) INFO (RANDOM_DELAY will be scaled wit....)
	Dec 04 23:40:29 localhost.localdomain crond[729]: (CRON) INFO (running with inotify support)
	Dec 05 00:01:01 localhost.localdomain crond[729]: (root) RELOAD (/var/spool/cron/root)
	Hint: Some lines were ellipsized, use -l to show in full.

inactive (dead)進程

[root@localhost ~]# systemctl stop crond
[root@localhost ~]# systemctl status crond
	● crond.service - Command Scheduler
	   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
	   Active: inactive (dead) since Tue 2017-12-05 00:02:09 CST; 1s ago
	  Process: 729 ExecStart=/usr/sbin/crond -n $CRONDARGS (code=exited, status=0/SUCCESS)
	 Main PID: 729 (code=exited, status=0/SUCCESS)
	
	Dec 04 23:40:29 localhost.localdomain systemd[1]: Started Command Scheduler.
	Dec 04 23:40:29 localhost.localdomain systemd[1]: Starting Command Scheduler...
	Dec 04 23:40:29 localhost.localdomain crond[729]: (CRON) INFO (RANDOM_DELAY will be scaled wit....)
	Dec 04 23:40:29 localhost.localdomain crond[729]: (CRON) INFO (running with inotify support)
	Dec 05 00:01:01 localhost.localdomain crond[729]: (root) RELOAD (/var/spool/cron/root)
	Dec 05 00:02:09 localhost.localdomain systemd[1]: Stopping Command Scheduler...
	Dec 05 00:02:09 localhost.localdomain systemd[1]: Stopped Command Scheduler.
	Hint: Some lines were ellipsized, use -l to show in full.

若沒有執行,頗有多是使用的命令而沒有用絕對路徑crontab

查看crondtabip

[root@localhost ~]# crontab -l
	0 3 1-10 */2 2,5 /bin/bash /usr/local/sbin/111.sh >> /tmp/123.log 2>> /tmp/123.log

對應用戶的crondtab執行腳本ci

[root@localhost ~]# cat /var/spool/cron/root 
	0 3 1-10 */2 2,5 /bin/bash /usr/local/sbin/111.sh >> /tmp/123.log 2>> /tmp/123.log

若要備份可直接備份/var/spool/cron/root的文件get

刪除crondtab

[root@localhost ~]# crontab -r

指定用戶

[root@localhost ~]# crondtab -u root -l

Linux系統服務管理-chkconfig

  • chkconfig --list
  • chkconfig --level 3 network off
  • chkconfig --level 345 network off
  • chkconfig --del network
  • chkconfig --add network

列出服務

[root@localhost ~]# chkconfig

	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]'.
	
	iprdump        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
	iprinit        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
	iprupdate      	0:off	1:off	2:on	3:on	4:on	5:on	6:off
	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

systemd服務的進程

[root@localhost ~]# top
	top - 02:52:51 up  3:12,  3 users,  load average: 0.00, 0.01, 0.05
	Tasks: 365 total,   1 running, 364 sleeping,   0 stopped,   0 zombie
	%Cpu(s):  0.0 us,  0.2 sy,  0.0 ni, 99.8 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
	KiB Mem:   1870784 total,   277448 used,  1593336 free,      692 buffers
	KiB Swap:  2097148 total,        0 used,  2097148 free.   100316 cached Mem

	   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                      
	   349 root      20   0       0      0      0 S   0.3  0.0   0:09.63 kworker/0:3                  
	     1 root      20   0   41236   3768   2432 S   0.0  0.2   0:01.46 systemd                      
	     2 root      20   0       0      0      0 S   0.0  0.0   0:00.06 kthreadd                     
	     3 root      20   0       0      0      0 S   0.0  0.0   0:00.06 ksoftirqd/0                  
	     5 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/0:0H                 
	     6 root      20   0       0      0      0 S   0.0  0.0   0:00.13 kworker/u256:0               
	     7 root      rt   0       0      0      0 S   0.0  0.0   0:00.03 migration/0                  
	     8 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcu_bh                       
	     9 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcuob/0                      
	    10 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcuob/1                      
	    11 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcuob/2                      
	    12 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcuob/3

服務腳本

[root@localhost ~]# ls /etc/init.d/
	functions  iprdump  iprinit  iprupdate  netconsole  network  README
[root@localhost ~]# chkconfig network off
[root@localhost ~]# 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]'.
	
	iprdump        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
	iprinit        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
	iprupdate      	0:off	1:off	2:on	3:on	4:on	5:on	6:off
	netconsole     	0:off	1:off	2:off	3:off	4:off	5:off	6:off
	network        	0:off	1:off	2:off	3:off	4:off	5:off	6:off
[root@localhost ~]# chkconfig network on
[root@localhost ~]# 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]'.
	
	iprdump        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
	iprinit        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
	iprupdate      	0:off	1:off	2:on	3:on	4:on	5:on	6:off
	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

而這個級別爲系統對應的七個級別

[root@localhost ~]# vi /etc/inittab
	# inittab is no longer used when using systemd.
	#
	# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
	#
	# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
	#
	# systemd uses 'targets' instead of runlevels. By default, there are two main targets:
	#
	# multi-user.target: analogous to runlevel 3
	# graphical.target: analogous to runlevel 5
	#
	# To view current default target, run:
	# systemctl get-default
	#
	# To set a default target, run:
	# systemctl set-default TARGET.target
	#
	~

指定級別

[root@localhost ~]# chkconfig --level 3 network off
[root@localhost ~]# 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]'.
	
	iprdump        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
	iprinit        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
	iprupdate      	0:off	1:off	2:on	3:on	4:on	5:on	6:off
	netconsole     	0:off	1:off	2:off	3:off	4:off	5:off	6:off
	network        	0:off	1:off	2:on	3:off	4:on	5:on	6:off
[root@localhost ~]# chkconfig --level 345 network on
[root@localhost ~]# 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]'.
	
	iprdump        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
	iprinit        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
	iprupdate      	0:off	1:off	2:on	3:on	4:on	5:on	6:off
	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

添加服務

[root@localhost ~]# cp /etc/init.d/network /etc/init.d/123
[root@localhost ~]# 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]'.

	iprdump        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
	iprinit        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
	iprupdate      	0:off	1:off	2:on	3:on	4:on	5:on	6:off
	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
[root@localhost ~]# chkconfig --add /etc/init.d/123
[root@localhost ~]# 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]'.
	
	123            	0:off	1:off	2:on	3:on	4:on	5:on	6:off
	iprdump        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
	iprinit        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
	iprupdate      	0:off	1:off	2:on	3:on	4:on	5:on	6:off
	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
[root@localhost ~]# vi /etc/init.d/123
	#! /bin/bash
	#
	# network       Bring up/down networking
	#
	# chkconfig: 2345 10 90
	# description: Activates/Deactivates all network interfaces configured to \
	#              start at boot time.
	#
	### BEGIN INIT INFO
	# Provides: $network
	# Should-Start: iptables ip6tables NetworkManager-wait-online NetworkManager $network-pre
	# Short-Description: Bring up/down networking
	# Description: Bring up/down networking
	### END INIT INFO
	
	# Source function library.
	. /etc/init.d/functions
	.
	.
	.

chkconfig: 2345 10 90 1234爲運行級別,第10位啓動,第90位關閉

關閉服務

[root@localhost ~]# chkconfig --del /etc/init.d/123
[root@localhost ~]# 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]'.
	
	iprdump        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
	iprinit        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
	iprupdate      	0:off	1:off	2:on	3:on	4:on	5:on	6:off
	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
相關文章
相關標籤/搜索