How to create a script to automatically update and restart your Linux server daily
By: Date: April 23, 2023 Categories: Wordpress

Introduction

Linux servers are the backbone of many organizations, running critical applications and services. As with any system, keeping your Linux server up-to-date is essential for security and optimal performance. In this blog post, we’ll guide you through the process of creating a script to automatically update your Linux server daily and restart it if needed.

Step 1: Update the Package List and Packages

To begin, create a new file named ‘update_server.sh’ using your preferred text editor. We’ll start by writing the script to update the package list and upgrade the packages on the server.

#!/bin/bash

# Update package list and upgrade packages
sudo apt-get update -y
sudo apt-get upgrade -y

Step 2: Check for Required Restart

Some updates may require a server reboot to take effect. Add the following lines to the script to check if a reboot is needed:

# Check if a reboot is required
if [ -f /var/run/reboot-required ]; then
  echo "Reboot required. Restarting the server."
  sudo reboot
else
  echo "No reboot required."
fi

This part of the script checks for the existence of the ‘/var/run/reboot-required’ file, which is created when an update requires a system reboot. If the file is present, the script will restart the server; otherwise, it will display a message indicating that no reboot is necessary.

Step 3: Make the Script Executable

To make the script executable, run the following command:

chmod +x update_server.sh

Step 4: Schedule the Script to Run Daily

To ensure that the script runs automatically once a day, we’ll use the ‘cron’ scheduler. Open the ‘crontab’ file for the current user with the following command:

crontab -e

Add the following line at the end of the file:

0 3 * * * /path/to/update_server.sh >> /path/to/update_server.log 2>&1

This line schedules the script to run daily at 3:00 AM. Replace ‘/path/to/update_server.sh’ with the full path to your script, and ‘/path/to/update_server.log’ with the desired location of the log file. The ‘2>&1’ part redirects the script’s output and error messages to the specified log file.

Save and exit the ‘crontab’ file. The script will now run daily and automatically update your Linux server, restarting it if necessary.

Conclusion

In this blog post, we’ve demonstrated how to create a simple script to update your Linux server daily and restart it if needed. This script will help ensure your server stays up-to-date, secure, and running smoothly. Be sure to monitor the log file regularly to catch any errors or issues during the update process.

Here is full script :

#!/bin/bash

# Update package list and upgrade packages
sudo apt-get update -y
sudo apt-get upgrade -y

# Check if a reboot is required
if [ -f /var/run/reboot-required ]; then
  echo "Reboot required. Restarting the server."
  sudo reboot
else
  echo "No reboot required."
fi

Save this script as ‘update_server.sh’ and follow the instructions provided in the blog post to make it executable and schedule it to run daily using ‘cron’.

Leave a Reply

Your email address will not be published. Required fields are marked *