在VPS上使用Crontab的几点注意事项


昨天晚上快8点的时候设置了几个crontab定时任务,本以为今天上班的时候就可以看到效果,但是,来了之后发现,根本就没有任何内容o(╯□╰)o

调试了好久(ps/lsof)也没发现问题所在;最后的最后,看看date命令的时间,才发现:

# date
Thu Nov 27 03:51:03 EST 2014
# date -R
Thu, 27 Nov 2014 03:52:40 -0500

本地的时间、时区为:

# date
Thu Nov 27 17:06:13 HKT 2014
# date -R
Thu, 27 Nov 2014 17:06:16 +0800

两者之间相隔了13个小时,我勒个去,怪不得在结果文件夹中时不时冒出来一个文件,但是使用ps命令又无法找到对应在运行的进程,弄得我以为活见鬼了o(╯□╰)o

使用国外的VPS一定要注意一下时区的不同,免得设置crontab任务时出现失误o(╯□╰)o

更改Ubuntu上的系统时区的2个方法(tzselect/dpkg-reconfigure tzdata):

# tz[Tab..]
tzconfig tzselect

# tzselect
Please identify a location so that time zone rules can be set correctly.
Please select a continent, ocean, "coord", or "TZ".
1) Africa
2) Americas
3) Antarctica
4) Arctic Ocean
5) Asia
6) Atlantic Ocean
7) Australia
8) Europe
9) Indian Ocean
10) Pacific Ocean
11) coord - I want to use geographical coordinates.
12) TZ - I want to specify the time zone using the Posix TZ format.
#?

# tzconfig
WARNING: the tzconfig command is deprecated, please use:
dpkg-reconfigure tzdata

# dpkg-reconfigure tzdata #打开交互式界面来更改系统时区

调整好了时区之后记得重启cron服务,否则不会生效的(Also be sure to restart cron as it won’t pick up the timezone change and will still be running on UTC.):

$ /etc/init.d/cron stop
$ /etc/init.d/cron start

另一个要注意的地方就是,记得在crontab任务里面设置log,即,对执行的任务打印日志,方便出问题的时候进行分析。这里需要注意的就是:

“2>&1 > file”和 “> file 2>&1″的区别

其他的想到了之后再补充。

 

参考链接:

《 “在VPS上使用Crontab的几点注意事项” 》 有 3 条评论

  1. 在CentOS 7系统上修改主机名(hostname):
    http://ask.xmodulo.com/change-hostname-centos-rhel-7.html
    https://www.cyberciti.biz/faq/rhel-redhat-centos-7-change-hostname-command/
    http://www.itzgeek.com/how-tos/linux/centos-how-tos/change-hostname-in-centos-7-rhel-7.html
    `
    $ hostnamectl
    $ hostnamectl status

    # hostnamectl set-hostname new_hostname
    # hostnamectl set-hostname “new_hostname” –pretty
    # hostnamectl set-hostname new_hostname –static
    # hostnamectl set-hostname new_hostname –transient

    Static – The static host name is traditional host which can be chosen by the user and is stored in /etc/hostname file.
    Transient – The transient host name is maintained by kernel and can be changed by DHCP and mDNS.
    Pretty – It is a free form UTF -8 host name for the presentation to the user.
    `

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注