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


本文转自 jiekegz  51CTO博客,原文链接:http://blog.51cto.com/jacksoner/1978674



相关文章
|
5月前
|
存储 缓存 Linux
哇~~真的是你呀!今天是LInux安装方法和systemd管理机制。
Linux服务安装和systemd管理机制是Linux操作系统中重要的服务管理机制。systemd是Linux下一种基本的系统服务管理器,负责管理Linux中的各种服务,如进程管理、启动和停止服务、日志记录、挂载文件系统等等。在systemd中,每个服务都以一个Unit的形式存在,Unit定义了该服务相关的一些属性,如服务名称、启动脚本的路径、依赖关系等。
88 0
哇~~真的是你呀!今天是LInux安装方法和systemd管理机制。
|
5天前
|
存储 监控 Ubuntu
Linux 中常用的 systemd 命令讲解
Linux 中常用的 systemd 命令讲解
|
26天前
|
Linux
Linux systemd 定时任务
Linux systemd 定时任务
|
3月前
|
存储 监控 Ubuntu
揭秘 systemd:释放 Linux 服务管理的力量【systemd 一】
揭秘 systemd:释放 Linux 服务管理的力量【systemd 一】
50 0
|
3月前
|
运维 网络协议 Linux
Linux 这50个 systemd 命令值得每位Linux运维工程师收藏!
Linux 这50个 systemd 命令值得每位Linux运维工程师收藏!
446 0
Linux 这50个 systemd 命令值得每位Linux运维工程师收藏!
|
4月前
|
算法 Linux Shell
【Linux】systemv init和systemd的区别
【Linux】systemv init和systemd的区别
41 0
|
7月前
|
存储 Cloud Native Java
Linux下systemd深入指南:如何优化Java服务管理与开机自启配置
Linux下systemd深入指南:如何优化Java服务管理与开机自启配置
155 0
|
8月前
|
Linux
17.1.2 【Linux】systemd使用的unit分类
17.1.2 【Linux】systemd使用的unit分类
74 0
|
11月前
|
域名解析 存储 网络协议
如何在 Linux 中列出 Systemd 下所有正在运行的服务
如何在 Linux 中列出 Systemd 下所有正在运行的服务
149 0
|
12月前
|
Linux
linux系统服务systemd探讨
linux系统服务systemd探讨
138 0