阿布云

你所需要的,不仅仅是一个好用的代理。

[crontab]linux下简单的调度配置及使用

阿布云 发表于

介绍

虽然在几篇博客之前介绍了Dagobah这个调度框架,不过对于一些固定调度,很稳定的那种,可以考虑使用linux内置调度器,crontab ,这是系统内置的,相当稳定 ,不用人工干预

优点

可以实现不同的用户调度不同的东西,cron 表达式功能强大,很容易写出符合你需求的周期表达式

使用

登录后直接使用crontab -e 这个时候会要你选一个编辑器,对于我来说vim够用了,然后我们来看看crontab 的里面的内容

 

# Edit this file to introduce tasks to be run by cron. # # Each task to run has to be defined through a single line # indicating with different fields when the task will be run # and what command to run for the task # # To define the time you can provide concrete values for # minute (m), hour (h), day of month (dom), month (mon), # and day of week (dow) or use '*' in these fields (for 'any').# # Notice that tasks will be started based on the cron's system # daemon's notion of time and timezones. # # Output of the crontab jobs (including errors) is sent through # email to the user the crontab file belongs to (unless redirected). # # For example, you can run a backup of all your user accounts # at 5 a.m every week with: # 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/ # # For more information see the manual pages of crontab(5) and cron(8) # # m h dom mon dow command

        # Edit this file to introduce tasks to be run by cron.

        #

        # Each task to run has to be defined through a single line

        # indicating with different fields when the task will be run

        # and what command to run for the task

        #

        # To define the time you can provide concrete values for

        # minute (m), hour (h), day of month (dom), month (mon),

        # and day of week (dow) or use '*' in these fields (for 'any').#

        # Notice that tasks will be started based on the cron's system

        # daemon's notion of time and timezones.

        #

        # Output of the crontab jobs (including errors) is sent through

        # email to the user the crontab file belongs to (unless redirected).

        #

        # For example, you can run a backup of all your user accounts

        # at 5 a.m every week with:

        # 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/

        #

        # For more information see the manual pages of crontab(5) and cron(8)

        #

        # m h  dom mon dow   command

 

 

都是一些介绍和例子,以及解释说明的 ,注意crontab是使用cron表达式,所以你必须了解一些基本的语法,比如我要每隔一分钟运行一个命令

 

*/1 * * * * echo hello >> ~/echo.log

*/1 * * * * echo hello >> ~/echo.log

 

 

写好之后保存,就可以正常使用了 ,这样就会每隔一分钟写入到echo.log文件,相当方便

脚本

如果你有一些其它的task,尽量都写成脚本,另外,脚本的权限一定要是可执行的,以定时run一个echo.sh 为例子

 

*/1 * * * * /root/echo.sh

    */1 * * * *  /root/echo.sh

 

 

另外还要将echo.sh文件更新权限(这个坑会坑到很多人,包括我,后来在日志里面也没有反应出来)

 

chmod +x /root/echo.sh

    chmod +x /root/echo.sh

 

 

还有一个是写完之后记得重启一下service

 

service cron restart

    service cron restart

 

 

日志

日志存在 /var/log/syslog ,每次调度你都会看到,不过这个日志略坑,没有失败的相关信息,还得自己猜

参考

  • http://askubuntu.com/questions/350861/how-to-set-a-cron-job-to-run-a-shell-script
  • https://help.ubuntu.com/community/CronHowto
  • 文章来源:http://brucedone.com/archives/985
  • 阿布云代理提供海量极速高匿名HTTPS代理IP,HTTP代理、HTTPS代理、SOCKS代理、动态代理、爬虫代理等专业动态代理IP服务,阿布云因为专业,所以简单