generated from cotes2020/chirpy-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create 2023-12-08-关于cron执行失败原因分析及查找.md
- Loading branch information
1 parent
6346c6c
commit f012a61
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
--- | ||
layout: post | ||
title: "关于cron执行失败原因分析及查找" | ||
date: 2023-12-08 13:29:20 +0800 | ||
categories: | ||
- 杂项 | ||
tags: | ||
- cron定时任务 | ||
- cron异常排查定位 | ||
|
||
--- | ||
|
||
这几天想将博客迁移到我服务器上,因为baidu不能收录到github的内容. | ||
|
||
于是想了个方案,思路如下: | ||
- 从github将仓库pull到服务器上 | ||
- 使用nginx作为web服务器 | ||
- 服务器建一个脚本,每5分钟从拉取一次,然后再编译下就ok | ||
|
||
思路很简单,但是实践的时候出现了不少问题,特此记录一下。 | ||
|
||
#### 第一个问题 | ||
脚本写好并配置到crontab 后, 不执行,也不知道是出了啥问题。此时可以在cron中加入以下代码将错误进行输出: | ||
```shell | ||
* * * * * /usr/local/services/nginx_site/html/misterchangray.github.io/gitupdate.sh 2>/tmp/test.log | ||
``` | ||
注意后面的`2>/tmp/test.log`,此语句意思是将错误输出到这个日志文件中。 | ||
|
||
#### 第二个问题 | ||
此时可以通过日志排查问题,定位到原因是脚本在cron执行时没有相关环境变量,baidu搜索后得知cron脚本执行和直接shell执行环境不一样,可以通过以下方式增加环境: | ||
- 在cron配置时增加环境变量配置,如下: | ||
``` shell | ||
* * * * * . /etc/profile;/bin/sh /usr/local/services/nginx_site/html/misterchangray.github.io/gitupdate.sh 2>/tmp/test.log | ||
``` | ||
- 在脚本头部引用环境变量 | ||
```shell | ||
#!/bin/sh | ||
. /etc/profile | ||
. ~/.bashrc | ||
|
||
``` |