cron,chkconfig,systemd,unit,target介绍

cron任务计划


1.配置文件路径及模板:

[[email protected] ~]# 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

2.编辑该模板:

[[email protected] ~]# crontab -e
no crontab for root - using an empty one
0 3 1-10 */2 2,5  /bin/bash /usr/local/123.sh >> /tmp/123.log 2>> /tmp/123.log

3.启动服务并查看:

[[email protected] ~]# systemctl start crond
[[email protected] ~]# ps aux |grep crond
root        564  0.0  0.1 126236  1636 ?        Ss   16:18   0:00 /usr/sbin/cron d -n
root       1051  0.0  0.0 112676   984 pts/0    S+   16:27   0:00 grep --color=a uto crond
[[email protected] ~]# systemctl status crond
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset : enabled)
   Active: active (running) since 一 2018-01-29 16:18:19 CST; 9min ago
 Main PID: 564 (crond)
   CGroup: /system.slice/crond.service
           └─564 /usr/sbin/crond -n

1月 29 16:18:19 weix-01 systemd[1]: Started Command Scheduler.
1月 29 16:18:19 weix-01 systemd[1]: Starting Command Scheduler...
1月 29 16:18:19 weix-01 crond[564]: (CRON) INFO (RANDOM_DELAY will be scal....)
1月 29 16:18:19 weix-01 crond[564]: (CRON) INFO (running with inotify support)
Hint: Some lines were ellipsized, use -l to show in full.

4.常用用法:

[[email protected] ~]# crontab -e                   # -e编辑
no crontab for root - using an empty one
crontab: installing new crontab
[[email protected] ~]# corntab -l                  # -l 列出
-bash: corntab: 未找到命令
[[email protected] ~]# crontab -l
1 10 * 2 * /usr/bin/find /tmp/ -type f -mtime +100 |xargs rm -f
[[email protected] ~]# cat /var/spool/cron/root              #文件存放路径
1 10 * 2 * /usr/bin/find /tmp/ -type f -mtime +100 |xargs rm -f
[[email protected] ~]# crontab -r                     # -r 删除
[[email protected] ~]# crontab -l
no crontab for root

chkconfig工具


1.列出所有服务:

[[email protected] ~]# 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:关

2.将指定服务开机启动或不启动:

[[email protected] ~]# chkconfig network off
[[email protected] ~]# 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:关
[[email protected] ~]# chkconfig network on
[[email protected] ~]# 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:关

3.指定某个服务某个级别开关:

[[email protected] ~]# chkconfig --level 35 network off
[[email protected] ~]# 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:关
[[email protected] ~]# chkconfig --level 35 network on
[[email protected] ~]# 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:关

4.自定义添加启动脚本:脚本要在init.d路径下,且文件格式有要求

[[email protected] ~]# cd /etc/init.d/
[[email protected] init.d]# cp network  123
[[email protected] init.d]# ls -l
总用量 48
-rwxr-xr-x. 1 root root  7293 1月  29 16:55 123
-rw-r--r--. 1 root root 17500 5月   3 2017 functions
-rwxr-xr-x. 1 root root  4334 5月   3 2017 netconsole
-rwxr-xr-x. 1 root root  7293 5月   3 2017 network
-rw-r--r--. 1 root root  1160 8月   5 14:38 README
[[email protected] init.d]# 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:关
[[email protected] init.d]# chkconfig --add 123
[[email protected] init.d]# chkconfig --list

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。

      要列出 systemd 服务,请执行 ‘systemctl list-unit-files‘。
      查看在具体 target 启用的服务请执行
      ‘systemctl list-dependencies [target]‘。

123             0:关    1:关    2:开    3:开    4:开    5:开    6:关
netconsole      0:关    1:关    2:关    3:关    4:关    5:关    6:关
network         0:关    1:关    2:开    3:开    4:开    5:开    6:关
#! /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

if [ ! -f /etc/sysconfig/network ]; then
    exit 6
fi

. /etc/sysconfig/network

5.删除启动脚本:

[[email protected] init.d]# chkconfig --del 123
[[email protected] init.d]# 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:关

systemd


1.查看服务:

[[email protected] ~]# systemctl list-units --all --type=service
  UNIT                        LOAD      ACTIVE   SUB     DESCRIPTION
  auditd.service              loaded    active   running Security Auditing Servic
  brandbot.service            loaded    inactive dead    Flexible Branding Servic
  chronyd.service             loaded    active   running NTP client/server
  cpupower.service            loaded    inactive dead    Configure CPU power rela
  crond.service               loaded    active   running Command Scheduler
  dbus.service                loaded    active   running D-Bus System Message Bus
● display-manager.service     not-found inactive dead    display-manager.service
  dm-event.service            loaded    inactive dead    Device-mapper event daem
  dracut-shutdown.service     loaded    inactive dead    Restore /run/initramfs
  ebtables.service            loaded    inactive dead    Ethernet Bridge Filterin
  emergency.service           loaded    inactive dead    Emergency Shell
● exim.service                not-found inactive dead    exim.service

2.让服务开机启动,不启动:

[[email protected] ~]# systemctl disable crond.service
Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service.
[[email protected] ~]# systemctl enable crond.service
Created symlink from /etc/systemd/system/multi-user.target.wants/crond.service to /usr/lib/systemd/system/crond.service.

3.查看状态:

[[email protected] ~]# systemctl status crond
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since 一 2018-01-29 22:21:00 CST; 4min 4s ago
 Main PID: 542 (crond)
   CGroup: /system.slice/crond.service
           └─542 /usr/sbin/crond -n

1月 29 22:21:00 weixing01 systemd[1]: Started Command Scheduler.
1月 29 22:21:00 weixing01 systemd[1]: Starting Command Scheduler...
1月 29 22:21:00 weixing01 crond[542]: (CRON) INFO (RANDOM_DELAY will be s...)
1月 29 22:21:00 weixing01 crond[542]: (CRON) INFO (running with inotify s...)
Hint: Some lines were ellipsized, use -l to show in full.

4.停止,启动,重启服务:

[[email protected] ~]# systemctl stop crond
[[email protected] ~]# systemctl start crond
[[email protected] ~]# systemctl restart crond

5.查看服务是否开机启动:

[[email protected] ~]# systemctl is-enabled crond
enabled
[[email protected] ~]# systemctl disable crond.service
Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service.
[[email protected] ~]# systemctl is-enabled crond
disabled

unit

1.七个运行级别:

[[email protected] system]# ls -l runlevel*
lrwxrwxrwx. 1 root root 15 11月 15 02:02 runlevel0.target -> poweroff.target
lrwxrwxrwx. 1 root root 13 11月 15 02:02 runlevel1.target -> rescue.target
lrwxrwxrwx. 1 root root 17 11月 15 02:02 runlevel2.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 11月 15 02:02 runlevel3.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 11月 15 02:02 runlevel4.target -> multi-user.target
lrwxrwxrwx. 1 root root 16 11月 15 02:02 runlevel5.target -> graphical.target
lrwxrwxrwx. 1 root root 13 11月 15 02:02 runlevel6.target -> reboot.target

target

1.查找target服务:

[[email protected] system]# systemctl list-unit-files --type=target
UNIT FILE                 STATE
basic.target              static
bluetooth.target          static
cryptsetup-pre.target     static
cryptsetup.target         static
ctrl-alt-del.target       disabled
default.target            enabled
emergency.target          static
final.target              static
getty.target              static
graphical.target          static
halt.target               disabled
hibernate.target          static

2.查看指定target下面有哪些unit:

[[email protected] system]# systemctl list-dependencies multi-user.target
multi-user.target
● ├─auditd.service
● ├─brandbot.path
● ├─chronyd.service
● ├─crond.service
● ├─dbus.service
● ├─firewalld.service
● ├─irqbalance.service
● ├─kdump.service
● ├─network.service
● ├─NetworkManager.service
● ├─plymouth-quit-wait.service
● ├─plymouth-quit.service
● ├─postfix.service
● ├─rsyslog.service
● ├─sshd.service
● ├─sysstat.service
● ├─systemd-ask-password-wall.path
● ├─systemd-logind.service
● ├─systemd-readahead-collect.service
● ├─systemd-readahead-replay.service
● ├─systemd-update-utmp-runlevel.service
● ├─systemd-user-sessions.service
● ├─tuned.service
● ├─vmtoolsd.service
● ├─basic.target
● │ ├─microcode.service
● │ ├─rhel-autorelabel-mark.service
lines 1-28

3.查看系统默认target:

[[email protected] system]# systemctl  get-default
multi-user.target

4.设置开机启动target:

[[email protected] 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.

5.一个service属于一种类型unit,多个unit组成target,一个target里面包含多个service。
6.查看一个service属于哪个target:看install部分

[[email protected] 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

原文地址:http://blog.51cto.com/13517254/2066608

时间: 2024-08-29 12:24:12

cron,chkconfig,systemd,unit,target介绍的相关文章

10.23-10.27 corn, chkconfig, systemd, unit, target

八周一次课(3月26日)10.23 linux任务计划cron10.24 chkconfig工具10.25 systemd管理服务10.26 unit介绍10.27 target介绍扩展1. anacron  http://blog.csdn.net/strikers1982/article/details/47872262. xinetd服(默认机器没有安装这个服务,需要yum install xinetd安装)   http://blog.sina.com.cn/s/blog_465bbe6

Linux20180511八周一次课cron chkconf systemd unit target

八周一次课(5月11日) 10.23 linux任务计划cron10.24 chkconfig工具10.25 systemd管理服务10.26 unit介绍10.27 target介绍扩展1. anacron http://blog.csdn.net/strikers1982/article/details/47872262. xinetd服(默认机器没有安装这个服务,需要yum install xinetd安装)http://blog.sina.com.cn/s/blog_465bbe6b01

linux任务计划cron、chkconfig工具、systemd管理服务、unit介绍、target介绍

linux任务计划cron [[email protected] ~]# crontab -e no crontab for root - using an empty one 每天的凌晨3点 执行这个 0 3 * * * /usr/bin/touch /root/123.txt &>/dev/null 启动服务 [[email protected] ~]# systemctl start crond 基本格式 : * * * * * command 分 时 日 月 周 命令 第1列表示分钟

cron计划任务、chkconfig工具、systemd管理服务、unit、target介绍

1. linux任务计划cron linux的大部分系统管理工作都是通过定期自动执行某个脚本来完成的,那么如何定期执行某个脚本呢?这就要借助linux的cron功能了,这部分的内容很重要,请牢记! 命令crontab linux的任务计划功能的操作都是通过crontab命令来完成的,其常用的选项有以下几个: -u:表示指定某个用户,不加-u选项则为当前用户. -e:表示制定计划任务 -l:表示列出计划任务 -r:表示删除计划任务 任务计划的配置文件: # cat /etc/crontab SHE

Linux centos7 linux任务计划cron、chkconfig工具、systemd管理服务、unit介绍、 target介绍

一.linux任务计划cron crontab -u  -e -l -r 格式;分 时 日 月 周 user command 文件/var/spool/corn/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 etop crond停止 systemctl start crond.service  二

任务计划cron / chkconfig工具/ system管理服务/unit及target介绍

一.linux任务计划cron 1.cat /etc/crontab 任务计划的配制文件: 2.crontab -e 编辑配制文件. 图片说明,每个月的1-10号3点执行脚本123.sh ,以追加形式生成日志文件123.log,生成错误文件日志321.log crontab -e 实际上是打开了 "/var/spool/cron/username" (如果是root则打开的是/var/spool/cron/root)这个文件,所以不要直接去编辑那个文件,因为可能会出错,所以一定要使用

10.23 linux任务计划cron10.24chkconfig工具10.25 systemd管理服务10.26 unit介绍 10.27 target介绍

- 10.23 linux任务计划cron - 10.24 chkconfig工具 - 10.25 systemd管理服务 - 10.26 unit介绍 - 10.27 target介绍 - 扩展 1. anacron http://blog.csdn.net/strikers1982/article/details/4787226  2. xinetd服(默认机器没有安装这个服务,需要yum install xinetd安装) http://blog.sina.com.cn/s/blog_46

linux日常运维(crond,systemd,chkconfing,unit,target)

1.任务计划:crond [[email protected] ~]# cat /etc/crontab                             (crontab配置文件) SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin                                 (命令的路径) MAILTO=root                                               (发送邮件给

Systemd详解之单元配置systemd.unit

英文网址:https://www.freedesktop.org/software/systemd/man/systemd.unit.html 名称 systemd.unit - systemd 单元配置 概要 service.service, socket.socket, device.device, mount.mount, automount.automount, swap.swap, target.target, path.path, timer.timer, slice.slice,

15.2、systemd守护进程介绍

1.系统启动的流程回顾: post(加电自检,主要用来检查支持系统启动的硬件环境是否满足)---> bootseqence(根据bios中设定启动顺序,去加载相应的启动项设备)---> bootloader(根据启动项设备的mbr去加载bootloader)---> kernel(加载内核至内存中)---> rootfs(根切换)---> init(/sbin/init) 不同系统的init程序不一样: centos5:sysV init //V表示版本号,第五版 cento