将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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...