当前位置:网站首页>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
边栏推荐
- Customize the theme of matrix (I) night mode
- Cmake tutorial Step4 (installation and testing)
- [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
- Xiaobai getting started with NAS - quick building private cloud tutorial series (I) [easy to understand]
- "Xiaodeng in operation and maintenance" is a single sign on solution for cloud applications
- LeetCode笔记:Weekly Contest 300
- matlab内建函数怎么不同颜色,matlab分段函数不同颜色绘图
- Interpretation: how to deal with the current security problems faced by the Internet of things?
- EPM相关
- nano的CAN通信
猜你喜欢

Cmake tutorial Step4 (installation and testing)

模拟百囚徒问题

What are the changes in the 2022 PMP Exam?

Sophon AutoCV:助力AI工业化生产,实现视觉智能感知

记一次使用Windbg分析内存“泄漏”的案例

Leetcode daily question: the first unique character in the string

Sophon Base 3.1 推出MLOps功能,为企业AI能力运营插上翅膀

How awesome is the architecture of "12306"?

MATLAB查阅

Star ring technology data security management platform defender heavy release
随机推荐
nacos -分布式事务-Seata** linux安装jdk ,mysql5.7启动nacos配置ideal 调用接口配合 (保姆级细节教程)
Matlab reference
星环科技重磅推出数据要素流通平台Transwarp Navier,助力企业实现隐私保护下的数据安全流通与协作
Huaxia Fund: sharing of practical achievements of digital transformation in the fund industry
Why is February 28 in the Gregorian calendar
Nanjing University: Discussion on the training program of digital talents in the new era
The comprehensive competitiveness of Huawei cloud native containers ranks first in China!
论文阅读_医疗NLP模型_ EMBERT
【PaddlePaddle】 PaddleDetection 人脸识别 自定义数据集
Sophon kg upgrade 3.1: break down barriers between data and liberate enterprise productivity
毫无章法系列
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
ITK Example
Why is all (()) true and any (()) false?
隐私计算助力数据的安全流通与共享
Easynmon Usage Summary
Ten top automation and orchestration tools
图扑软件数字孪生 | 基于 BIM 技术的可视化管理系统
Sophon base 3.1 launched mlops function to provide wings for the operation of enterprise AI capabilities