系统服务管理

1、任务计划

cron是一个linux下的定时执行工具(相当于windows下的scheduled task),可以在无需人工干预的情况下定时地运行任务task。cron服务提供crontab命令来设定cron服务的。

▎常用参数:

命令作用
crontab -u设定某个用户的cron服务,一般root用户在执行这个命令的时候需要此参数 
crontab -l列出某个用户cron服务的详细内容 
crontab -r

删除某个用户的cron服务 

crontab -e编辑某个用户的cron服务  


▎crontab格式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@juispan ~] # 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

格式:分 时 日 月 周 user command

每个用户的任务计划文件存在/var/spool/cron/目录下同名的文件里

分范围0-59,时范围0-23,日范围0-31,月范围0-12,周0-6

可用格式1-5表示一个范围1到5

可用格式1,2,3表示1或者2或者3

可用格式*/2表示被2整除的数字,比如小时,就指每隔2小时

要保证服务是启动状态:

  ①systemctl start crond            启动服务

  ②systemctl status crond.service   查看状态


测试示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[root@juispan ~] # crontab -e
0 5 1-15 * /2  1,3,5  /usr/local/sbin/1 .sh   
                         ##0分,5点,1-15日,双数月份,周一,周三,周五 执行脚本1.sh
[root@juispan ~] # crontab -l
0 5 1-15 * /2  1,3,5  /usr/local/sbin/1 .sh
[root@juispan ~] # crontab -u juispan -l
0 1 1-5 * *  /tmp/123 .py[root@juispan ~] # crontab -e0 5 1-15 */2 1,3,5 /usr/local/sbin/1.sh   
                         ##0分,5点,1-15日,双数月份,周一,周三,周五 执行脚本1.sh
[root@juispan ~] # crontab -l
0 5 1-15 * /2  1,3,5  /usr/local/sbin/1 .sh
[root@juispan ~] # crontab -u juispan -l
0 1 1-5 * *  /tmp/123 .py
[root@juispan ~] # crontab -r
[root@juispan ~] # crontab -l
no  crontab  for  root
[root@juispan ~] # systemctl start crond
[root@juispan ~] # systemctl status crond.service
● crond.service - Command Scheduler
    Loaded: loaded ( /usr/lib/systemd/system/crond .service; enabled; vendor preset: enabled)
    Active: active (running) since 五 2017-07-14 22:37:31 CST; 6h ago
  Main PID: 495 (crond)
    CGroup:  /system .slice /crond .service
            └─495  /usr/sbin/crond  -n
 
7月 14 22:37:31 juispan systemd[1]: Started Command Scheduler.
7月 14 22:37:31 juispan systemd[1]: Starting Command Scheduler...
7月 14 22:37:31 juispan crond[495]: (CRON) INFO (RANDOM_DELAY will be scal....)
7月 14 22:37:33 juispan crond[495]: (CRON) INFO (running with inotify support)
Hint: Some lines were ellipsized, use -l to show  in  full.


2、开机启动

1)chkconfig

chkconfig是CentOS 7以前用的系统服务管理工具。作为过渡,CentOS 7中还存在着。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
[root@juispan ~] # ls /etc/init.d          ##存放服务的目录
functions  netconsole  network  README
[root@juispan ~] # 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@juispan ~] # chkconfig --level 3 network off  ##关闭3模式下的network服务
[root@juispan ~] # 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@juispan ~] # chkconfig --level 345 network off  ##关闭345模式下的network服务
[root@juispan ~] # 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@juispan ~] # chkconfig --del network   ##删除network服务
[root@juispan ~] # chkconfig --list
 
注意:该输出结果只显示 SysV 服务,并不包含原生 systemd 服务。SysV 配置数据可能被原生 systemd 配置覆盖。
       如果您想列出 systemd 服务,请执行  'systemctl list-unit-files'
       欲查看对特定 target 启用的服务请执行
       'systemctl list-dependencies [target]'
 
netconsole      0:关    1:关    2:关    3:关    4:关    5:关    6:关
[root@juispan ~] # chkconfig --add network   ##增加network服务
[root@juispan ~] # 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@juispan ~] # chkconfig network off     ##关闭network服务
[root@juispan ~] # 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)systemd

systemctl是systemd下的一个工具。该命令是用来替代service和chkconfig两个命令的。在systemd的管理体系里面,以前的运行级别(runlevel)的概念被新的运行目标(target)所取代。tartget的命名类似于multi-user.target等这种形式,比如原来的运行级别3(runlevel3)就对应新的多用户目标(multi-user.target),run level 5就相当于graphical.target。

▎systemd:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
[root@juispan ~] # systemctl enable crond.service  ##让服务开机启动
[root@juispan ~] # systemctl status crond    ##查看状态
● crond.service - Command Scheduler
    Loaded: loaded ( /usr/lib/systemd/system/crond .service; enabled; vendor preset: enabled)
    Active: active (running) since 五 2017-07-14 22:37:31 CST; 7h ago
  Main PID: 495 (crond)
    CGroup:  /system .slice /crond .service
            └─495  /usr/sbin/crond  -n
 
7月 14 22:37:31 juispan systemd[1]: Started Command Scheduler.
7月 14 22:37:31 juispan systemd[1]: Starting Command Scheduler...
7月 14 22:37:31 juispan crond[495]: (CRON) INFO (RANDOM_DELAY will be scal....)
7月 14 22:37:33 juispan crond[495]: (CRON) INFO (running with inotify support)
Hint: Some lines were ellipsized, use -l to show  in  full.  
[root@juispan ~] # systemctl disable crond         ##不让开机启动
[root@juispan ~] # systemctl status crond
● crond.service - Command Scheduler
    Loaded: loaded ( /usr/lib/systemd/system/crond .service; disabled; vendor preset: enabled)
    Active: active (running) since 五 2017-07-14 22:37:31 CST; 7h ago
  Main PID: 495 (crond)
    CGroup:  /system .slice /crond .service
            └─495  /usr/sbin/crond  -n
 
7月 14 22:37:31 juispan systemd[1]: Started Command Scheduler.
7月 14 22:37:31 juispan systemd[1]: Starting Command Scheduler...
7月 14 22:37:31 juispan crond[495]: (CRON) INFO (RANDOM_DELAY will be scal....)
7月 14 22:37:33 juispan crond[495]: (CRON) INFO (running with inotify support)
Hint: Some lines were ellipsized, use -l to show  in  full.
[root@juispan ~] # systemctl stop crond      ##停止服务
[root@juispan ~] # systemctl status crond
● crond.service - Command Scheduler
    Loaded: loaded ( /usr/lib/systemd/system/crond .service; disabled; vendor preset: enabled)
    Active: inactive (dead)
 
7月 14 22:37:31 juispan systemd[1]: Started Command Scheduler.
7月 14 22:37:31 juispan systemd[1]: Starting Command Scheduler...
7月 14 22:37:31 juispan crond[495]: (CRON) INFO (RANDOM_DELAY will be scal....)
7月 14 22:37:33 juispan crond[495]: (CRON) INFO (running with inotify support)
7月 15 06:01:11 juispan systemd[1]: Stopping Command Scheduler...
7月 15 06:01:11 juispan systemd[1]: Stopped Command Scheduler.
Hint: Some lines were ellipsized, use -l to show  in  full.
[root@juispan ~] # systemctl start crond       ##启动服务
[root@juispan ~] # systemctl restart crond     ##重启服务
[root@juispan ~] # systemctl status crond
● crond.service - Command Scheduler
    Loaded: loaded ( /usr/lib/systemd/system/crond .service; disabled; vendor preset: enabled)
    Active: active (running) since 六 2017-07-15 06:02:14 CST; 7s ago
  Main PID: 5962 (crond)
    CGroup:  /system .slice /crond .service
            └─5962  /usr/sbin/crond  -n
 
7月 15 06:02:14 juispan systemd[1]: Started Command Scheduler.
7月 15 06:02:14 juispan systemd[1]: Starting Command Scheduler...
7月 15 06:02:14 juispan crond[5962]: (CRON) INFO (RANDOM_DELAY will be sca....)
7月 15 06:02:14 juispan crond[5962]: (CRON) INFO (running with inotify support)
7月 15 06:02:14 juispan crond[5962]: (CRON) INFO (@reboot jobs will be run....)
Hint: Some lines were ellipsized, use -l to show  in  full.
[root@juispan ~] # systemctl is-enabled crond  ##检查服务是否开机启动
disabled


▎unit:

一个service属于一种类型的unit。

1
2
3
4
5
6
7
8
9
10
[root@juispan ~] # systemctl list-units  ##列出正在运行的unit
 
[root@juispan ~] # systemctl list-units --all  ##列出所有,包括失败的或者inactive的
 
[root@juispan ~] # systemctl list-units --all --state=inactive  ##列出inactive的unit
 
[root@juispan ~] # systemctl list-units --type=service  ##列出状态为active的service
 
[root@juispan ~] # systemctl is-active crond.service  ##查看某个服务是否为active
active


▎target:

多个unit组成了一个target,一个target里面包含了多个service。系统为了方便管理用target来管理unit。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
[root@juispan ~] # systemctl list-unit-files --type=target
UNIT FILE                 STATE
basic.target              static
bluetooth.target          static
cryptsetup-pre.target     static
......
[root@juispan ~] # systemctl list-dependencies multi-user.target
multi-user.target                                ##查看指定target下有哪些unit
● ├─auditd.service
● ├─brandbot.path
......
[root@juispan ~] # 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.
[root@juispan ~] # systemctl get-default  ##查看系统默认的target
multi-user.target
[root@juispan ~] # 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=forking
PIDFile= /var/run/sshd .pid
EnvironmentFile= /etc/sysconfig/sshd
ExecStart= /usr/sbin/sshd  $OPTIONS
ExecReload= /bin/kill  -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s
 
[Install]
WantedBy=multi-user.target





















本文转自Grodd51CTO博客,原文链接:http://blog.51cto.com/juispan/1948762 ,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值