Ubuntu服务器(ARM)Bash脚本未写入文件



我创建了一个简单的bash脚本来更新我运行PiHole的树莓pi(在Ubuntu服务器ARM上(。

我有一个cron设置,但我想让脚本把它运行的日期写到日志文件中,这样我就可以确保它在应该运行的时候运行

date >> ./update.log

我已经手动运行了好几次脚本,但update.log文件仍然是空的。整个脚本如下

#!/bin/bash
# This script will update the system
## This command updated the raspberry pi
echo " Updating OS..."
echo ""
apt-get update -y
echo ""
echo " Upgrading OS..."
echo ""
apt-get upgrade -y
## This command updates Pi-Hole
echo ""
echo " Updating PiHole..."
echo ""
pihole -up
## This command updates DNS
echo ""
echo " Updating DNS..."
echo ""
wget https:/www.internic.net/domain/named.root -qO- | sudo tee /var/lib/unbound/root.hints
## This command logs when this script is ran
echo ""
echo " Entering date in log..."
echo ""
date >> ./update_log
## This command reboots the system
echo ""
echo " Rebooting..."
echo ""
reboot -fn

如果你能告诉我输入新行的正确方式,这样我就不必使用额外的echo,那就可以获得加分"命令。(我不是bash脚本编写者,习惯于PowerShell(

此时您正在写入某个未定义目录中的文件update.log

您可能需要指定一个绝对路径,例如/home/user/update.log


要打印换行符,需要使用-e调用echo

标志:echo -e "nnnn"

来自man echo:

-n     do not output the trailing newline
-e     enable interpretation of backslash escapes
-E     disable interpretation of backslash escapes (default)
...       

If -e is in effect, the following sequences are recognized:
...
n     new line
r     carriage return
...

更改

reboot -fn

reboot

修复了问题。

我确实尝试在日志记录命令之后和重新启动之前添加了2秒的睡眠,但时间不够长,在将日期时间写入日志文件之前,系统仍会重新启动。

不管我是否使用绝对路径,都会发生这种情况。

最新更新