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

一、linux任务计划cron
1./etc/crontab  系统级别的任务计划配置文件
[root@lx003 ~]# 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)                     #分钟(0-50)
# |  .------------- hour (0 - 23)                           #小时(0-23)
# |  |  .---------- day of month (1 - 31)              #天(1-31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...    #月份(1-12)
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat   #星期(0-7)
# |  |  |  |  |
# *  *  *  *  * user-name            command to be executed
      用户名,默认当前用户            命令
2.  crontab -e   编辑任务计划,
0 */2 1-10 12 3,5 /usr/bin/local/wq.sh >>/tmp/1.txt 2>>/tmp/1.txt

这个任务的意思是,12月的1到10号的每周3和周五,双数点(2,4,6,8......),执行/usr/bin/local /33.sh这个脚本,并把正确和错误的信息都重定向到 /tmp/1.txt 下。
怎么确定年呢,比如一个任务今年执行,明年不执行,可以使用星期来确定唯一性,因为每年同一天的星期几都是不一样的。

4. systemctl start crond   启动任务计划
[root@localhost ~]# systemctl start crond              # 启动任务计划
[root@localhost ~]# ps -aux |grep crond                 #可以查看进程是否启动
root       603  0.0  0.0 126264  1708 ?        Ss   1月27   0:02 /usr/sbin/crond -n
root     10270  0.0  0.0 112680   980 pts/0    S+   07:20   0:00 grep --color=auto crond
5.   systemctl status crond    查看任务计划的状态,启动的时候是 active (running),停止的时候是inactive (dead)
[root@localhost ~]# 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-23 21:52:34 CST; 4 days ago
Main PID: 603 (crond)
   CGroup: /system.slice/crond.service
           └─603 /usr/sbin/crond -n
1月 23 21:52:34 localhost.localdomain systemd[1]: Started Command Scheduler.
1月 23 21:52:34 localhost.localdomain systemd[1]: Starting Command Scheduler...
1月 23 21:52:34 localhost.localdomain crond[603]: (CRON) INFO (RANDOM_DELAY will be scaled with facto...d.)
1月 23 21:52:35 localhost.localdomain crond[603]: (CRON) INFO (running with inotify support)
Hint: Some lines were ellipsized, use -l to show in full.
6.   systemctl stop crond  停止crond服务,
[root@localhost ~]# systemctl stop crond
[root@localhost ~]# systemctl status crond
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since 日 2018-01-28 07:24:15 CST; 15s ago
Main PID: 603 (code=exited, status=0/SUCCESS)
1月 23 21:52:34 localhost.localdomain systemd[1]: Started Command Scheduler.
1月 23 21:52:34 localhost.localdomain systemd[1]: Starting Command Scheduler...
1月 23 21:52:34 localhost.localdomain crond[603]: (CRON) INFO (RANDOM_DELAY will be scaled with facto...d.)
1月 23 21:52:35 localhost.localdomain crond[603]: (CRON) INFO (running with inotify support)
1月 28 07:24:15 localhost.localdomain systemd[1]: Stopping Command Scheduler...
1月 28 07:24:15 localhost.localdomain systemd[1]: Stopped Command Scheduler.
Hint: Some lines were ellipsized, use -l to show in full.
7.  crontab -l     列出任务计划
[root@localhost ~]# crontab -l
0 */2 1-10 12 3,5 /usr/bin/local/wq.sh >>/tmp/1.txt 2>>/tmp/1.txt
8. /var/spool/cron/     用户的任务计划配置文件地址,以用户的名字为文件,当需要备份任务计划的时候,拷贝这个文件就可以
[root@localhost ~]# ls  /var/spool/cron/
root
9.   crontab -r   -u      -r删除任务计划   -u 指定用户
[root@localhost ~]# crontab -u root -r       #指定root用户删除任务计划,当不指定用户的时候,默认当前用户
[root@localhost ~]# crontab -l
no crontab for root

二 、chkconfig工具
1.  chkconfig --list      列出chkconfig管理的服务
[root@localhost ~]# chkconfig --list                                         #centos7不再使用sysV管理服务,而是使用systemd
注:该输出结果只显示 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.  /etc/init.d/   服务的脚本存放在这个目录下
[root@lx003 ~]# ls /etc/init.d/
functions  netconsole  network  README
3.  chkconfig 服务名 off      关闭服务
       chkconfig 服务名on    开启服务
[root@localhost ~]# chkconfig network off           #关闭network服务
[root@localhost ~]# chkconfig --list
netconsole         0:关    1:关    2:关    3:关    4:关    5:关    6:关
network             0:关    1:关    2:关    3:关    4:关    5:关    6:关
[root@localhost ~]# c hkconfig network on           #开启network服务
[root@localhost ~]# chkconfig --list
netconsole          0:关    1:关    2:关    3:关    4:关    5:关    6:关
network              0:关    1:关    2:开    3:开    4:开    5:开    6:关
4.  chkconfig --level 数字 服务名 off on       指定某一个级别开启或关闭
[root@localhost ~]# chkconfig --level 24 network off           #将2、4级别下的network服务关闭
[root@localhost ~]# chkconfig --list
netconsole         0:关    1:关    2:关    3:关    4:关    5:关    6:关
network            0:关    1:关    2:关    3:开    4:关    5:开    6:关

[root@localhost ~]# chkconfig --level 4 network on              #开启4级别下的network服务
[root@localhost ~]# chkconfig --list
netconsole         0:关    1:关    2:关    3:关    4:关    5:关    6:关
network            0:关    1:关    2:关    3:开    4:开    5:开    6:关
5.   chkconfig --add  
    chkconfig --del       要将一个服务添加到chkconfig这个工具,首先要把脚本放到/etc/init.d这个目录下,在使用chkconfig这个命令添加或者删除
[root@localhost init.d]# chkconfig --add 123               #将123添加到服务
[root@localhost init.d]# chkconfig --list
123                0:关    1:关    2:开    3:开    4:开    5:开    6:关
netconsole         0:关    1:关    2:关    3:关    4:关    5:关    6:关
network            0:关    1:关    2:关    3:开    4:开    5:开    6:关

[root@localhost init.d]# chkconfig --del 123             #删除123服务
[root@localhost init.d]# chkconfig --list
netconsole         0:关    1:关    2:关    3:关    4:关    5:关    6:关
network            0:关    1:关    2:关    3:开    4:开    5:开    6:关

三、systemd管理服务
1. 列出所有的服务
  systemctl list-units --all --type=service
2.设置服务开机启动或不启动
[root@lx003 ~]# systemctl enable crond.service               #设置crond服务开机启动
[root@lx003 ~]# systemctl disable crond                             #设置crond服务开机不启动
3. 停止(stop)、开启(start)、重启( restart)  crond服务,查看服务的状态(status)
[root@lx003 ~]# systemctl stop crond              #启动 crond 服务
[root@lx003 ~]# systemctl start crond              #停止 crond 服务
[root@lx003 ~] # systemctl restart crond           #重启 crond 服务
[root@lx003 ~]# systemctl status crond            #查看 crond 服务的状态
4. systemctl is-enabled 服务名           查看一个服务是否开机启动
[root@localhost init.d]# systemctl is-enabled crond
disabled
[root@localhost init.d]# systemctl enable crond.service
[root@localhost init.d]# systemctl is-enabled crond
enabled
5. 当启动服务的时候,会 Created一个软链接,标红色的就是软连接,指向真正的服务地址,当我们disable服务时,会remove软链接。
[root@lx003 ~]# systemctl enable crond
Created symlink from /etc/systemd/system/multi-user.target.wants/crond.service to /usr/lib/systemd/system/crond.service.
[root@lx003 ~]# systemctl disable crond
Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service.

四、 unit介绍
1. 系统的unit都在这个目录下:
/usr/lib/systemd/system
2.   nuit分为以下几种:
service            系统服务 
target             多个unit组成的组
device            硬件设备 
mount            文件系统挂载点
automount     自动挂载点
path                文件或路径 
scope              不是由systemd启动的外部进程
slice                 进程组 
snapshot         systemd快照 
socket             进程间通信套接字
swap               swap文件
timer               定时器
3.unit相关的命令
systemctl list-units            # 列出所有正在运行的unit
systemctl list-units --all    # 列出所有的unit,包括失败的或者inactive的
systemctl list-units --all --state=inactive        # 列出状态为inactive的unit
systemctl list-units --type=service                  # 列出状态为active的service
systemctl is-active crond                                  # 查看crond服务的状态是什么样的

五、target介绍
Target(目标):
在systemd中有一个叫做target的单元,也叫作目标单元。这个单元没有专用的配置选项,它只是以.target结尾的文件,它本身没有具体功能,你可以理解为类别,它的作用就是将一些单元汇聚在一起。
1. 列出系统所有的target
systemctl list-unit-files --type=target
2. 查看一个target里面有哪些unit
systemctl list-dependencies runlevel2.target                #查看 runlevel2.target里面有哪些unit
3. 查看系统默认的target
[root@localhost ~]# systemctl get-default
multi-user.target
4.设置默认的target
[root@lx003 ~]# systemctl set-default multi-user.target                                                #设置默认的target为 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.            #当我们设置默认的target时会重新指定软链接
5.  查看一个service属于哪个target
[root@lx003 ~]# cat /usr/lib/systemd/system/crond.service                   #可以用cat查看service属于那个target
[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                                 #从这里就可以看到属于 multi-user.target这个targe


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值