0

I learned to use a computer with DOS so I am trying to approximate a batch/.bat file to run all the updates to my system and create a log file of the outcome.

#!/bin/bash
#update system in one step
cd ~
date > aupdate.log
echo Launching updates...
echo Beginning apt update
sudo apt update >> aupdate.log
sudo apt upgrade -y >> aupdate.log
echo Completing apt autoremove process
sudo apt autoremove -y >> aupdate.log
echo Beginning snap update
sudo snap refresh >> aupdate.log
echo Beginning flatpak update
flatpak update -y >> aupdate.log
echo Updates complete. Review aupdate.log for details.

Am I on the right track? So far I have two problems I need to resolve.

  1. No log file is being created
  2. How do I suppress the warning about redirecting output when running apt > filename (EDIT: See below)

Edit: This is the result when I run the script:

Launching updates...
Beginning apt updates

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

Completing apt autoremove process

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

Beginning snap update All snaps up to date. Beginning flatpak update Updates complete. Review aupdate.log for details.

EDIT: Based on some early feedback from the comments, I changed the script

#!/bin/bash
#update system in one step. Vers 1.1
date > ~/aupdate.log
echo Beginning update...
echo Beginning apt-get updates
apt-get update >> ~/aupdate.log
apt-get upgrade -y >> ~/aupdate.log
echo Completing apt autoremove process
apt-get autoremove -y >> ~/aupdate.log
echo Beginning snap update
snap refresh >> ~/aupdate.log 2>/dev/null
echo Beginning flatpak update
flatpak update -y >> ~/aupdate.log
echo Updates complete. Review aupdate.log for details.

Switching to apt-get resolved the warning and using 2>/dev/null with the snap line suppressed a snap output I just noticed. I also removed sudo from the lines because I run the script as su. However there's still no log file

EDIT: This is the result when I run the script now as well as ls after.

eddie@mrpbell5:~$ sudo ./aupdate.sh
Beginning update...
Beginning apt-get updates
Completing apt autoremove process
Beginning snap update
Beginning flatpak update
Updates complete. Review aupdate.log for details.
eddie@mrpbell5:~$ ls ~ | grep log
eddie@mrpbell5:~$ ls -a
.           .bash_history  .cache      .config    Downloads  .local    Music     .profile  src                        .surfshark  Videos
..          .bash_logout   .cert       Desktop    .gnome     move      Pictures  Public    .ssh                       Templates   .wget-hsts
aupdate.sh  .bashrc        CiscoSpark  Documents  .gnupg     .mozilla  .pki      snap      .sudo_as_admin_successful  .var        .zoom
EHJ
  • 85

1 Answers1

7

By sudo you become the root

I ran your modified script with sudo ./aupdate.sh.

The log file is created in the folder /root/ as I had suspected. This is because, when you execute a command (or a script) with sudo the command is executed by the special user root. The user root has its "home" in a special folder at /root/.

Where should the log go?

You can put the log in your home folder /home/ehj/. However, since this is a log of system update activities you may want to keep it in /var/log/.

Sample code

I have added a "environmental variable" in your script to keep the information about the log file.

#!/bin/bash
# Location of log
LOG="/var/log/aupdate.log"
#update system in one step. Vers 1.1
date > "$LOG"
echo Beginning update...
echo Beginning apt-get updates
apt-get update >> "$LOG"
apt-get upgrade -y >> "$LOG"
echo Completing apt autoremove process
apt-get autoremove -y >> "$LOG"
echo Beginning snap update
snap refresh >> "$LOG" 2>/dev/null
echo Beginning flatpak update
flatpak update -y >> "$LOG"
echo Updates complete. Review "$LOG" for details.

Edit the location of the log file in the line that starts with LOG=... to your home folder if you like.

Other Options

Unattended Upgrades

The unattended-upgrades app usually keeps the security updates up-to-date. However it can be used to keep other apps updated as well. See How to enable silent automatic updates for any repository? for how to set it up.

The unattended-upgrades also keeps a log of what it does, much like the log the above script creates.

Alias

Another approach is to use the alias command to create an alias for a list of commands. You have to create a file called /home/$USER/.bash_aliases with the following content:

# My list of aliases
# Note: Since this is called by .bashrc 
# it seems this file does not need to be executable

alias update="sudo apt update && sudo apt -y upgrade && sudo apt -y --purge autoremove"

This alias called update does not keep log of the updates but does something similar.

Hope this helps

user68186
  • 33,360
  • There it is! Why is it ending up there instead of ~/ ? is it because /root is ~/ for sudo? how do I fix the output? – EHJ Apr 28 '22 at 21:57
  • I will complete my answer tomorrow. You are right about the why. – user68186 Apr 28 '22 at 23:02
  • @EHJ Just give the full path to where you want the output to go, don't use ~ – Organic Marble Apr 29 '22 at 01:47
  • @user68186 thank you very much for the information! however I am just curious why if I type out the command sudo apt update >> aupdate.log it goes to /home but if I run the script it goes to /root... why is that? – EHJ Apr 30 '22 at 15:29
  • @EHJ That is a very good question. I don't know the answer yet. Please ask a new question, so that others with more knowledge can answer it. This may be due to the difference in the environmental settings in the terminal as compared with the bash. – user68186 Apr 30 '22 at 17:05