---
title: "linux加入计划任务"
author: "onlyor"
date: "2012-10-13T16:33:32Z"
tags: []
categories: ["操作系统"]
slug: "linux"
draft: false
lastmod: "2012-12-14T14:20:10Z"
id: 1697854
url: "https://onlyor.iteye.com/blog/1697854"
---

cron 是一个 linux 下的定时执行工具，可以在无需人工干预的情况下运行作业。由于 Cron 是 Linux 的内置服务，但它不自动起来，可以用以下的方法启动、关闭这个服务 :

/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

现在 Cron 这个服务已经在进程里面了，我们就可以用这个服务了。

 

cron 服务提供 crontab 命令来设定 cron 服务的，以下是这个命令的一些参数与说明 :

　　 crontab -u // 设定某个用户的 cron 服务，一般 root 用户在执行这个命令的时候需要此参数

　　 crontab -l // 列出某个用户 cron 服务的详细内容

　　 crontab -r // 删除没个用户的 cron 服务

　　 crontab -e // 编辑某个用户的 cron 服务

 

比如说 root 查看自己的 cron 设置 :crontab -u root -l

再例如， root 想删除 fred 的 cron 设置 :crontab -u fred -r

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

进入 vi 编辑模式，编辑的内容一定要符合下面的格式 :

**\*/60 * * * * top -b -n1|head -10 &gt;&gt; /usr/moniterlog 2&gt;&1**

**(每个小时将top10 追加到文件中)**

 

 

以下有个事例

 

1\. 在命令行中输入

crontab -e

打开一个编辑的区域

 

2 在编辑区域中输入

\*/1 * * * * top -b -n1|head -10 &gt;&gt; /usr/moniterlog 2&gt;&1

（命令的意思为：将每小时记的系统资源占用情况，写入到systemMonitor.log）

 

3 保存退出（依次输入ESC 输入： 输入wq）