当前位置:网站首页>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
边栏推荐
- Easynmon Usage Summary
- EasyCVR平台通过接口编辑通道出现报错“ID不能为空”,是什么原因?
- Use QT designer interface class to create two interfaces, and switch from interface 1 to interface 2 by pressing the key
- tkinter窗口预加载
- [TestLink] testlink1.9.18 solutions to common problems
- Access the database and use redis as the cache of MySQL (a combination of redis and MySQL)
- EPM相关
- 星环科技数据安全管理平台 Defensor重磅发布
- MATLAB查阅
- How awesome is the architecture of "12306"?
猜你喜欢

Cmake tutorial Step2 (add Library)

Ten top automation and orchestration tools

What are the requirements for PMP certification? How much is it?

GFS distributed file system

What are the changes in the 2022 PMP Exam?

How awesome is the architecture of "12306"?

论文阅读_中文NLP_LTP

“12306” 的架构到底有多牛逼?

Elk log analysis system

神经网络自我认知模型
随机推荐
Sophon Base 3.1 推出MLOps功能,为企业AI能力运营插上翅膀
To solve the stubborn problem of Lake + warehouse hybrid architecture, xinghuan Technology launched an independent and controllable cloud native Lake warehouse integrated platform
Action avant ou après l'enregistrement du message teamcenter
Huaxia Fund: sharing of practical achievements of digital transformation in the fund industry
Binder开辟线程数过多导致主线程ANR异常
Tkinter window preload
深拷贝与浅拷贝【面试题3】
PMP认证需具备哪些条件啊?费用多少啊?
leetcode每日一练:旋转数组
ISPRS2020/云检测:Transferring deep learning models for cloud detection between Landsat-8 and Proba-V
Penetrate the whole intranet through socks agent
Disabling and enabling inspections pycharm
检查命名空间和类
ISPRS2022/雲檢測:Cloud detection with boundary nets基於邊界網的雲檢測
从XML架构生成类
Please tell me why some tables can find data by writing SQL, but they can't be found in the data map, and the table structure can't be found
mybash
Cmake tutorial step5 (add system self-test)
使用QT遍历Json文档及搜索子对象
Anaconda中配置PyTorch环境——win10系统(小白包会)