您的位置:首页 > 运维架构 > Linux

linux任务计划,linux服务管理

2018-03-28 09:05 302 查看

linux任务计划

linux任务计划使用crontab命令来完成的。定期自动执行某一脚本。 
命令:crontab 
选项: 
-u 表示指定用户,不加-u 选项则是当前用户 
-e 表示制定任务计划 
-l 表示列出计划任务 
-r 表示删除计划任务编写任务计划的格式:
[root@shuai-01 ~]# 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分的范围:0-59 (* 表示所有) 
时的范围:0-23 (* 表示所有) 
日的范围:1-31 (* 表示所有) 
月的范围:1-12 (* 表示所有) 
周的范围:0-6(Sunday=0 or 7) (* 表示所有)例子:每天三点,执行/usr/local/sbin/123.sh脚本,对的日志输入到/tmp/123.log下,错的日志输入到/tmp/123.log下
0 3 * * *  /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log
1
2
编写完后,要想执行,需要启动任务计划。启动任务计划:
[root@shuai-01 ~]# systemctl start crond
查看任务计划是否启动:
[root@shuai-01 ~]# ps aux |grep cron
root        563  0.0  0.1 126232  1664 ?        Ss   04:59   0:01 /usr/sbin/crond -n
root       3164  0.0  0.0 112676   976 pts/0    S+   08:57   0:00 grep --color=auto cron

[root@shuai-01 ~]# systemctl status crond
● crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
Active: active (running) since 日 2017-12-03 04:59:21 CST; 3h 58min ago
Main PID: 563 (crond)
CGroup: /system.slice/crond.service
└─563 /usr/sbin/crond -n

12月 03 04:59:21 shuai-01 systemd[1]: Started Command Scheduler.
12月 03 04:59:21 shuai-01 systemd[1]: Starting Command Scheduler...
12月 03 04:59:22 shuai-01 crond[563]: (CRON) INFO (RANDOM_DELAY will be s...)
12月 03 04:59:24 shuai-01 crond[563]: (CRON) INFO (running with inotify s...)
Hint: Some lines were ellipsized, use -l to show in full.
任务日志保存的文件:/var/spool/cron/usernameps:如果发现任务计划也写了,也输入了执行命令。但是就是服务没启动。这是可能是你写的脚本有问题,最有可能的是,脚本命令路径有问题。写任务计划是,最好将对的情况错的情况都输入到任务日志中。

linux系统服务管理-chkconfig

chkconfig是centos6的系统服务管理工具。centos7虽然保留了chkconfig,但是保留的能管理服务已经很少了。 
预设的服务能在/etc/init.d中看到:
[root@shuai-01 ~]# ls /etc/init.d
functions  netconsole  network  README
列出其管理的服务及每个级别的状态:
[root@shuai-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表示系统的运行级别更改某级别下的开启状态:
[root@shuai-01 ~]# chkconfig --list |grep network

注意:该输出结果只显示 SysV 服务,并不包含原生 systemd 服务。SysV 配置数据可能被原生 systemd 配置覆盖。
如果您想列出 systemd 服务,请执行 'systemctl list-unit-files'。
欲查看对特定 target 启用的服务请执行
'systemctl list-dependencies [target]'。

network         0:关 1:关 2:开 3:关 4:开 5:开 6:关
加某个服务到系统服务:
将服务文件加到/etc/init.d文件下
[root@shuai-01 init.d]# cp network 123 
[root@shuai-01 init.d]# ls 
123 functions netconsole network README

chkconfig添加服务
[root@shuai-01 init.d]# chkconfig --add 123
[root@shuai-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:关

删除某个服务:
[root@shuai-01 init.d]# chkconfig --del 123

linux服务管理–systemd

systemd是centos7服务管理机制。列出全部服务:
[root@shuai-01 ~]# systemctl list-unit-files
列出所有service服务(既包含运行的有包含不运行的):
[root@shuai-01 ~]# systemctl list-units --all --type=service
给服务(crond)设置开机启动(通过创建软连接):
[root@shuai-01 ~]# systemctl enable crond
设置开机不启动:
[root@shuai-01 ~]# systemctl disable crond
Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service.
查看服务状态:
[root@shuai-01 ~]# systemctl status crond
● crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; disabled; vendor preset: enabled)
Active: active (running) since 日 2017-12-03 04:59:21 CST; 11h ago
Main PID: 563 (crond)
CGroup: /system.slice/crond.service
└─563 /usr/sbin/crond -n

12月 03 04:59:21 shuai-01 systemd[1]: Started Command Scheduler.
12月 03 04:59:21 shuai-01 systemd[1]: Starting Command Scheduler...
12月 03 04:59:22 shuai-01 crond[563]: (CRON) INFO (RANDOM_DELAY will be s...)
12月 03 04:59:24 shuai-01 crond[563]: (CRON) INFO (running with inotify s...)
Hint: Some lines were ellipsized, use -l to show in full.
启动服务:
[root@shuai-01 ~]# systemctl start crond
停止服务:
[root@shuai-01 ~]# systemctl stop crond
重启服务:
[root@shuai-01 ~]# systemctl restart crond
查看是否开机启动:
[root@shuai-01 ~]# systemctl is-enabled crond
disabled
查看是否运行:
[root@shuai-01 ~]# systemctl is-active crond
active
系统的所有unit分为以下类型 


 
service : 表示系统服务 
target : 多个unit组成的组unit相关的命令:
列出正在运行的unit:
[root@shuai-01 system]# systemctl list-units

列出所有的unit:
[root@shuai-01 system]# systemctl list-units --all

列出inactive的unit
[root@shuai-01 system]# systemctl list-units --all --state=inactive

列出状态为active的service
[root@shuai-01 system]# systemctl list-units --type=active

查看某个服务是否为active:
[root@shuai-01 system]# systemctl is-active crond.service

系统为了方面管理。用target来管理unit。(target是多个unit和service组成的组)查看系统的所有target:
[root@shuai-01 system]# systemctl list-unit-files --type=target
查看特定的target(multi-user.target)下有那些unit:
[root@shuai-01 system]# systemctl list-dependencies multi-user.target
查看系统默认的target:
[root@shuai-01 system]# systemctl get-default
multi-user.target
设置默认的target:
[root@shuai-01 system]# systemctl set-default multi-user.target
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: