With Amazon Lightsail, like most other virtual private servers (VPS), you are responsible for performing server maintenance activities, e.g. applying patches, once the instance is running. While it’s easy to perform this manually based on a calendar reminder, it’s also easy to forget to do it periodically. When added to the user crontab, the following bash script automatically performs OS patches on a schedule that you define. Please be aware that most Lightsail images are Bitnami based and this method will not apply upgrades/patches on its packaged applications. Updating Apache, for example, requires moving to a new instance with the latest Bitnami image.
Since this example assumes a Lightsail environment based on a Bitnami stack (Debian), user and path specifics may need change for your specific environment.
Script File Setup
As a personal preference, I write user scripts to a “scripts” directory in my home directory. This first step creates a new “scripts” directory in my home directory with 700 permissions (read, write, execute). Then, I create a blank file named “maintenance.sh” to contain the script. I prefer to use Vim to edit files, but please feel free to adjust to your preferred editor.
mkdir -m 700 ~/scripts touch ~/scripts/maintenance.sh chmod 700 ~/scripts/maintenance.sh vim ~/scripts/maintenance.sh
Script Source Code
The next step is to add the script to the blank maintenance.sh file. The script performs only a few actions: “apt update” retrieves the latest list of available packages and versions, but it does not upgrade any packages; “apt upgrade” upgrades existing/installed packages to the latest version. Finally, “apt autoclean” removes package files from the local repository that have been uninstalled or they are no longer available for downloaded.
The script also deletes older log files in the Apache, MySQL, and PHP directories. The rm commands remove all log files older than the latest 5 based on the date based file naming conventions.
#!/bin/bash sudo apt update && sudo apt upgrade -y sudo apt autoclean sudo rm -f $( find /opt/bitnami/apache/logs -maxdepth 1 -iname '*access_log-*' -type f | sort -r | tail -n +6 ) sudo rm -f $( find /opt/bitnami/apache/logs -maxdepth 1 -iname '*error_log-*' -type f | sort -r | tail -n +6 ) sudo rm -f $( find /opt/bitnami/mysql/logs -maxdepth 1 -iname '*mysqld.log-*' -type f | sort -r | tail -n +6 ) sudo rm -f $( find /opt/bitnami/php/logs -maxdepth 1 -iname '*php-fpm.log-*' -type f | sort -r | tail -n +6 )
Configure Cron Schedule
The last step is to schedule the job using cron. The next command allows you to edit the user crontab.
As an example, add the following line to the user crontab to schedule the script to run at 3:00 AM every Sunday.
0 3 * * 0 /home/bitnami/scripts/maintenance.sh
Exit the editor and the your maintenance activities will be performed automatically as scheduled.
- Install a LAMP Stack Manually on a Debian OS Only Amazon Lightsail Instance
- Automatically Back Up a Linux Server to Google Drive
- Enable Browser Caching Directives in Amazon Lightsail Apache Server
- Hardening WordPress Security Using .htaccess
- How to Migrate an Existing WordPress Site to Amazon Lightsail LAMP