六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 30|回复: 0

linux 定时任务

[复制链接]

升级  29.33%

24

主题

24

主题

24

主题

秀才

Rank: 2

积分
94
 楼主| 发表于 2013-1-27 05:20:41 | 显示全部楼层 |阅读模式
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
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表