您的位置:首页 > 运维架构 > Linux

日常运维(六):Linux系统的任务计划与系统服务管理

2017-11-01 11:27 1081 查看
主要内容:

1.linux任务计划cron

2.chkconfig工具

3.systemd管理服务

4.unit介绍

5.target介绍



1.linux任务计划cron

cron任务计划功能的操作都是通过crontab这个命令来完成的。其中常用的选项有:

-u :指定某个用户,不加-u选项则为当前用户;

-e :制定计划任务;

-l:列出计划任务;

-r :删除计划任务。

查看配置文件


可以看到crontab的环境变量PATH=/sbin:/bin:/usr/sbin:/usr/bin。

使用 crontab -e 来进行编写任务计划,实际上是使用vim工具打开了crontab的配置文件。

01 10 05 06 3 echo "ok" > /root/cron.log

每个字段的数字从左到右,依次为:分,时,日,月,周,命令行。

上例含义是:在6月5日(这一天必须是星期3)的10点01分执行命令 echo "ok" > /root/cron.log

crontab -e 实际上是打开了 "/var/spool/cron/username" (如果是root则打开的是/var/spool/cron/root)这个文件。但是,不要直接去编辑那个文件,因为可能会出错,所以要使用 crontab -e 来编辑。查看已经设定的任务计划使用 crontab  -l 命令。删除计划任务要用 crontab –r,这个选项最好不要用,因为它会一下子把全部计划都删除掉,如果你想只删除一条计划,可以使用-e选项进入crontab进行编辑。

开启crond:systemctl start crond



关闭用systemctl stop crond

如果编写了任务计划却没执行,可能是因为没有加绝对路径。

例如:vim /usr/local/sbin/iptables.sh

使用了ipt作为绝对路径,直接用iptables –F是不行的。



2.chkconfig服务管理工具

使用 chkconfig --list 列出所有的服务以及每个级别是否开启:



Note:该命令输出的内容并没有包含centos7的原生systemd服务,而这里仅仅列出来sysV服务,这也是/etc/init.d/目录下只有一两个启动脚本的根本原因,centos7之前采用的服务管理都是SysV,7换成了systemd。

级别(0,1,2,3,4,5,6)就是 /etc/inittab 里面的那几个启动级别了(centos之前版本的用法,而centos7已经不再严格区分级别的概念了),0、1、6运行级别被系统保留:其中0作为shutdown动作,1作为重启至单用户模式,6为重启;在一般的Linux系统实现中,都使用了2、3、4、5几个级别,在CentOS系统中,2表示无NFS支持的多用户模式,3表示完全多用户模式(也是最常用的级别),4保留给用户自定义,5表示图形登录方式。



系统的预设服务可以通过这样的命令实现:service服务名 start|stop|restart。这里的服务名就是/etc/init.d目录下的这些文件了。启动crond除了可以使用命令service crond start外,还可以使用命令/etc/init.d/crond start.

更改某级别下的开启状态:



用 --level 指定级别,后面是服务名,然后是off或者on,--level 后还可以跟多个级别:chkconfig --level 345 crond off

另外还可以省略级别,默认是针对2,3,4,5级别操作:

chkconfig crond on

chkconfig 还有一个功能就是可以把某个服务加入到系统服务,即可以使用 service 服务名 start 这样的形式,并且可以在 chkconfig --list 中查找到。也能删除掉。

cp network 123

chkconfig --add 123 增加服务

vim 123 编辑或查看脚本

chkconfig --del 123 删除服务

3.systemd管理服务

systemctl list-units-files可以列出systemd服务。

systemctl list-units --all --type=service列出所有的service。

LOAD = Reflects whether the unit definition was properly loaded.

ACTIVE = The high-level unit activation state, i.e. generalization of SUB.

SUB = The low-level unit activation state, values depend on unit type.

 

几个常用的服务相关的命令

systemctl enable crond.service //让服务开机启动

systemctl disable crond //不让开机启动

systemctl status crond //查看状态

systemctl stop crond //停止服务

systemctl start crond //启动服务

systemctl restart crond //重启服务

systemctl is-enabled crond //检查服务是否开机启动

[root@gregory ~]# systemctl disable crond

Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service.

[root@gregory ~]# systemctl is-enabled crond

disabled

[root@gregory ~]# systemctl enable crond

Created symlink from /etc/systemd/system/multi-user.target.wants/crond.service to /usr/lib/systemd/system/crond.service.

[root@gregory ~]# 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@gregory ~]# ls -l /etc/systemd/system/multi-user.target.wants/crond.service

lrwxrwxrwx 1 root root 37 Nov 1 18:53 /etc/systemd/system/multi-user.target.wants/crond.service -> /usr/lib/systemd/system/crond.service 软链接

[root@gregory ~]# ls -l /usr/lib/systemd/system/crond.service

-rw-r--r--. 1 root root 284 Mar 31 2016 /usr/lib/systemd/system/crond.service 真正的文件

[root@gregory ~]# systemctl disable crond

Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service.

[root@gregory ~]# ls -l /etc/systemd/system/multi-user.target.wants/crond.service

ls: cannot access /etc/systemd/system/multi-user.target.wants/crond.service: No such file or directory

disable其实就是一出软链接

[root@gregory ~]# systemctl enable crond

Created symlink from /etc/systemd/system/multi-user.target.wants/crond.service to /usr/lib/systemd/system/crond.service.

[root@gregory ~]# ls -l /etc/systemd/system/multi-user.target.wants/crond.service

lrwxrwxrwx 1 root root 37 Nov 1 18:55 /etc/systemd/system/multi-user.target.wants/crond.service -> /usr/lib/systemd/system/crond.service

enable创建软链接

4.unit介绍

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



0关闭系统;1单用户模式;2用户自定义级别,通常识别为级别3;3多用户无图形;4用户自定义级别,通常识别为级别3;5多用户,有图形,比级别3就多了一个图形;6重启

service 系统服务

target 多个unit组成的组

device 硬件设备

mount 文件系统挂载点

automount 自动挂载点

path 文件或路径

scope 不是由systemd启动的外部进程

slice 进程组

snapshot systemd快照

socket 进程间通信套接字

swap swap文件

timer 定时器



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,与is-enabled相似。

5.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



一个service属于一种类型的unit

多个unit组成了一个target

一个target里面包含了多个service

cat /usr/lib/systemd/system/sshd.service //看[install]部分



 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: