Script to Automate Periodic Updates and Kernel Installation

Hello everyone! I’m looking for some guidance on creating a script that automates the periodic update of all programs, including those from the AUR, every 3 hours. I’m thinking of setting up a crontab or something similar to ensure that the updates are performed automatically and regularly. Additionally, I’d like the script to be idempotent, meaning that running it multiple times won’t change the result.

Furthermore, if possible, I’d also like to extend this script to automatically install the latest kernels when they become available.

I would like to have this functionality in a function so that I can add it to my post-install script. Any guidance or suggestions on how to achieve this would be greatly appreciated.

Thank you in advance for your help!

Related:

  1. How can I automatically install the latest kernel?
  2. How to Auto-Update All Packages on a Scheduled Frequency without User Intervention
  3. Automation of Package Updates
  4. Automatic Updates with Cronjob
  5. Automatic Updates for AUR Software
  6. Automating Scripting Pamac Upgrades Including AUR Packages
  7. Using Arch Linux Way of Managing the Kernel and Updates

$HOME/update_system_and_kernel.sh

#!/usr/bin/env bash

create_timeshift_snapshot() {
  sudo timeshift --create --comments "Before system update"
}

update_system() {
  pamac update --aur --no-confirm
}

install_latest_kernel() {
  current_kernel=$(pacman -Qqs linux[0-9] | grep -Po 'linux\d+$' | tail -1)
  latest_kernel=$(mhwd-kernel -l | grep -Po 'linux\d+$' | tail -1)

  if [[ $latest_kernel > $current_kernel ]]; then
    if ! sudo mhwd-kernel -i $latest_kernel; then
      echo "Error installing $latest_kernel"
    fi
  fi
}

main() {
  create_timeshift_snapshot
  update_system
  install_latest_kernel
}

main
#!/usr/bin/env bash

setup_auto_update() {
  CRON_FILE="$HOME/update_system_and_kernel.sh"
  CRON_JOB="0 */3 * * * /usr/bin/sudo $CRON_FILE"

  # Create a new crontab file for root if it doesn't exist
  if ! sudo crontab -l -u root >/dev/null 2>&1; then
    echo "# Automatically generated by setup_auto_update()" | sudo tee /var/spool/cron/crontabs/root >/dev/null
    sudo chown root:crontab /var/spool/cron/crontabs/root
    sudo chmod 600 /var/spool/cron/crontabs/root
  fi

  # Add the cron job if it doesn't exist
  if ! sudo crontab -l -u root | grep -q "$CRON_FILE"; then
    (sudo crontab -u root -l; echo "$CRON_JOB") | sudo crontab -u root -
  fi
}

To check if a cron job is running as expected, you can check the system logs to see if the job has been executed at the expected time. The system logs can be found in /var/log/syslog or /var/log/cron.

To check if the cron job defined in the setup_auto_update() function is running as expected, you can wait for the next scheduled run time (in this case, every 3 hours) and then check the system logs for any messages related to the cron job.

For example, if the cron job is scheduled to run at 12:00 PM, you can wait until 3:00 PM and then check the system logs for any messages related to the cron job by running the following command:

sudo grep CRON /var/log/syslog | grep update_system_and_kernel.sh

This command searches the system logs for any messages containing “CRON” (which indicates that a cron job has been executed) and “update_system_and_kernel.sh” (which is the name of the script that the cron job executes). If the cron job has been executed successfully, you should see a message similar to the following:

Jul 11 15:00:01 hostname CRON[12345]: (root) CMD (/usr/bin/env bash /home/user/update_system_and_kernel.sh)

This message indicates that the cron job was executed by the root user at 3:00 PM. If you don’t see any messages related to the cron job, it may indicate that the cron job is not running as expected and you should investigate further.

Hello ChatGPT!
Please note that Manjaro is an OS, geared towards consumer/advanced home user.
What you are going to do (updating and automatically answering on every question with yes without thinking) will eventually break the system. Please do not shoot youself in the leg although we gave you a gun.

There are more suitable setups that are / can be completely unattended. Like a cloud provider that offers containerized os-es and updates the containers automatically.

3 Likes

Please remove all your AI copy/paste - it is not the intention of this forum to rectify AI generated answers.

Further down the road all those AI copy/paste will make it into search engines and the general quality is decreased by nonsense walls of text.

The amount of links in the topic will lead to even more search engine confusion - it is a really bad manner the way you have begun to post.

2 Likes

And the from those over 100 links about 90 are irrelevant. As everybody that has worked with chatgpt should know by now, the bot is really good in making up fake/irrelevant bibliography, just to look like backing up a thesis…that it just made up :slight_smile:

P.s. i bet Individual-heart718 is working in an it department somewhere in India. He gets a task from his boss, feeds the question through the bot, and posts here so that we correct the errors that the stupid bot made…and voila, did his job and receives his salary.

Why don’t you go talk about it somewhere else? You are being more useless than the AI, at least it understands the question and gives relevant information, it doesn’t ramble about bots. Do you think people who browse online and find this question want to hear you complaining about the bots writing fake links?

I beg your pardon - but you are the one posting walls of AI text - thinking that’s OK.

Please consult the forum rules - posting materiel irrelevant to Manjaro is strictly frowned upon.

And do you truly believe this is the appropriate place to say that when there are private messages available? After I corrected the post, your mention of AI links is unrelated to the discussion, and despite my request for you to refrain from doing so, you continue to bring it up.

You want to create an automatic update when

And even Arch Linux recommends in System maintenance - ArchWiki

And Manjaro is in the same boat regarding that. Moreover, with such unattended updates that you want to be run automatically, you can make your system more prone to Partial Updates

image

You did good removing those AI “citations” links. That means we are on the same page now; the forum doesn’t need those.

1 Like

Yes, even with all the caveats, this is what I want to do. If the system breaks I’ll recover it with Timeshift or reinstall.