当前位置:网站首页>Crontab 日志:如何记录我的 Cron 脚本的输出
Crontab 日志:如何记录我的 Cron 脚本的输出
2022-07-05 17:51:00 【华为云】
我创建了一个 backup.sh shell 脚本并将其添加到我的 crontab 以每天执行它。如何验证备份 cron 脚本作业是否成功运行?此外,我的 backup.sh shell 脚本中有几个 echo 语句。当脚本作为 cron 作业执行时,如何将脚本的输出保存到日志文件中?
假设您已将 backup.sh 添加到您的crontab中,如下所示,以便在每天零晨执行它。
$ crontab -e59 23 * * * /home/john/bin/backup.sh
要验证此作业是否成功执行,请检查 /var/log/cron 文件,该文件包含有关系统中执行的所有 cron 作业的信息。从以下输出中可以看出,john 的 cron 作业已成功执行。
$ tail /var/log/cronOct 8 22:00:00 dev-db crond[18340]: (root) CMD (/bin/sh /home/root/bin/system_check &)Oct 8 23:00:00 dev-db crond[20348]: (oracle) CMD (/bin/sh /home/oracle/bin/cleanup.sh &)Oct 8 23:59:00 dev-db crond[20399]: (john) CMD (/bin/sh /home/john/bin/backup.sh &)
Cron 日志包含以下信息:
- 时间戳 - 执行 cron 作业的日期和时间
- 主机名 – 服务器的主机名(例如 dev-db)
- cron 守护进程名称和 PID。例如,crond[20399]
- 用户名 – 执行此 cron 作业的用户名。例如,约翰。
- CMD – 这之后的任何内容都是当时执行的真正命令。
如果 backup.sh 中有任何 echo 语句,您可能希望将它们记录到文件中。通常,如果 backup.sh cron 脚本抛出任何输出(包括错误),您可能希望将这些输出记录到日志文件中。为此,请修改 crontab 条目并添加输出和错误重定向,如下所示。
$ crontab -e59 23 * * * /home/john/bin/backup.sh > /home/john/logs/backup.log 2>&1
在上面:
- > /home/john/logs/backup.log表示将backup.sh 脚本的标准输出重定向到backup.log 文件。
- 2>&1表示标准错误 (2>) 被重定向到标准输出 (&1) 指向的相同文件描述符。
- 因此,标准输出和错误都将被重定向到 /home/john/logs/backup.log
边栏推荐
猜你喜欢
含重复元素取不重复子集[如何取子集?如何去重?]
基于YOLOv3的口罩佩戴检测
Nanjing University: Discussion on the training program of digital talents in the new era
RSE2020/云检测:基于弱监督深度学习的高分辨率遥感图像精确云检测
使用QT遍历Json文档及搜索子对象
ISPRS2022/雲檢測:Cloud detection with boundary nets基於邊界網的雲檢測
[JMeter] advanced writing method of JMeter script: all variables, parameters (parameters can be configured by Jenkins), functions, etc. in the interface automation script realize the complete business
修复漏洞 - mysql 、es
Unicode processing in response of flash interface
LeetCode每日一题:合并两个有序数组
随机推荐
Mask wearing detection based on yolov3
[JMeter] advanced writing method of JMeter script: all variables, parameters (parameters can be configured by Jenkins), functions, etc. in the interface automation script realize the complete business
Leetcode daily practice: rotating arrays
Which platform of outer disk gold is regular and safe, and how to distinguish it?
Teamcenter 消息注册前操作或后操作
【PaddlePaddle】 PaddleDetection 人脸识别 自定义数据集
基于YOLOv3的口罩佩戴检测
数值计算方法 Chapter8. 常微分方程的数值解
为什么阳历中平年二月是28天
星环科技数据安全管理平台 Defensor重磅发布
Access the database and use redis as the cache of MySQL (a combination of redis and MySQL)
MATLAB中print函数使用
多线程(一) 进程与线程
通过SOCKS代理渗透整个内网
Operation before or after Teamcenter message registration
OpenShift常用管理命令杂记
Tencent music launched its new product "quyimai", which provides music commercial copyright authorization
Neural network self cognition model
EasyCVR平台通过接口编辑通道出现报错“ID不能为空”,是什么原因?
Sophon CE社区版上线,免费Get轻量易用、高效智能的数据分析工具