Home > Back-end >  Ubuntu Server (ARM) Bash script not writing to file
Ubuntu Server (ARM) Bash script not writing to file

Time:04-01

I created a simple bash script to update my raspberry pi running PiHole (on Ubuntu Server ARM).

I have a cron setup, but I want to have the script write the date of when it runs to a log file so I can make sure it is running when it is supposed to. The command I am having issues with is

date >> ./update.log

I have manually ran the script several times, but the update.log file is still empty. The entire script is as follows

#!/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

bonus points if you can tell me the right way to enter a new line so I don't have to use extra echo "" commands. (I'm not a bash scripter, used to PowerShell)

CodePudding user response:

At the moment you are writing to the file update.log in some undefined directory.

You probably need to specify an absolute path, for example /home/user/update.log.


For printing printing newlines, you need to call echo with the -e

flag: echo -e "\n\n\n\n"

From 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
...

CodePudding user response:

changing

reboot -fn

to

reboot

fixed the issue.

I did try adding a 2 second sleep after the logging command, and before the reboot, but it wasn't long enough and the system would still reboot before the datetime was written to the log file.

This happened regardless if I used absolute paths or not.

  • Related