`
daoger
  • 浏览: 524557 次
  • 性别: Icon_minigender_1
  • 来自: 山东济南
社区版块
存档分类
最新评论

linux 定时任务

阅读更多
cron是linux下的执行定时任务的工具,对于还没有添加定时任务的系统,初次设置时要在/var/spool/cron/下建立一个文件名和用户名相同的文件;内容示例:
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (/tmp/crontab.9205 installed on Mon Oct 15 10:44:32 2007)
# (Cron version -- $Id: crontab.c,v 2.13 1994/01/17 03:20:37 vixie Exp $)
1 * * * * /syn_time.sh

前半部分是类似于quartz的时间格式,几个时间格式点依次表示:
分钟 (0-59),
小时 (0-23),
月份中的某一天 (1-31),
一年中的某一月 (1-12),
一周中的星期几 (0-6 with 0=Sunday).

后半段是要执行的定时任务文件,例子中该文件是在根目录下。

重启cron服务就可以了:
/sbin/service crond start //启动服务
/sbin/service crond stop //关闭服务
/sbin/service crond restart //重启服务
/sbin/service crond reload //重新载入

也可以将这个服务在系统启动的时候自动启动:

在/etc/rc.d/rc.local这个脚本的末尾加上:
/sbin/service crond start
eg.
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local
/sbin/service crond start



crontab相关命令

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

查看自己的cron设置:crontab -u root -l
删除fred的cron设置:crontab -u fred -r

在编辑cron服务时,编辑的内容有一些格式和约定,输入:crontab -u root -e

进入vi编辑模式,编辑的内容一定要符合下面的格式:*/1 * * * * ls >> /tmp/ls.txt






分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics