10.23 Linux任务计划

crontab -u、-e、-l、-r
格式:分 时 日 月 周 user command
文件  /var/spool/cron/username
分范围 0-59;时范围 0-23;日范围 0-31;月范围 0-12;周范围1-6
可用格式 1-5 表示一个范围1到5
可用格式 1,2,3 表示1或2或3
可用格式 */2 表示被2整除的数字,比如小时,那就是每隔2小时
要保证服务是启动状态
systemctl start crond.service


	[root@aming-01 ~]# 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


crontab -e  编辑任务
crontab -l  列出任务
crontab -u root -l  列出指定用户任务

10.24 chkconfig 工具

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


		[root@aming-01 ~]# 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:关

10.25 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-enable crond   //检查服务是否启动


		[root@aming-01 ~]# cat /etc/systemd/system/multi-user.target.wants/crond.service 
		[Unit]
		Description=Command Scheduler
		After=auditd.service systemd-user-sessions.service time-sync.target

		[Service]
		EnvironmentFile=/etc/sysconfig/crond
		ExecStart=/usr/sbin/crond -n $CRONDARGS
		ExecReload=/bin/kill -HUP $MAINPID
		KillMode=process

		[Install]
		WantedBy=multi-user.target


		[root@aming-01 ~]# ls -l /etc/systemd/system/multi-user.target.wants/crond.service 
		lrwxrwxrwx. 1 root root 37 2月  28 05:03 /etc/systemd/system/multi-user.target.wants/crond.service -> /usr/lib/systemd/system/crond.service

10.26 unit 介绍

ls /usr/lib/systemd/system //系统所有 unit,分为以下类型

service  系统服务
target   多个unit组成的组
device   硬件设备
mount    文件系统挂载点
automount  自动挂载点
path     文件或路径
scope    不是由systemd启动的外部进程
slice    进程组
snapshot systemd 快照
socket   进程间通信套接字
swap     swap 文件
timer    定时器


	[root@aming-01 system]# cd /usr/lib/systemd/system
	[root@aming-01 system]# ls -l runlevel*
	lrwxrwxrwx. 1 root root 15 3月   2 04:46 runlevel0.target -> poweroff.target
	lrwxrwxrwx. 1 root root 13 3月   2 04:46 runlevel1.target -> rescue.target
	lrwxrwxrwx. 1 root root 17 3月   2 04:46 runlevel2.target -> multi-user.target
	lrwxrwxrwx. 1 root root 17 3月   2 04:46 runlevel3.target -> multi-user.target
	lrwxrwxrwx. 1 root root 17 3月   2 04:46 runlevel4.target -> multi-user.target
	lrwxrwxrwx. 1 root root 16 3月   2 04:46 runlevel5.target -> graphical.target
	lrwxrwxrwx. 1 root root 13 3月   2 04:46 runlevel6.target -> reboot.target


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


	[root@aming-01 system]# systemctl is-active crond.service 
	active
	[root@aming-01 system]# systemctl is-enabled crond.service 
	enabled

10.27 target 介绍

系统为了方便管理用 target 来管理 unit
systemctl list-unit-files --type=target
systemctl list-dependencies multi-user.target  //查看指定 target 下面有哪些 unit
systemctl get-default   //查看系统默认的target
systemctl set-default multi-user.target   //设置系统默认 target
一个 service 属于一种类型的 unit
多个 unit 组成了一个 target
一个 target 里面包含了多个 service
cat /usr/lib/systemd/system/sshd.service   //看 [install] 部分


	[root@aming-01 system]# systemctl get-default
	multi-user.target
	[root@aming-01 system]# systemctl set-default multi-user.target 
	Removed symlink /etc/systemd/system/default.target.
	Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/multi-user.target.


	[root@aming-01 system]# cat /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