Linux系统使用--定时关机的实现以及crontab命令和文件详解

一、前言

为了以后不用再半夜跑回实验室关电脑,昨天中午花了两个小时左右的时间搞了一下如何实现Linux系统的定时关机,结果没搞出来。在老师的指点下得以解决,写一博客分享之。
另,发现现在网上的资料越发的不靠谱了,昨日查阅了那么多资料都没有解决问题。

本博文主要内容如下:

  1. Linux16.04系统定时关机的实现;
  2. crontab命令和文件的相关解析。

二、定时关机的实现

废话不多说,直接点明解决方法,具体解析见下一目录。
傻瓜式解决问题,步骤如下:

2.1 查看/etc/crontab文件

进入/etc目录

cd /etc

查看crontab文件

vi crontab

文件内容如下:

# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user  command
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#下面这一行是定时关机的实现
55 22   * * *   root    /sbin/shutdown -h now
#

(红框中为个人添加内容)
截图如下:
crontab

2.2 修改crontab文件

使用root权限打开并修改crontab文件。

sudo vi crontab

添加你要实现的定时关机操作
例如,我想在每天22时55分定时关机:

55 22 * * * root /sbin/shutdown -h now

具体格式如下:

m + h + dom + mon + dow + user + command

解析:分钟 + 小时 + 日 + 月 + 星期 + 具体操作(该操作可以是执行某条命令也可以是执行某个脚本)

三、crontab命令详解

3.1 crond简介

crond 是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务 工具,并且会自动启动crond进程,crond进程每分钟会定期检查是否有要执行的任务,如果有要执行的任务,则自动执行该任务。

3.2 crontab命令详解

3.2.1 crontab命令

linux 系统则是由 cron (crond) 这个系统服务来控制的。Linux 系统上面原本就有非常多的计划性工作,因此这个系统服务是默认启动的。另 外, 由于使用者自己也可以设置计划任务,所以, Linux 系统也提供了使用者控制计划任务的命令 :crontab 命令。

3.2.2 命令解析

1.命令格式:

crontab [-u user] file

crontab [-u user] [ -e | -l | -r ]

2.命令功能:

通过crontab 命令,我们可以在固定的间隔时间执行指定的系统指令或 shell script脚本。时间间隔的单位可以是分钟、小时、日、月、周及以上的任意组合。这个命令非常设合周期性的日志分析或数据备份等工作。

3.命令参数:

-u user:用来设定某个用户的crontab服务,例如,“-u ixdba”表示设定ixdba用户的crontab服务,此参数一般有root用户来运行。

file:file是命令文件的名字,表示将file做为crontab的任务列表文件并载入crontab。如果在命令行中没有指定这个文件,crontab命令将接受标准输入(键盘)上键入的命令,并将它们载入crontab。

-e:编辑某个用户的crontab文件内容。如果不指定用户,则表示编辑当前用户的crontab文件。

-l:显示某个用户的crontab文件内容,如果不指定用户,则表示显示当前用户的crontab文件内容。

-r:从/var/spool/cron目录中删除某个用户的crontab文件,如果不指定用户,则默认删除当前用户的crontab文件。

-i:在删除用户的crontab文件时给确认提示。

3.3 crontab文件详解

Linux下的任务调度分为两类,系统任务调度和用户任务调度。

3.3.1 系统任务

系统任务调度是指系统需要周期性进行的任务操作,具有周期性和强制性。
系统任务在/etc目录下的crontab文件中,如上文所示。
文件内容:

# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user  command
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#

文件简析:
a.环境变量配置
前两行是用来配置crond任务运行的环境变量,第一行SHELL变量指定了系统要使用哪个shell,这里是sh,第二行PATH变量指定了系统执行 命令的路径主要有/usr/local/sbin、/usr/local/bin、/sbin、/bin、/usr/sbin、/usr/bin等路径。
b.任务详解
文件正文是该系统的系统任务调度,每一行都是一个任务。

17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
3.3.2 用户任务调度

1.使用命令为你要某个用户添加的操作。

sudo crontab -u username -e

然后保存退出。
截图如下:
edit
2.启动cron
命令

/etc/init.d/cron start

root

3.4 如何使用crontab命令设立定时任务

使用root在/etc/crontab文件下添加你要执行的定时任务即可。
任务格式:m + h + dom + mon + dow + user + command(分钟 + 小时 + 日 + 月 + 星期 + 具体操作)
其中:
minute: 表示分钟,可以是从0到59之间的任何整数。
hour:表示小时,可以是从0到23之间的任何整数。
day:表示日期,可以是从1到31之间的任何整数。
month:表示月份,可以是从1到12之间的任何整数。
week:表示星期几,可以是从0到7之间的任何整数,这里的0或7代表星期日。
command:要执行的命令,可以是系统命令,也可以是自己编写的脚本文件。

四、个人总结

1.如何更好的使用/etc/crontab文件,使系统任务更加高效调理
2.如何将任务的执行结果及时有效地反馈给用户

五、参考资料

http://www.cnblogs.com/xiaoluo501395377/archive/2013/04/06/3002602.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值