使用 heredocument 重定向到 crontab

Arc*_*ing 0 cron io-redirection here-document

是否可以crontab使用 heredocument打印?

我尝试了这些但失败了:

cat <<-"CRONTAB" > crontab
    0 0 * * * cat /dev/null > /var/mail/root
    0 1 * * 0 certbot renew -q
CRONTAB
Run Code Online (Sandbox Code Playgroud)

和:

bash <<-"CRONTAB" > crontab
    0 0 * * * cat /dev/null > /var/mail/root
    0 1 * * 0 certbot renew -q
CRONTAB
Run Code Online (Sandbox Code Playgroud)

另一方面,这不是此处的文档,而是有效的:

# CRONTAB
echo "
    0 0 * * * cat /dev/null > /var/mail/root
    0 1 * * 0 certbot renew -q
" | crontab
Run Code Online (Sandbox Code Playgroud)

因此,我想知道是否可以使用 heredocument。

Ste*_*itt 6

正如其他人指出的那样,crontab是一个命令,因此您需要做的就是将其提供给 heredoc:

crontab <<-"CRONTAB"
Run Code Online (Sandbox Code Playgroud)

但正如前面已经提到的,这是一个可怕的很多容易被操纵的文件管理cron作业/etc/cron.daily/etc/cron.d等等。

  • 特别是如果您不想破坏任何可能已经定义的 cronjobs! (4认同)