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










本文转自 iekegz 51CTO博客,原文链接:http://blog.51cto.com/jacksoner/1978674,如需转载请自行联系原作者
目录
相关文章
|
7月前
|
安全 Linux Shell
四、Linux核心工具:Vim, 文件链接与SSH
要想在Linux世界里游刃有余,光会“走路”还不够,还得配上几样“高级装备”。首先是Vim编辑器,它像一把瑞士军刀,让你能在命令行里高效地修改文件。然后要懂“软硬链接”,软链接像个快捷方式,硬链接则是给文件起了个别名。最后,SSH是你的“传送门”,不仅能让你安全地远程登录服务器,还能用scp轻松传输文件,设置好密钥更能实现免-密登录,极大提升效率。
509 5
|
6月前
|
Linux 网络安全 Docker
盘古栈云,创建带ssh服务的linux容器
创建带ssh服务的linux容器
399 146
|
11月前
|
Linux 开发工具
7种比较Linux中文本文件的最佳工具
7种比较Linux中文本文件的最佳工具
7种比较Linux中文本文件的最佳工具
|
7月前
|
缓存 安全 Linux
六、Linux核心服务与包管理
在没有网络的情况下,使用系统安装光盘是获取RPM包的常用方法。场景二:配置本地文件镜像源 (使用系统安装光盘/ISO)(检查RPM包的GPG签名以保证安全) 或。YUM/DNF包管理工具 (yum/dnf)(此处可以放置您为本主题制作的思维导图)处理依赖问题的危险选项 (应极力避免)(覆盖文件、替换已安装包)。(list) 则是列出文件。(query file) 是。(假设系统安装光盘已挂载到。信息 (verbose)。(upgrade) 选项。(all) 已安装的包。(package) 选项
528 11
|
7月前
|
Unix Linux 程序员
Linux文本搜索工具grep命令使用指南
以上就是对Linux环境下强大工具 `grep` 的基础到进阶功能介绍。它不仅能够执行简单文字查询任务还能够处理复杂文字处理任务,并且支持强大而灵活地正则表达规范来增加查询精度与效率。无论您是程序员、数据分析师还是系统管理员,在日常工作中熟练运用该命令都将极大提升您处理和分析数据效率。
564 16
|
7月前
|
安全 Linux iOS开发
SonarQube Server 2025 Release 5 (macOS, Linux, Windows) - 代码质量、安全与静态分析工具
SonarQube Server 2025 Release 5 (macOS, Linux, Windows) - 代码质量、安全与静态分析工具
317 0
SonarQube Server 2025 Release 5 (macOS, Linux, Windows) - 代码质量、安全与静态分析工具
|
9月前
|
缓存 监控 Linux
Linux系统性能调优技巧和相关工具
Linux 作为一种应用应展和系统服务的优选操作系统,在处理性能和端到端点评估上持有出色表现。但是,在处理进程或系统处于低效状态时,性能调优就显得十分重要。本文将探讨一些 Linux 系统性能调优的常用技巧,并介绍相关工具
237 1
Linux系统性能调优技巧和相关工具
|
9月前
|
Linux 数据安全/隐私保护 iOS开发
推荐Linux环境下效能优良的双向文件同步工具
综合上述条件,对于Linux环境下的双向文件同步需求,Unison 和 Syncthing 是两个非常出色的选择。它们都有良好的社区支持和文档资源,适用于不同规模的环境,从个人使用到商业部署。Unison 特别适合那些需要手动干预同步过程、需要处理文件冲突解决的场景。而 Syncthing 更加现代化,适合需要自动、实时的数据同步与备份的环境。对于选择哪一个,这将取决于个人的使用场景和具体需求。
949 16
|
8月前
|
数据采集 编解码 运维
一文讲完说懂 WowKey -- WowKey 是一款 Linux 类设备的命令行(CLT)运维工具
WowKey 是一款面向 Linux 类设备的命令行运维工具,支持自动登录、批量执行及标准化维护,适用于企业、团队或个人管理多台设备,显著提升运维效率与质量。