Crontab电子邮件主题中的日期 [英] Date in Crontab email subject

查看:66
本文介绍了Crontab电子邮件主题中的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一台ubuntu服务器,在其中安排了如下的crontab进程.

I have an ubuntu server where I schedule crontab processes like the following.

59 2 * * * : Backup Settings; ~/backup_settings.sh

在该过程结束时,我将收到一封主题为"Backup Settings ..."的电子邮件.本质上,noop函数(:)不会对"Backup Settings"(备份设置)字样起作用.我想在电子邮件主题中添加今天的日期.自然,我尝试过

At the conclusion of the process, I will get an email with the subject line "Backup Settings ...". Essentially the noop function (:) does nothing with the words "Backup Settings". I would like to add today's date to the email subject. Naturally, I tried

59 2 * * * : $(date +%Y%m%d) Backup Settings; ~/backup_settings.sh

,但这不会产生所需的电子邮件主题,即"20180519备份设置".$(...)代码未评估.我不想运行另一个具有电子邮件功能的脚本,该脚本随后将调用backup_settings.sh.是否可以仅使用crontab中的Bash命令来做到这一点?

but that doesn't result in the desired email subject, i.e. "20180519 Backup Settings". The $(...) code gets unevaluated. I don't want to run another script with email functionality that will then call backup_settings.sh. Is there a way of doing it using just Bash commands in crontab?

推荐答案

字符在crontab中是特殊字符,必须转义为 \%:

The character % is special in the crontab and has to be escaped as \%:

59 2 * * * : $(date +\%Y\%m\%d) Backup Settings; "$HOME/backup_settings.sh"

在Ubuntu系统上的 man 5 crontab 中:

From man 5 crontab on an Ubuntu system:

该行的整个命令部分,直到换行符或字符,将由以下命令执行/bin/sh 或crontab文件的 SHELL 变量中指定的shell.除非使用反斜杠( \ )进行转义,否则命令中的百分号()将被更改为换行符,第一个之后的所有数据将作为标准输入发送到命令.

The entire command portion of the line, up to a newline or % character, will be executed by /bin/sh or by the shell specified in the SHELL variable of the crontab file. Percent-signs (%) in the command, unless escaped with backslash (\), will be changed into newline characters, and all data after the first % will be sent to the command as standard input.

不过,请注意,cron会将cronjob的逐字记录命令作为主题发送到它发送的任何电子邮件中,而不是展开的命令行中.

Note, though, that cron will put the verbatim command of the cronjob in as the subject in any email that it sends, not the expanded command line.

要发送具有自己标题的电子邮件,请明确使用 mail :

To send an email with your own title, use mail explicitly:

59 2 * * * "$HOME/backup_settings.sh" | mail -s "$(date +\%Y\%m\%d) Backup Settings" myname

(其中 myname 是您要将电子邮件发送到的地址).

(where myname is the address you'd like to send the email to).

这篇关于Crontab电子邮件主题中的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆