linux任务计划 在linux中,任务计划是必不可少的,在linux中怎样设置任务计划呢? 首先看一个文件 [root@localhost ~]# 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 (星期,0代表周日,也可以写成7,也可以写英文简写)

| | | | |

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

这就是任务计划的配置文件,在这个配置文件中他会定义几个变量SHELL=/bin/bash、PATH=/sbin:/bin:/usr/sbin:/usr/bin、MAILTO=root(发送邮件谁)

在他下面的是格式。我们可以看到他下面有五个星号,所以分为五个位。

用命令crontab -e,这样就进入了配置文件当中。它的用法和vim相同。

比如现在有一项任务,需要在凌晨3点去执行 0 3 * * * /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log 这里面的“*”代表所有的意思,第一个代表“分”,第二个代表“时”,第三个代表“日”。所以这是每天都执行。 然后后面接一个shell脚本,这个脚本的意思是每天把正确内容追加到一个指定的文件,错误的也追加到指定的文件。 任务计划的格式很简单,分时日月周后接具体的命令。

同时我们也可以用一个范围去执行。 还是上面的那个脚本。 0 3 1-10 */2 2,5 /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log 它的意思是每双月的1-10日凌晨三点执行这个脚本。 但是我们发现这里面没有年份,如果我们想那一年执行一个脚本,这里我们就用星期去确定它的唯一性。比如今年的6月18日和明年的6月18日所在的周肯定也不一样。

想让任务计划crontab正常使用,我们还要去启动服务,那么怎样去启动服务呢? systemctl start crond输入这条命令,他就启动了。 然后我们检查一下他有没有启动 [root@localhost ~]# ps -aux |grep cron root 560 0.0 0.1 126236 1656 ? Ss 22:37 0:00 /usr/sbin/crond -n root 1253 0.0 0.0 112676 984 pts/0 S+ 23:37 0:00 grep --color=auto cron 如果出现了含有cron的字符串,就证明他已经启动了。

我们也可以用systenctl status crond来查看 [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-03-25 22:37:48 CST; 1h 2min ago Main PID: 560 (crond) CGroup: /system.slice/crond.service └─560 /usr/sbin/crond -n

3月 25 22:37:48 localhost.localdomain systemd[1]: Started Command Scheduler. 3月 25 22:37:48 localhost.localdomain systemd[1]: Starting Command Scheduler... 3月 25 22:37:48 localhost.localdomain crond[560]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 18% if used.) 3月 25 22:37:49 localhost.localdomain crond[560]: (CRON) INFO (running with inotify support)

还有一种情况,写了一个计划,放到了配置文件中,但是没有执行,这可能是写的脚本中,用的是一个命令,而没有用绝对路径,这就容易造成命令不执行的现象。因为你用的这个命令并没有在他的PATHZ中,所以解决的方法是要么写绝对路径,要么就将命令的路径加入到PATH。

还有一个建议就是,我们每写一个任务计划,后面就追加一个任务日志,正确输出和错误输入都要写上,这样才能保证我们的任务有据可查,如果不执行,是不是有错误,我么只要查看任务日志就能知晓。

任务日志的备份 [root@localhost ~]# crontab -l 1 10 * 2 * /usr/bin/find /tmp/ -type f -mtime +100 |xargs rm -f 这样在我们写完任务计划后就能查看了。 它的问价在/var/spool/cron/这里面会有对应用户的crond,他会以用户名字作为结尾,形成一个文件。所以我们如果要备份,直接备份这个文件就可以,或者将这个目录整个拷贝一下就行。

如果我们想删除一个任务计划,就输入crontab -r 我们也可以指定一个用户,比如我们指定root用户。crontab -u root -l

linux系统服务管理-chkconfig linux中有很多服务,比如iptables、crontab、firewalld等等都是服务,这么多的服务就需要有一个工具去管理。管理他怎样启动,怎样开机启动,怎么样让他在指定的级别启动。

列出chkconfig所有服务 chkconfig --list [root@linletao-001 ~]# 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:关

chkconfig服务变更 chkconfig 服务名 off/on(关闭或者打开) 比如我们现在要关闭chkconfig中的network chkconfig --list [root@linletao-001 ~]# chkconfig --list 然后我们查询一下 chkconfig --list network 0:关 1:关 2:关 3:关 4:关 5:关 6:关 这样在6个级别就全部关闭network了。 其中这6个数字的的含义是:0是关机状态,1是单用户状态,2是多用户,但是没有NFS、3是多用户模式,4是暂时没用,5是多用户的图形模式,6是重启。

指定某一个级别的关闭或者开启。 例如我们把3级别关闭 [root@linletao-001 ~]# chkconfig --level network 3 off 然后我们查询一下 [root@linletao-001 ~]# chkconfig --list network 0:关 1:关 2:开 3:关 4:开 5:开 6:关 这样network在3级别就关闭了。

如果我想在多个级别上关闭服务,就直接将级别的序号写上,必要用“,”隔开 [root@linletao-001 ~]# chkconfig --level 35 network off [root@linletao-001 ~]# chkconfig --list

network 0:关 1:关 2:开 3:关 4:开 5:关 6:关 这样就在3,5级别将network关闭了。

将脚本加入到服务列表中 chkconfig --add 脚本名 比如我们要将123加入到chkconfig的服务列表中 [root@linletao-001 init.d]# chkconfig --add 123 然后我们查看一下 123 0:关 1:关 2:开 3:开 4:开 5:开 6:关 netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关 network 0:关 1:关 2:开 3:关 4:开 5:关 6:关 这样就添加了一项服务。

这时怎样做到的呢?首先我们要将服务脚本放到init.d这个目录下,只有放在这个目录中,我们才能添加到服务中去。名字无所谓,但是文件内容有格式。

#! /bin/bash(必须是shell脚本)

network Bring up/down networking

chkconfig: 2345 10 90(第一组数字是运行级别,第二组是运行级别启动顺序,第三组是关闭顺序)

description: Activates/Deactivates all network interfaces configured to \

start at boot time.(描述)

以上的内容是脚本必须要有的

将脚本从chkconfig列表中删除。 chkconfig --del 我们还用123做例子 [root@linletao-001 init.d]# chkconfig --del 123 然后查询 netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关 network 0:关 1:关 2:开 3:关 4:开 5:关 6:关 这样名为123的服务就没有了。

linux系统管理服务-systemd 它是centos7的管理机制。centos6主要使用chkconfig。

查看所有服务 systemctl list-unit-files [root@linletao-001 init.d]# systemctl list-unit-files|head -10 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 这样就查看了所有服务

还有回个命令,也可以查看,而且更加的清晰。 [root@linletao-001 init.d]# systemctl list-units --all --type=service|head -5 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 related settings

开机启动 systemctl enable 服务名

开机不启动 systemctl disable 服务名

查看状态 systemctl status crond

启动服务 systemctl status 文件名

停止服务 systemctl stop 文件名

检查服务是否开机启动 systemctl is-enabled 文件名

unit的概念 systemd开启和监督整个系统是基于unit的概念。unit是由一个与配置文件名同名的名字和类型组成的(例如:avahi.service unit有一个具有相同名字的配置文件,它是守护进程avahi的一个封装单元)。

主要分类: service 系统服务
target 多个unit组成的组 device 硬件设备 mount 文件系统挂载点 automount 自动挂载点 path 文件或路径 scope 不是由systemd启动的外部进程 slice 进程组 snapshot systemd快照 socket 进程间通信套接字 swap swap文件 timer 定时器

unit相关的命令

列出正在运行的unit systemctl list-units

列出所有的unit,包括失败的或者inactive的 systemctl list-units -all

列出inactive的unit systemctl list-units --all --state=inactive

列出状态为active的service systemctl list-units --type=service

查看某个服务是否为active systemctl is-active crond.service

target概念 target就是多个unit的组合,​系统为了方便管理用target来管理unit。

列出系统中所有的target systemctl list-unit-files --type=target

UNIT FILE STATE
basic.target static
bluetooth.target static
cryptsetup-pre.target static
cryptsetup.target static

查看指定target下面有哪些unit systemctl list-dependencies multi-user.target

查看系统默认的target systemctl get-default [root@linletao-001 init.d]# systemctl get-default multi-user.target 这既是系统默认的target

设置默认target systemctl set-default multi-user.target