如何判断Linux shell脚本是由用户还是cronjob执行的?
如果可以,我该如何判断/检查shell脚本是否由cronjob执行?
我想在我的脚本中实现一个功能,当它被用户执行时返回一些其他信息,就像这样:
if [[ "$type" == "cron" ]]; then
echo "This was executed by a cronjob. It's an automated task.";
else
USERNAME="$(whoami)"
echo "This was executed by a user. Hi ${USERNAME}, how are you?";
fi
* * * * * CRON_FIRED=true /path/to/myscript
但请注意,语法取决于cron实现,并且可以轻松被欺骗。 - bishop