Post by majpooper on Oct 10, 2020 6:27:02 GMT
I am in the process of testing a cron that will run the apt update and upgrade commands automatically on a daily basis. Since I am basically a hack with no formal computer skills, training or experience all comments and suggestions are welcome - I don't want to be doing something ill-advised.
I ran into two glitches:
1.) apt dist-upgrade broke something and on reboot I got a blank screen
Solution - I use apt upgrade
2.) automating the user y/n prompt in apt upgrade
2.) automating the user y/n prompt in apt upgrade
Solution - the yes | to run a script nested in the update script
bash scripts are run as root from /home
update.sh script
#!/bin/bash
#check if this script actually kicks off
echo "Cron ran update.sh at $(date)" >> /home/sparky/update.txt
# execute apt update
apt update
# execute apt upgrade using yes | upgrade.sh script
yes | bash upgrade.sh
# autoremove
apt autoremove
# autoclean
apt autoclean
#send notification "Update Complete"
function notify-send() {
#Detect the name of the display in use
local display=":$(ls /tmp/.X11-unix/* | sed 's#/tmp/.X11-unix/X##' | head -n 1)"
#Detect the user using such display
local user=$(who | grep '('$display')' | awk '{print $1}' | head -n 1)
#Detect the id of the user
local uid=$(id -u $user)
sudo -u $user DISPLAY=$display DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$uid/bus notify-send "$@"
}
notify-send -u critical "UPDATE COMPLETE $(date)"
#check if this script actually kicks off
echo "Cron ran update.sh at $(date)" >> /home/sparky/update.txt
# execute apt update
apt update
# execute apt upgrade using yes | upgrade.sh script
yes | bash upgrade.sh
# autoremove
apt autoremove
# autoclean
apt autoclean
#send notification "Update Complete"
function notify-send() {
#Detect the name of the display in use
local display=":$(ls /tmp/.X11-unix/* | sed 's#/tmp/.X11-unix/X##' | head -n 1)"
#Detect the user using such display
local user=$(who | grep '('$display')' | awk '{print $1}' | head -n 1)
#Detect the id of the user
local uid=$(id -u $user)
sudo -u $user DISPLAY=$display DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$uid/bus notify-send "$@"
}
notify-send -u critical "UPDATE COMPLETE $(date)"
#End of script
upgrade.sh script
#!/bin/bash
#check if this script actually kicks off
echo "Cron ran upgrade.sh at $(date)" >> /home/sparky/upgrade.txt
# run apt upgrade
apt upgrade
#End of script
#check if this script actually kicks off
echo "Cron ran upgrade.sh at $(date)" >> /home/sparky/upgrade.txt
# run apt upgrade
apt upgrade
#End of script
root crontab
# Edit this file to introduce tasks to be run by cron.
#
# m h dom mon dow command
00 05 * * * bash /home/update.sh
#
# m h dom mon dow command
00 05 * * * bash /home/update.sh
/etc/crontab (reboot will only auto run from here)
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user command
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#
05 05 * * * root reboot
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user command
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#
05 05 * * * root reboot