想让Nutch定时抓取某一网站内容,cron正是解决这个问题的工具。
cron是linux下定时执行工具,可在完全无人工干预的情况下作业。就像windows中的“任务计划”功能一样
在Ubuntu server 10.04下,cron是被默认安装并启动的。通过/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 )
#
-----------------------------------------------------------------------------------------------------------------------
ununtu 通过调用 run-parts 命令,定时运行四个目录下的所有脚本。
1)/etc/cron.hourly,目录下的脚本会每个小时让执行一次,在每小时的17分钟时运行;
2)/etc/cron.daily,目录下的脚本会每天让执行一次,在每天的6点25分时运行;
3)/etc/cron.weekly,目录下的脚本会每周让执行一次,在每周第七天的6点47分时运行;
4)/etc/cron.mouthly,目录下的脚本会每月让执行一次,在每月1号的6点52分时运行;
当然,以上的时间均是系统默认时间,可以根据自己的需求进行修改。
虽然是默认开启的,但是也可以用命令来手动关闭和重启该任务,crontab的启动服务放在/etc/init.d/cron
sudo /etc/initd./cron start 启动cron服务
sudo /etc/initd./cron stop 关闭cron服务
sudo /etc/initd./cron restart 重启cron服务
/etc/crontab文件语法如下:
Minute Hour Day Month Dayofweek user command
分钟 小时 天 月 天每星期 用户 命令
“ * ” ,代表所有的取值范围内的数字;
" / ", 代表"每"(“*/5”,表示每5个单位);
" - ", 代表从某个数字到某个数字(“1-4”,表示1-4个单位);
" , ", 分开几个离散的数字;
举例如下:
5 * * * * ls // 指定每小时的第5分钟执行一次ls命令
30 5 * * * ls // 指定每天的 5:30 执行ls命令
30 7 8 * * ls // 指定每月8号的7:30分执行ls命令
50 7 * * * root run-parts/etc/cron.daily // 每天7:50以root 身份执行/etc/cron.daily目录中的所有可执行文件
使用crontab -e命令添加自定义的任务,执行crontab -e的时候如果你是第一次执行这个命令,那么它会让你选择编辑器,选vi,如果你选择了之后想更改编辑器的时候可以用 sudo update-alternatives --config editor 这样再重新选择编辑器,重新再选择就好了
1)直接执行命令行
每2分钟打印一个字符串“Hello World”,保存至文件/home/test/demo/HelloWorld.txt中,cron 格式如下:
*/2 * * * * echo "Hello World." >> /home/test/demo/HelloWorld.txt
不用重启cron服务,过两分种就可以在/home/test/demo目录下看到HelloWorld.txt了
2)shell 文件
每3分钟调用一次 /home/test/demo/test.sh 文件,cron 格式如下:
# */3 * * * * /home/test/demo/test.sh //注意这里,网上都是这样写的,可是在我机器上根本不执行,需要加上sh命令才能执行该脚本
*/3 * * * * sh /home/test/demo/test.sh //不加sh也可以,但需要将test.sh改为可执行权限
文件 /home/test/demo/test.sh 的内容如下:
-----------------------------------------------------------------------------------------------------------------------
#!/bin/sh //这个头加不加,都是可以执行的
cd /home/laigw/cron
echo "shell" >> shell.txt
-----------------------------------------------------------------------------------------------------------------------
参考资料:
1.http://www.cnw.com.cn/software-bi/htm2010/20100831_205988.shtml
2.http://hi.baidu.com/huangyunjun999/blog/item/f6d17d8227d3da88f703a6d7.html
3.http://ghj19850926.blog.163.com/blog/static/1859156020114252033265/
4.http://blog.sina.com.cn/s/blog_461c24d50100hvaz.html
5.http://blog.sina.com.cn/s/blog_64492fe10100ql9w.html