码迷,mamicode.com
首页 > 其他好文 > 详细

7.17 10.23-10.27

时间:2018-07-18 10:14:29      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:ash   http   用户模式   apr   解决办法   queue   base   图形化界面   roo   

10.23 linux任务计划cron

 

linux任务计划配置文件

[root@hyc-01-01 ~]# cat /etc/crontab

SHELL=/bin/bash 文件中定义的shell

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 星期几(07均表示周日)

# |  |  |  |  |

# *  *  *  *  * user-name  command to be executed 用户 要执行的命令

 

定义一个任务计划

[root@hyc-01-01 ~]# crontab –e 用法同vim

no crontab for root - using an empty one

 

0 3 * * * /bin/bash /usr/local/sbin/123.sh>>/tmp/123.log 2>>/tmp/123.log

解释:在每月每周每日的3点执行shell脚本123.sh,将执行结果中正确的内容追加重定向到123.log文件,将执行结果中错误的内容也追加重定向到123.log文件

 

[root@hyc-01-01 ~]# crontab –e

no crontab for root - using an empty one

 

0 3 1-10 */2 2,5 /bin/bash /usr/local/sbin/123.sh>>/tmp/123.log 2>>/tmp/123.log

解释:在偶数月的110号的周二和周五的凌晨3点执行shell脚本

 

如何指定年份执行脚本?

每年的同一个日期的星期一定是不一样的,可以基于此确定脚本执行的年份

 

启动任务计划cron

[root@hyc-01-01 ~]# ps aux|grep cron

root       503  0.0  0.1 126264  1696 ?        Ss   03:48   0:00 /usr/sbin/crond -n

root      6833  0.0  0.0 112676   984 pts/0    S+   20:45   0:00 grep --color=auto cron

有上面的进程则说明服务已经启动

[root@hyc-01-01 ~]# systemctl status crond

crond.service - Command Scheduler

   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)

   Active: active (running) since 2018-07-15 22:05:31 CST; 1 day 22h ago

 Main PID: 503 (crond)

   CGroup: /system.slice/crond.service

           └─503 /usr/sbin/crond -n

 

7 15 22:05:31 hyc-01-01 systemd[1]: Started Command Scheduler.

7 15 22:05:31 hyc-01-01 systemd[1]: Starting Command Scheduler...

7 15 22:05:31 hyc-01-01 crond[503]: (CRON) INFO (RANDOM_DELAY will be scaled....)

7 15 22:05:32 hyc-01-01 crond[503]: (CRON) INFO (running with inotify support)

Hint: Some lines were ellipsized, use -l to show in full.

以上activeactiverunning)且为绿色说明服务正常启动

[root@hyc-01-01 ~]# systemctl stop crond

[root@hyc-01-01 ~]# systemctl status crond

crond.service - Command Scheduler

   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)

   Active: inactive (dead) since 2018-07-17 20:50:11 CST; 2s ago

 Main PID: 503 (code=exited, status=0/SUCCESS)

 

7 15 22:05:31 hyc-01-01 systemd[1]: Started Command Scheduler.

7 15 22:05:31 hyc-01-01 systemd[1]: Starting Command Scheduler...

7 15 22:05:31 hyc-01-01 crond[503]: (CRON) INFO (RANDOM_DELAY will be scaled....)

7 15 22:05:32 hyc-01-01 crond[503]: (CRON) INFO (running with inotify support)

7 17 20:50:11 hyc-01-01 systemd[1]: Stopping Command Scheduler...

7 17 20:50:11 hyc-01-01 systemd[1]: Stopped Command Scheduler.

Hint: Some lines were ellipsized, use -l to show in full.

此时crond服务未启动

 

crond任务计划中的脚本未执行的原因

有时脚本中用到的命令不在crond配置文件(/etc/crontab)定义的PATH路径中,导致找不到命令无法执行:

解决办法:

1 在配置文件的PATH路径中加入对应命令的路径

2 写任务计划时直接写命令的绝对路径

建议写任务计划时将执行的正确和错误结果重定向到文件,这样使执行过程有据可查,可以清楚的知道任务是否正确执行

 

-l 查看系统当前用户的任务计划

[root@hyc-01-01 ~]# crontab -l

0 3 * * * /bin/bash /usr/local/sbin/123.sh>>/tmp/123.log 2>>/tmp/123.log

 

crontab文件

[root@hyc-01-01 ~]# cat /var/spool/cron/root

0 3 * * * /bin/bash /usr/local/sbin/123.sh>>/tmp/123.log 2>>/tmp/123.log

该文件存放crontab –l命令显示的内容

 

10.24 chkconfig工具

 

chkconfig工具在centos6及更早的版本中常用,centos7不常用

 

查看当前系统中使用chkconfig工具的服务

[root@hyc-01-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:

centos6及更早版本使用的服务管理机制叫SysVcentos7使用的服务管理机制为systemd

centos7中的服务管理进程为systemdcentos6的服务管理进程为init

[root@hyc-01-01 ~]# ls /etc/init.d 以上两个服务脚本的存放位置

functions  netconsole  network  README

 

关闭network服务

[root@hyc-01-01 ~]# chkconfig network off

[root@hyc-01-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:

以上0-6的开关表示在0-67个级别分别是什么状态,数字对应的开关表示开机启动或关闭

几个级别的状态:

0 关机状态

1 单用户模式

2 3级别少了NFS服务

3 无图形化界面的多用户模式

4 保留级别

5 多用户带图形

6 重启

centos6及更早的版本通过修改/etc/inittab定义服务的运行级别,centos7中已不再使用

 

指定某一级别时关闭或开启服务

[root@hyc-01-01 ~]# chkconfig --level 3 network on

[root@hyc-01-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:

[root@hyc-01-01 ~]# chkconfig --level 234 network on

[root@hyc-01-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:

一般0/1/6级别服务关闭,其他级别服务可以选择性开启

 

将某个服务/脚本加入chkconfig列表

[root@hyc-01-01 init.d]# cp network 123 123作为新的脚本/服务

[root@hyc-01-01 init.d]# ls

123  functions  netconsole  network  README

[root@hyc-01-01 init.d]# chkconfig --add 123 123加入chkconfig服务列表

[root@hyc-01-01 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:

加入服务列表的要求:

1 将指定的脚本放到/etc/init.d目录下

2 文件名称无要求

3 文件内容为shell脚本

4 文件中必须有红字内容才能被识别

#! /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

 

从列表中删除某服务

[root@hyc-01-01 init.d]# chkconfig --del 123

[root@hyc-01-01 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:

 

10.25 systemd管理服务

 

systemdcentos7的服务管理机制

 

列出systemd中的service

[root@hyc-01-01 ~]# systemctl list-units --all --type=service

  UNIT                         LOAD      ACTIVE   SUB     DESCRIPTION

  auditd.service               loaded    active   running Security Auditing Service

  brandbot.service             loaded    inactive dead    Flexible Branding Service

  chronyd.service              loaded    active   running NTP client/server

  cpupower.service             loaded    inactive dead    Configure CPU power relate

  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

  dracut-shutdown.service      loaded    inactive dead    Restore /run/initramfs

  ebtables.service             loaded    inactive dead    Ethernet Bridge Filtering

  emergency.service            loaded    inactive dead    Emergency Shell

exim.service                 not-found inactive dead    exim.service

  firewalld.service            loaded    active   running firewalld - dynamic firewa

  getty@tty1.service           loaded    active   running Getty on tty1

  ip6tables.service            loaded    inactive dead    IPv6 firewall with ip6tabl

ipset.service                not-found inactive dead    ipset.service

  iptables.service             loaded    inactive dead    IPv4 firewall with iptable

  irqbalance.service           loaded    inactive dead    irqbalance daemon

kdump.service                loaded    failed   failed  Crash recovery kernel armi

  kmod-static-nodes.service    loaded    active   exited  Create list of required st

  microcode.service            loaded    inactive dead    Load CPU microcode update

  network.service              loaded    active   exited  LSB: Bring up/down network

  NetworkManager-wait-online.service loaded    active   exited  Network Manager Wait

  NetworkManager.service       loaded    active   running Network Manager

ntpd.service                 not-found inactive dead    ntpd.service

ntpdate.service              not-found inactive dead    ntpdate.service

  plymouth-quit-wait.service   loaded    inactive dead    Wait for Plymouth Boot Scr

  plymouth-quit.service        loaded    inactive dead    Terminate Plymouth Boot Sc

  plymouth-read-write.service  loaded    inactive dead    Tell Plymouth To Write Out

  plymouth-start.service       loaded    inactive dead    Show Plymouth Boot Screen

  polkit.service               loaded    active   running Authorization Manager

  postfix.service              loaded    active   running Postfix Mail Transport Age

  rc-local.service             loaded    inactive dead    /etc/rc.d/rc.local Compati

  rescue.service               loaded    inactive dead    Rescue Shell

  rhel-autorelabel-mark.service loaded    inactive dead    Mark the need to relabel

  rhel-autorelabel.service     loaded    inactive dead    Relabel all filesystems, i

  rhel-configure.service       loaded    inactive dead    Reconfigure the system on

  rhel-dmesg.service           loaded    active   exited  Dump dmesg to /var/log/dme

  rhel-import-state.service    loaded    active   exited  Import network configurati

  rhel-loadmodules.service     loaded    inactive dead    Load legacy module configu

  rhel-readonly.service        loaded    active   exited  Configure read-only root s

  rsyslog.service              loaded    active   running System Logging Service

  selinux-policy-migrate-local-changes@targeted.service loaded    inactive dead    M

sendmail.service             not-found inactive dead    sendmail.service

sntp.service                 not-found inactive dead    sntp.service

  sshd-keygen.service          loaded    inactive dead    OpenSSH Server Key Generat

  sshd.service                 loaded    active   running OpenSSH server daemon

syslog.service               not-found inactive dead    syslog.service

  systemd-ask-password-console.service loaded    inactive dead    Dispatch Password

  systemd-ask-password-plymouth.service loaded    inactive dead    Forward Password

  systemd-ask-password-wall.service loaded    inactive dead    Forward Password Requ

  systemd-binfmt.service       loaded    inactive dead    Set Up Additional Binary F

  systemd-firstboot.service    loaded    inactive dead    First Boot Wizard

  systemd-fsck-root.service    loaded    inactive dead    File System Check on Root

  systemd-hwdb-update.service  loaded    inactive dead    Rebuild Hardware Database

  systemd-initctl.service      loaded    inactive dead    /dev/initctl Compatibility

  systemd-journal-catalog-update.service loaded    inactive dead    Rebuild Journal

  systemd-journal-flush.service loaded    active   exited  Flush Journal to Persiste

  systemd-journald.service     loaded    active   running Journal Service

  systemd-logind.service       loaded    active   running Login Service

  systemd-machine-id-commit.service loaded    inactive dead    Commit a transient ma

  systemd-modules-load.service loaded    inactive dead    Load Kernel Modules

systemd-random-seed-load.service not-found inactive dead    systemd-random-seed-lo

  systemd-random-seed.service  loaded    active   exited  Load/Save Random Seed

  systemd-readahead-collect.service loaded    inactive dead    Collect Read-Ahead Da

  systemd-readahead-done.service loaded    inactive dead    Stop Read-Ahead Data Col

  systemd-readahead-replay.service loaded    inactive dead    Replay Read-Ahead Data

  systemd-reboot.service       loaded    inactive dead    Reboot

  systemd-remount-fs.service   loaded    active   exited  Remount Root and Kernel Fi

  systemd-shutdownd.service    loaded    inactive dead    Delayed Shutdown Service

  systemd-sysctl.service       loaded    active   exited  Apply Kernel Variables

systemd-sysusers.service     not-found inactive dead    systemd-sysusers.service

systemd-timesyncd.service    not-found inactive dead    systemd-timesyncd.service

  systemd-tmpfiles-clean.service loaded    inactive dead    Cleanup of Temporary Dir

  systemd-tmpfiles-setup-dev.service loaded    active   exited  Create Static Device

  systemd-tmpfiles-setup.service loaded    active   exited  Create Volatile Files an

  systemd-udev-trigger.service loaded    active   exited  udev Coldplug all Devices

  systemd-udevd.service        loaded    active   running udev Kernel Device Manager

  systemd-update-done.service  loaded    inactive dead    Update is Completed

  systemd-update-utmp-runlevel.service loaded    inactive dead    Update UTMP about

  systemd-update-utmp.service  loaded    active   exited  Update UTMP about System B

  systemd-user-sessions.service loaded    active   exited  Permit User Sessions

  systemd-vconsole-setup.service loaded    active   exited  Setup Virtual Console

  tuned.service                loaded    active   running Dynamic System Tuning Daem

  vgauthd.service              loaded    active   running VGAuth Service for open-vm

  vmtoolsd.service             loaded    active   running Service for virtual machin

 

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.

 

85 loaded units listed.

To show all installed unit files use 'systemctl list-unit-files'.

lines 78-93/93 (END)

DESCRIPTION:描述信息

SUB:是否running

ACTIVE:是否active

LOAD:是否load

 

不加—all则不会列出inactive(未激活状态)的service

[root@hyc-01-01 ~]# systemctl list-units --type=service

  UNIT                          LOAD   ACTIVE SUB     DESCRIPTION

  auditd.service                loaded active running Security Auditing Service

  chronyd.service               loaded active running NTP client/server

  crond.service                 loaded active running Command Scheduler

  dbus.service                  loaded active running D-Bus System Message Bus

  firewalld.service             loaded active running firewalld - dynamic firewall d

  getty@tty1.service            loaded active running Getty on tty1

kdump.service                 loaded failed failed  Crash recovery kernel arming

  kmod-static-nodes.service     loaded active exited  Create list of required static

  network.service               loaded active exited  LSB: Bring up/down networking

  NetworkManager-wait-online.service loaded active exited  Network Manager Wait Onli

  NetworkManager.service        loaded active running Network Manager

  polkit.service                loaded active running Authorization Manager

  postfix.service               loaded active running Postfix Mail Transport Agent

  rhel-dmesg.service            loaded active exited  Dump dmesg to /var/log/dmesg

  rhel-import-state.service     loaded active exited  Import network configuration f

  rhel-readonly.service         loaded active exited  Configure read-only root suppo

  rsyslog.service               loaded active running System Logging Service

  sshd.service                  loaded active running OpenSSH server daemon

  systemd-journal-flush.service loaded active exited  Flush Journal to Persistent St

  systemd-journald.service      loaded active running Journal Service

  systemd-logind.service        loaded active running Login Service

  systemd-random-seed.service   loaded active exited  Load/Save Random Seed

  systemd-remount-fs.service    loaded active exited  Remount Root and Kernel File S

  systemd-sysctl.service        loaded active exited  Apply Kernel Variables

  systemd-tmpfiles-setup-dev.service loaded active exited  Create Static Device Node

  systemd-tmpfiles-setup.service loaded active exited  Create Volatile Files and Dir

  systemd-udev-trigger.service  loaded active exited  udev Coldplug all Devices

  systemd-udevd.service         loaded active running udev Kernel Device Manager

  systemd-update-utmp.service   loaded active exited  Update UTMP about System Boot/

  systemd-user-sessions.service loaded active exited  Permit User Sessions

  systemd-vconsole-setup.service loaded active exited  Setup Virtual Console

  tuned.service                 loaded active running Dynamic System Tuning Daemon

  vgauthd.service               loaded active running VGAuth Service for open-vm-too

  vmtoolsd.service              loaded active running Service for virtual machines h

 

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.

 

34 loaded units listed. Pass --all to see loaded but inactive units, too.

To show all installed unit files use 'systemctl list-unit-files'.

lines 27-42/42 (END)

 

这里显示的内容包含了targetservicesocket,不仅是service

[root@hyc-01-01 ~]# systemctl list-unit-files

UNIT FILE                                     STATE  

proc-sys-fs-binfmt_misc.automount             static 

dev-hugepages.mount                           static 

dev-mqueue.mount                              static 

proc-sys-fs-binfmt_misc.mount                 static 

sys-fs-fuse-connections.mount                 static 

sys-kernel-config.mount                       static 

sys-kernel-debug.mount                        static 

tmp.mount                                     disabled

brandbot.path                                 disabled

systemd-ask-password-console.path             static 

systemd-ask-password-plymouth.path            static 

 

指定服务是否开机启动

[root@hyc-01-01 ~]# systemctl enable crond.service 开机启动

[root@hyc-01-01 ~]# systemctl disable crond.service 开机不启动

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

 

查看服务的状态

[root@hyc-01-01 ~]# systemctl status crond.service

crond.service - Command Scheduler

   Loaded: loaded (/usr/lib/systemd/system/crond.service; disabled; vendor preset: enabled)

   Active: active (running) since 2018-07-17 20:51:53 CST; 10h ago

 Main PID: 6851 (crond)

   CGroup: /system.slice/crond.service

           └─6851 /usr/sbin/crond -n

 

7 17 20:51:53 hyc-01-01 systemd[1]: Started Command Scheduler.

7 17 20:51:53 hyc-01-01 systemd[1]: Starting Command Scheduler...

7 17 20:51:53 hyc-01-01 crond[6851]: (CRON) INFO (RANDOM_DELAY will be scale....)

7 17 20:51:53 hyc-01-01 crond[6851]: (CRON) INFO (running with inotify support)

7 17 20:51:53 hyc-01-01 crond[6851]: (CRON) INFO (@reboot jobs will be run a....)

7 17 21:04:01 hyc-01-01 crond[6851]: (root) RELOAD (/var/spool/cron/root)

Hint: Some lines were ellipsized, use -l to show in full.

 

启动和停止服务

[root@hyc-01-01 ~]# systemctl stop crond 停止服务

[root@hyc-01-01 ~]# systemctl start crond 启动服务

[root@hyc-01-01 ~]# systemctl restart crond 重启服务

 

检测service是否开机启动

[root@hyc-01-01 ~]# systemctl is-enabled crond

disabled

 

crond实际为一个软链接,源文件在/usr/lib下,链接文件在/etc/systemd

[root@hyc-01-01 ~]# systemctl is-enabled crond

disabled

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

ls: 无法访问/etc/systemd/system/multi-user.target.wants/crond.service: 没有那个文件或目录

[root@hyc-01-01 ~]# systemctl enable crond

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

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

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

 

10.26 unit介绍

 

技术分享图片

 

10.27 target介绍

 

10.23 linux任务计划cron

 

linux任务计划配置文件

[root@hyc-01-01 ~]# cat /etc/crontab

SHELL=/bin/bash 文件中定义的shell

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 星期几(07均表示周日)

# |  |  |  |  |

# *  *  *  *  * user-name  command to be executed 用户 要执行的命令

 

定义一个任务计划

[root@hyc-01-01 ~]# crontab –e 用法同vim

no crontab for root - using an empty one

 

0 3 * * * /bin/bash /usr/local/sbin/123.sh>>/tmp/123.log 2>>/tmp/123.log

解释:在每月每周每日的3点执行shell脚本123.sh,将执行结果中正确的内容追加重定向到123.log文件,将执行结果中错误的内容也追加重定向到123.log文件

 

[root@hyc-01-01 ~]# crontab –e

no crontab for root - using an empty one

 

0 3 1-10 */2 2,5 /bin/bash /usr/local/sbin/123.sh>>/tmp/123.log 2>>/tmp/123.log

解释:在偶数月的110号的周二和周五的凌晨3点执行shell脚本

 

如何指定年份执行脚本?

每年的同一个日期的星期一定是不一样的,可以基于此确定脚本执行的年份

 

启动任务计划cron

[root@hyc-01-01 ~]# ps aux|grep cron

root       503  0.0  0.1 126264  1696 ?        Ss   03:48   0:00 /usr/sbin/crond -n

root      6833  0.0  0.0 112676   984 pts/0    S+   20:45   0:00 grep --color=auto cron

有上面的进程则说明服务已经启动

[root@hyc-01-01 ~]# systemctl status crond

crond.service - Command Scheduler

   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)

   Active: active (running) since 2018-07-15 22:05:31 CST; 1 day 22h ago

 Main PID: 503 (crond)

   CGroup: /system.slice/crond.service

           └─503 /usr/sbin/crond -n

 

7 15 22:05:31 hyc-01-01 systemd[1]: Started Command Scheduler.

7 15 22:05:31 hyc-01-01 systemd[1]: Starting Command Scheduler...

7 15 22:05:31 hyc-01-01 crond[503]: (CRON) INFO (RANDOM_DELAY will be scaled....)

7 15 22:05:32 hyc-01-01 crond[503]: (CRON) INFO (running with inotify support)

Hint: Some lines were ellipsized, use -l to show in full.

以上activeactiverunning)且为绿色说明服务正常启动

[root@hyc-01-01 ~]# systemctl stop crond

[root@hyc-01-01 ~]# systemctl status crond

crond.service - Command Scheduler

   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)

   Active: inactive (dead) since 2018-07-17 20:50:11 CST; 2s ago

 Main PID: 503 (code=exited, status=0/SUCCESS)

 

7 15 22:05:31 hyc-01-01 systemd[1]: Started Command Scheduler.

7 15 22:05:31 hyc-01-01 systemd[1]: Starting Command Scheduler...

7 15 22:05:31 hyc-01-01 crond[503]: (CRON) INFO (RANDOM_DELAY will be scaled....)

7 15 22:05:32 hyc-01-01 crond[503]: (CRON) INFO (running with inotify support)

7 17 20:50:11 hyc-01-01 systemd[1]: Stopping Command Scheduler...

7 17 20:50:11 hyc-01-01 systemd[1]: Stopped Command Scheduler.

Hint: Some lines were ellipsized, use -l to show in full.

此时crond服务未启动

 

crond任务计划中的脚本未执行的原因

有时脚本中用到的命令不在crond配置文件(/etc/crontab)定义的PATH路径中,导致找不到命令无法执行:

解决办法:

1 在配置文件的PATH路径中加入对应命令的路径

2 写任务计划时直接写命令的绝对路径

建议写任务计划时将执行的正确和错误结果重定向到文件,这样使执行过程有据可查,可以清楚的知道任务是否正确执行

 

-l 查看系统当前用户的任务计划

[root@hyc-01-01 ~]# crontab -l

0 3 * * * /bin/bash /usr/local/sbin/123.sh>>/tmp/123.log 2>>/tmp/123.log

 

crontab文件

[root@hyc-01-01 ~]# cat /var/spool/cron/root

0 3 * * * /bin/bash /usr/local/sbin/123.sh>>/tmp/123.log 2>>/tmp/123.log

该文件存放crontab –l命令显示的内容

 

10.24 chkconfig工具

 

chkconfig工具在centos6及更早的版本中常用,centos7不常用

 

查看当前系统中使用chkconfig工具的服务

[root@hyc-01-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:

centos6及更早版本使用的服务管理机制叫SysVcentos7使用的服务管理机制为systemd

centos7中的服务管理进程为systemdcentos6的服务管理进程为init

[root@hyc-01-01 ~]# ls /etc/init.d 以上两个服务脚本的存放位置

functions  netconsole  network  README

 

关闭network服务

[root@hyc-01-01 ~]# chkconfig network off

[root@hyc-01-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:

以上0-6的开关表示在0-67个级别分别是什么状态,数字对应的开关表示开机启动或关闭

几个级别的状态:

0 关机状态

1 单用户模式

2 3级别少了NFS服务

3 无图形化界面的多用户模式

4 保留级别

5 多用户带图形

6 重启

centos6及更早的版本通过修改/etc/inittab定义服务的运行级别,centos7中已不再使用

 

指定某一级别时关闭或开启服务

[root@hyc-01-01 ~]# chkconfig --level 3 network on

[root@hyc-01-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:

[root@hyc-01-01 ~]# chkconfig --level 234 network on

[root@hyc-01-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:

一般0/1/6级别服务关闭,其他级别服务可以选择性开启

 

将某个服务/脚本加入chkconfig列表

[root@hyc-01-01 init.d]# cp network 123 123作为新的脚本/服务

[root@hyc-01-01 init.d]# ls

123  functions  netconsole  network  README

[root@hyc-01-01 init.d]# chkconfig --add 123 123加入chkconfig服务列表

[root@hyc-01-01 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:

加入服务列表的要求:

1 将指定的脚本放到/etc/init.d目录下

2 文件名称无要求

3 文件内容为shell脚本

4 文件中必须有红字内容才能被识别

#! /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

 

从列表中删除某服务

[root@hyc-01-01 init.d]# chkconfig --del 123

[root@hyc-01-01 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:

 

10.25 systemd管理服务

 

systemdcentos7的服务管理机制

 

列出systemd中的service

[root@hyc-01-01 ~]# systemctl list-units --all --type=service

  UNIT                         LOAD      ACTIVE   SUB     DESCRIPTION

  auditd.service               loaded    active   running Security Auditing Service

  brandbot.service             loaded    inactive dead    Flexible Branding Service

  chronyd.service              loaded    active   running NTP client/server

  cpupower.service             loaded    inactive dead    Configure CPU power relate

  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

  dracut-shutdown.service      loaded    inactive dead    Restore /run/initramfs

  ebtables.service             loaded    inactive dead    Ethernet Bridge Filtering

  emergency.service            loaded    inactive dead    Emergency Shell

exim.service                 not-found inactive dead    exim.service

  firewalld.service            loaded    active   running firewalld - dynamic firewa

  getty@tty1.service           loaded    active   running Getty on tty1

  ip6tables.service            loaded    inactive dead    IPv6 firewall with ip6tabl

ipset.service                not-found inactive dead    ipset.service

  iptables.service             loaded    inactive dead    IPv4 firewall with iptable

  irqbalance.service           loaded    inactive dead    irqbalance daemon

kdump.service                loaded    failed   failed  Crash recovery kernel armi

  kmod-static-nodes.service    loaded    active   exited  Create list of required st

  microcode.service            loaded    inactive dead    Load CPU microcode update

  network.service              loaded    active   exited  LSB: Bring up/down network

  NetworkManager-wait-online.service loaded    active   exited  Network Manager Wait

  NetworkManager.service       loaded    active   running Network Manager

ntpd.service                 not-found inactive dead    ntpd.service

ntpdate.service              not-found inactive dead    ntpdate.service

  plymouth-quit-wait.service   loaded    inactive dead    Wait for Plymouth Boot Scr

  plymouth-quit.service        loaded    inactive dead    Terminate Plymouth Boot Sc

  plymouth-read-write.service  loaded    inactive dead    Tell Plymouth To Write Out

  plymouth-start.service       loaded    inactive dead    Show Plymouth Boot Screen

  polkit.service               loaded    active   running Authorization Manager

  postfix.service              loaded    active   running Postfix Mail Transport Age

  rc-local.service             loaded    inactive dead    /etc/rc.d/rc.local Compati

  rescue.service               loaded    inactive dead    Rescue Shell

  rhel-autorelabel-mark.service loaded    inactive dead    Mark the need to relabel

  rhel-autorelabel.service     loaded    inactive dead    Relabel all filesystems, i

  rhel-configure.service       loaded    inactive dead    Reconfigure the system on

  rhel-dmesg.service           loaded    active   exited  Dump dmesg to /var/log/dme

  rhel-import-state.service    loaded    active   exited  Import network configurati

  rhel-loadmodules.service     loaded    inactive dead    Load legacy module configu

  rhel-readonly.service        loaded    active   exited  Configure read-only root s

  rsyslog.service              loaded    active   running System Logging Service

  selinux-policy-migrate-local-changes@targeted.service loaded    inactive dead    M

sendmail.service             not-found inactive dead    sendmail.service

sntp.service                 not-found inactive dead    sntp.service

  sshd-keygen.service          loaded    inactive dead    OpenSSH Server Key Generat

  sshd.service                 loaded    active   running OpenSSH server daemon

syslog.service               not-found inactive dead    syslog.service

  systemd-ask-password-console.service loaded    inactive dead    Dispatch Password

  systemd-ask-password-plymouth.service loaded    inactive dead    Forward Password

  systemd-ask-password-wall.service loaded    inactive dead    Forward Password Requ

  systemd-binfmt.service       loaded    inactive dead    Set Up Additional Binary F

  systemd-firstboot.service    loaded    inactive dead    First Boot Wizard

  systemd-fsck-root.service    loaded    inactive dead    File System Check on Root

  systemd-hwdb-update.service  loaded    inactive dead    Rebuild Hardware Database

  systemd-initctl.service      loaded    inactive dead    /dev/initctl Compatibility

  systemd-journal-catalog-update.service loaded    inactive dead    Rebuild Journal

  systemd-journal-flush.service loaded    active   exited  Flush Journal to Persiste

  systemd-journald.service     loaded    active   running Journal Service

  systemd-logind.service       loaded    active   running Login Service

  systemd-machine-id-commit.service loaded    inactive dead    Commit a transient ma

  systemd-modules-load.service loaded    inactive dead    Load Kernel Modules

systemd-random-seed-load.service not-found inactive dead    systemd-random-seed-lo

  systemd-random-seed.service  loaded    active   exited  Load/Save Random Seed

  systemd-readahead-collect.service loaded    inactive dead    Collect Read-Ahead Da

  systemd-readahead-done.service loaded    inactive dead    Stop Read-Ahead Data Col

  systemd-readahead-replay.service loaded    inactive dead    Replay Read-Ahead Data

  systemd-reboot.service       loaded    inactive dead    Reboot

  systemd-remount-fs.service   loaded    active   exited  Remount Root and Kernel Fi

  systemd-shutdownd.service    loaded    inactive dead    Delayed Shutdown Service

  systemd-sysctl.service       loaded    active   exited  Apply Kernel Variables

systemd-sysusers.service     not-found inactive dead    systemd-sysusers.service

systemd-timesyncd.service    not-found inactive dead    systemd-timesyncd.service

  systemd-tmpfiles-clean.service loaded    inactive dead    Cleanup of Temporary Dir

  systemd-tmpfiles-setup-dev.service loaded    active   exited  Create Static Device

  systemd-tmpfiles-setup.service loaded    active   exited  Create Volatile Files an

  systemd-udev-trigger.service loaded    active   exited  udev Coldplug all Devices

  systemd-udevd.service        loaded    active   running udev Kernel Device Manager

  systemd-update-done.service  loaded    inactive dead    Update is Completed

  systemd-update-utmp-runlevel.service loaded    inactive dead    Update UTMP about

  systemd-update-utmp.service  loaded    active   exited  Update UTMP about System B

  systemd-user-sessions.service loaded    active   exited  Permit User Sessions

  systemd-vconsole-setup.service loaded    active   exited  Setup Virtual Console

  tuned.service                loaded    active   running Dynamic System Tuning Daem

  vgauthd.service              loaded    active   running VGAuth Service for open-vm

  vmtoolsd.service             loaded    active   running Service for virtual machin

 

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.

 

85 loaded units listed.

To show all installed unit files use 'systemctl list-unit-files'.

lines 78-93/93 (END)

DESCRIPTION:描述信息

SUB:是否running

ACTIVE:是否active

LOAD:是否load

 

不加—all则不会列出inactive(未激活状态)的service

[root@hyc-01-01 ~]# systemctl list-units --type=service

  UNIT                          LOAD   ACTIVE SUB     DESCRIPTION

  auditd.service                loaded active running Security Auditing Service

  chronyd.service               loaded active running NTP client/server

  crond.service                 loaded active running Command Scheduler

  dbus.service                  loaded active running D-Bus System Message Bus

  firewalld.service             loaded active running firewalld - dynamic firewall d

  getty@tty1.service            loaded active running Getty on tty1

kdump.service                 loaded failed failed  Crash recovery kernel arming

  kmod-static-nodes.service     loaded active exited  Create list of required static

  network.service               loaded active exited  LSB: Bring up/down networking

  NetworkManager-wait-online.service loaded active exited  Network Manager Wait Onli

  NetworkManager.service        loaded active running Network Manager

  polkit.service                loaded active running Authorization Manager

  postfix.service               loaded active running Postfix Mail Transport Agent

  rhel-dmesg.service            loaded active exited  Dump dmesg to /var/log/dmesg

  rhel-import-state.service     loaded active exited  Import network configuration f

  rhel-readonly.service         loaded active exited  Configure read-only root suppo

  rsyslog.service               loaded active running System Logging Service

  sshd.service                  loaded active running OpenSSH server daemon

  systemd-journal-flush.service loaded active exited  Flush Journal to Persistent St

  systemd-journald.service      loaded active running Journal Service

  systemd-logind.service        loaded active running Login Service

  systemd-random-seed.service   loaded active exited  Load/Save Random Seed

  systemd-remount-fs.service    loaded active exited  Remount Root and Kernel File S

  systemd-sysctl.service        loaded active exited  Apply Kernel Variables

  systemd-tmpfiles-setup-dev.service loaded active exited  Create Static Device Node

  systemd-tmpfiles-setup.service loaded active exited  Create Volatile Files and Dir

  systemd-udev-trigger.service  loaded active exited  udev Coldplug all Devices

  systemd-udevd.service         loaded active running udev Kernel Device Manager

  systemd-update-utmp.service   loaded active exited  Update UTMP about System Boot/

  systemd-user-sessions.service loaded active exited  Permit User Sessions

  systemd-vconsole-setup.service loaded active exited  Setup Virtual Console

  tuned.service                 loaded active running Dynamic System Tuning Daemon

  vgauthd.service               loaded active running VGAuth Service for open-vm-too

  vmtoolsd.service              loaded active running Service for virtual machines h

 

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.

 

34 loaded units listed. Pass --all to see loaded but inactive units, too.

To show all installed unit files use 'systemctl list-unit-files'.

lines 27-42/42 (END)

 

这里显示的内容包含了targetservicesocket,不仅是service

[root@hyc-01-01 ~]# systemctl list-unit-files

UNIT FILE                                     STATE  

proc-sys-fs-binfmt_misc.automount             static 

dev-hugepages.mount                           static 

dev-mqueue.mount                              static 

proc-sys-fs-binfmt_misc.mount                 static 

sys-fs-fuse-connections.mount                 static 

sys-kernel-config.mount                       static 

sys-kernel-debug.mount                        static 

tmp.mount                                     disabled

brandbot.path                                 disabled

systemd-ask-password-console.path             static 

systemd-ask-password-plymouth.path            static 

 

指定服务是否开机启动

[root@hyc-01-01 ~]# systemctl enable crond.service 开机启动

[root@hyc-01-01 ~]# systemctl disable crond.service 开机不启动

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

 

查看服务的状态

[root@hyc-01-01 ~]# systemctl status crond.service

crond.service - Command Scheduler

   Loaded: loaded (/usr/lib/systemd/system/crond.service; disabled; vendor preset: enabled)

   Active: active (running) since 2018-07-17 20:51:53 CST; 10h ago

 Main PID: 6851 (crond)

   CGroup: /system.slice/crond.service

           └─6851 /usr/sbin/crond -n

 

7 17 20:51:53 hyc-01-01 systemd[1]: Started Command Scheduler.

7 17 20:51:53 hyc-01-01 systemd[1]: Starting Command Scheduler...

7 17 20:51:53 hyc-01-01 crond[6851]: (CRON) INFO (RANDOM_DELAY will be scale....)

7 17 20:51:53 hyc-01-01 crond[6851]: (CRON) INFO (running with inotify support)

7 17 20:51:53 hyc-01-01 crond[6851]: (CRON) INFO (@reboot jobs will be run a....)

7 17 21:04:01 hyc-01-01 crond[6851]: (root) RELOAD (/var/spool/cron/root)

Hint: Some lines were ellipsized, use -l to show in full.

 

启动和停止服务

[root@hyc-01-01 ~]# systemctl stop crond 停止服务

[root@hyc-01-01 ~]# systemctl start crond 启动服务

[root@hyc-01-01 ~]# systemctl restart crond 重启服务

 

检测service是否开机启动

[root@hyc-01-01 ~]# systemctl is-enabled crond

disabled

 

crond实际为一个软链接,源文件在/usr/lib下,链接文件在/etc/systemd

[root@hyc-01-01 ~]# systemctl is-enabled crond

disabled

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

ls: 无法访问/etc/systemd/system/multi-user.target.wants/crond.service: 没有那个文件或目录

[root@hyc-01-01 ~]# systemctl enable crond

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

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

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

 

10.26 unit介绍

 

技术分享图片

 

10.27 target介绍

 

技术分享图片


7.17 10.23-10.27

标签:ash   http   用户模式   apr   解决办法   queue   base   图形化界面   roo   

原文地址:http://blog.51cto.com/12216458/2146133

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!