如何在crontab中使用.profile别名

我已经阅读了这个问题的答案,但仍然不明白该怎么做:

如何在我的crontab中使用我的别名?

我以root身份login到我的Ubuntu服务器。 我在.profile有以下命令:

 alias test-alias="echo test" 

我在我的crontab文件中有以下命令:

 11 9 * * * source /root/.profile; test-alias > /root/tmp.output 2>&1 

当此命令运行时, tmp.output唯一的输出是:

 /bin/sh: 1: test-alias: not found 

我在这里做错了什么? 我如何在我的crontab文件中使用我的test-alias ? 我想直接在命令中使用别名,我不想创build额外的脚本来运行别名。

虽然这不是最漂亮的解决scheme,虽然我build议你不要使用它,但你可以做的是:

 11 9 * * * bash -ic "test-alias > /root/tmp.output 2>&1" 

这将运行bash作为交互式shell(-i),并将读取bashrc。 为了确保.profile源文件,你需要在你的.bashrc文件中包含这个块:

 [ -f ~/.profile ] && source ~/.profile 

请注意,这种运行cron作业或编写脚本是一个非常糟糕的做法,你应该尝试和避免它。