use*_*991 6 shell cron cron-task
是否有可能确定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
Run Code Online (Sandbox Code Playgroud)
一种选择是测试脚本是否附加到tty.
#!/bin/sh
if [ -t 0 ]; then
echo "I'm on a TTY, this is interactive."
else
logger "My output may get emailed, or may not. Let's log things instead."
fi
Run Code Online (Sandbox Code Playgroud)
请注意,触发的作业at(1)也是在没有tty的情况下运行的,尽管cron没有特别说明.
另请注意,这是POSIX,而不是Linux-(或bash-)特定的.
| 归档时间: |
|
| 查看次数: |
791 次 |
| 最近记录: |