Shell 脚本(从 cron 运行)将输出重定向到文件 Ubuntu(Orange Pi 上的 Armbian)

Shell 脚本(从 cron 运行)将输出重定向到文件 Ubuntu(Orange Pi 上的 Armbian)

我有一台运行 Armbian(基于 Ubuntu)的小型 Orange Pi One 服务器,它有一个脚本,用于检查运行 LibreElec 8.0 的 Rasberry Pi2 上运行的 Web 服务器是否可用,如果不可用,它会通过 ESP8266 设备上的 Web 服务器通过 433 Mhz 远程控制插座对服务器(Rasberry Pi)进行电源循环控制。所有设备都在我的家庭网络上;没有外部访问。

当我从命令行运行该脚本时,它运行良好 - 我看到了逻辑流程等,并且得到了所需的结果。我的 crontab 中有一行用于按小时运行检查。但是,我没有在 crontab 行重定向中指定的日志文件中看到任何输出。我知道 crontab 条目正在运行,因为创建了一个空的日志文件。

这是我的 crontab 中的一行。我试图将日期和时间以及消息输出到一个文件中,这样我就能知道是否以及何时运行了测试并启动了重启。

我意识到 wget 的输出被发送到 dev\null,但我希望我的 echo 命令被记录下来。这些概述了脚本中的逻辑流程,这正是我想要记录的内容。

59 0-23 * * */root/loftRPTCheck.sh | while IFS= read -r line; do echo "$(date) $line"; done >> /root/RPTloftlog.txt

以下是我用来手动运行检查的命令和我的 PuTTY 终端上的输出:

root@orangepione:~# ./loftRPTCheck.sh
--2017-05-18 21:05:05--  http://192.168.0.143:9091/
Connecting to 192.168.0.143:9091... connected.
HTTP request sent, awaiting response... Read error (Connection timed out) in headers.
Retrying.

--2017-05-18 21:05:13--  (try: 2)  http://192.168.0.143:9091/
Connecting to 192.168.0.143:9091... connected.
HTTP request sent, awaiting response... Read error (Connection timed out) in headers.
Retrying.

--2017-05-18 21:05:22--  (try: 3)  http://192.168.0.143:9091/
Connecting to 192.168.0.143:9091... connected.
HTTP request sent, awaiting response... Read error (Connection timed out) in headers.
Retrying.

--2017-05-18 21:05:32--  (try: 4)  http://192.168.0.143:9091/
Connecting to 192.168.0.143:9091... connected.
HTTP request sent, awaiting response... Read error (Connection timed out) in headers.
Giving up.

wget to Loft RP Transmission failed
Power off Loft RP
--2017-05-18 21:05:39--  http://192.168.0.237/LOFTOFF
Connecting to 192.168.0.237:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: ‘/dev/null’

/dev/null                           [ <=>                                                  ]      68  --.-KB/s    in 0s

2017-05-18 21:05:41 (1.23 MB/s) - ‘/dev/null’ saved [68]

Sleep 10s
Power on Loft RP
--2017-05-18 21:05:51--  http://192.168.0.237/LOFTON
Connecting to 192.168.0.237:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: ‘/dev/null’

/dev/null                           [ <=>                                                  ]      68  --.-KB/s    in 0s

2017-05-18 21:05:54 (1.26 MB/s) - ‘/dev/null’ saved [68]

Sleep 20s
Retrying Loft Tranismission..
--2017-05-18 21:06:14--  http://192.168.0.143:9091/
Connecting to 192.168.0.143:9091... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: /transmission/web/ [following]
--2017-05-18 21:06:14--  http://192.168.0.143:9091/transmission/web/
Reusing existing connection to 192.168.0.143:9091.
HTTP request sent, awaiting response... 200 OK
Length: 24139 (24K) [text/html]
Saving to: ‘/dev/null’

/dev/null                       100%[=====================================================>]  23.57K  --.-KB/s    in 0s

2017-05-18 21:06:14 (50.7 MB/s) - ‘/dev/null’ saved [24139/24139]

Restart worked ok.

因此,这看起来没问题,因为从命令行运行时输出消息,但我不确定如何最好地将此输出记录到文件中,并且在每行的开头带有日期和时间。

这是运行的 shell 脚本:

root@orangepione:~# cat loftRPTCheck.sh
wget --tries=4 --timeout=7 -O/dev/null http://192.168.0.143:9091

if [[ $? -ne 0 ]]; then
    echo "wget to Loft RP Transmission failed"
    echo "Power off Loft RP"
    wget -O/dev/null http://192.168.0.237/LOFTOFF
    echo "Sleep 10s"
    sleep 10
    echo "Power on Loft RP"
    wget -O/dev/null http://192.168.0.237/LOFTON
    echo "Sleep 20s"
    sleep 20
    echo "Retrying Loft Transmission.."
    wget  --tries=4 --timeout=7 -O/dev/null http://192.168.0.143:9091
    if [[ $? -ne 0 ]]; then
        echo "RP not restarted???"
    else
        echo "Restart worked ok."
    fi
    exit 1;
fi

答案1

相关内容