linux - crontab 的调试,启动thin服务器

访问量: 72

很多时候我们要执行crontab命令,但是事后发现命令并没有被执行.

原因肯定是多种多样的.找到问题的线索:

例如,我希望在 每天凌晨2点运行某个命令:

0 2 * * * cd /var/data/zhi_dao_yuan_manage/current && ./restart_very_quickly

1. 查看 /var/log/crontab 这个日志文件,这里会记录某个命令在某个时刻是否被执行. 如下所示:

Nov 18 02:00:01 HBL-WEB-1 CROND[58286]: (zhidaoyuan) CMD (cd /var/data/zhi_dao_yuan_manage/current && ./restart_very_quickly)

2. 查看 /var/spool/mail/zhidaoyuan 文件,就可以看到具体的报错信息:

3220 From: root@HBL-WEB-1.localdomain (Cron Daemon)
3221 To: zhidaoyuan@HBL-WEB-1.localdomain
3222 Subject: Cron <zhidaoyuan@hbl-web-1> cd /var/data/zhi_dao_yuan_manage/current && ./restart_very_quickly
3223 Content-Type: text/plain; charset=UTF-8
3224 Auto-Submitted: auto-generated
3225 X-Cron-Env: <shell= bin="" sh="">
3226 X-Cron-Env: <home= home="" zhidaoyuan="">
3227 X-Cron-Env: <path= usr="" bin:="" bin="">
3228 X-Cron-Env: <logname=zhidaoyuan>
3229 X-Cron-Env: <user=zhidaoyuan>
3230 Message-Id: <20181117180001.BFE5C158936B@HBL-WEB-1.localdomain>
3231 Date: Sun, 18 Nov 2018 02:00:01 +0800 (CST)
3232 
3233 ./restart_very_quickly: line 2: bundle: command not found

所以,就知道,这个cron任务没能执行的原因是没有找到bundle命令.给它设置一下就可以了.

3. 解决办法:crontab 执行命令时,加载用户的配置文件:

source ~/.bash_profile && cd ... && ./restart ... 

就可以了. 

3262 From: root@HBL-WEB-1.localdomain (Cron Daemon)
3263 To: zhidaoyuan@HBL-WEB-1.localdomain
3264 Subject: Cron <zhidaoyuan@HBL-WEB-1> source ~/.bash_profile && cd /var/data/zhi_dao_yuan_manage/current && sh /var/data/zhi_dao_yua
3265 Content-Type: text/plain; charset=UTF-8
3266 Auto-Submitted: auto-generated
3267 X-Cron-Env: <SHELL=/bin/sh>
3268 X-Cron-Env: <HOME=/home/zhidaoyuan>
3269 X-Cron-Env: <PATH=/usr/bin:/bin>
3270 X-Cron-Env: <LOGNAME=zhidaoyuan>
3271 X-Cron-Env: <USER=zhidaoyuan>
3272 Message-Id: <20181118094344.964ED1580683@HBL-WEB-1.localdomain>
3273 Date: Sun, 18 Nov 2018 17:43:05 +0800 (CST)
3274 
3275 Starting server on 0.0.0.0:3300 ...
3276 Deleting stale PID file tmp/pids/thin.3300.pid
3277 
3278 Starting server on 0.0.0.0:3301 ...
3279 Deleting stale PID file tmp/pids/thin.3301.pid
3280 

订阅/RSS Feed

Subscribe

分类/category