如何解决将bash脚本输出显示到控制台或/ var / spool / mail / {user_name}中以邮件形式接收输出
我不熟悉脚本和cron作业,但仍在尝试以了解如何使用它们。
我创建了一个简单的脚本,该脚本现在可以告诉我我的IP
#!/bin/bash
#Create the container to hold the output of ip a command
ifco=$(ip a | grep 'scope global' | grep -v virbr0 | grep -oP '(?<=inet\s)\d+\.\d+\.\$
#Printing out the curent IP address
echo "My IP address currently is "$ifco
脚本完成了工作,输出如下:
My IP address currently is 192.168.135.249
然后输入crontab -e并输入以下内容:
MAILTO=gui
31 * * * * gui /home/gui/scripts/06/test_ifc.sh > /home/gui/scripts/06/cronoutput.txt 2>&1
然后我检查了cronoutput.txt文件的内容,它告诉我:
/bin/sh: gui: command not found
另外,当我键入 mail 命令时,由于我有 MAILTO = gui {我的用户} ,所以我希望收到一封包含命令输出的电子邮件,但是没有电子邮件。
请告诉我如何接收电子邮件或在控制台上显示脚本的输出
p.s。我输入了 tty ,这给了我 / dev / pts / 0 ,所以我将cron条目更改为:
31 * * * * gui /home/gui/scripts/06/test_ifc.sh > /dev/pts/0
但是,这种方法效果不佳。
谢谢。
解决方法
我钉了它。 CRON想要 ip 命令的完整路径。所以我的脚本应该是这样的:
#!/bin/bash
#Create the container to hold the output of ip a command
ifco=$(/usr/sbin/ip a | grep 'scope global' | grep -v virbr0 | grep -oP '(?<=inet\s)\d+\.\d+\.\$
#Printing out the curent IP address
echo "My IP address currently is "$ifco
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。