Automation is one of the best ways to increase productivity. Whether you are a general user or a technically sound individual, certain common and useful tasks can be automated on Ubuntu Linux to save you time—thereby increasing your productivity by many folds. I've selected a list of the 10 most useful tasks you can automate on your Ubuntu machine. Each task is explained with an example so you can easily implement it on your system. Once you get a knack for it, I'm sure you'll create more tasks to ease your daily workflow. Let's get started and become an automation ninja for the Linux platform.
If you are familiar with the Linux command line environment, applying these tasks to your system will be a cakewalk. If not, make sure you take a backup of your important data before going ahead.
Feel free to customize and fine-tune these tasks to match your requirements. Some of the tasks mentioned below can be applied to almost any Linux distribution. Get ready and pick the ones you like.
1. Automate System Updates
If you are short on time and want to keep your Linux system up to date, you can automate the software updates. Make sure your Linux computer is on at the time you choose for automatic updates.
It's a simple process. Here's how to do it.
sudo crontab -e
First of all, open the job scheduling utility file.
0 22 * * SAT apt-get update && apt-get upgrade -y
Then, add the entry as shown above. In this example, I'm scheduling an update job to run every Saturday at 10:00 PM. Feel free to customize the time at which you want to run updates on your system. If you struggle to customize the time, here's an excellent tool to build it with minimum hassle.
2. Automate Backups
Taking backups of important files and directories is a must when it comes to automating the best and most productive tasks. Here's how to do it on a Linux system.
#!/bin/bash
rsync -av --delete /home/username/Docs /mnt/external-drive-mount-point/backups/
Let's assume that all the important files are stored in the Docs folder in the home directory. Here's a script to backup this folder to an external drive. We're using the rsync command for taking the backups. Make the necessary changes to change your folder path and the path to your external drive. I'm giving backup_docs.sh name to this shell script.
# Open crontab file to automate backups
sudo crontab -e
# Add an entry to backup files
0 10 * * * /bin/bash /home/username/backup_docs.sh
And once again, we're adding an entry using the crontab task scheduler. Here, the shell script backup_docs.sh will be executed every day at 10:00 AM. Feel free to customize the time as well as the script to meet your requirements.
3. Automate Log Files' Compression and Deletion
One of the most disk-consuming activities on a Linux system can be log files. Even if you have a large-capacity disk, taking care of these log files is very important.
Here's how you can automate the compression and deletion of these log files. Let's create a shell script compress_delete_logs.sh to achieve this goal.
#!/bin/bash
# Compress log files older than 30 days. Adjust as per your needs
find /var/log -name "*.log" -type f -mtime +30 -exec gzip {} \;
# Delete log files older than 60 days. Adjust as per your needs
find /var/log -name "*.log.gz" -type f -mtime +60 -delete
In the first command, we're finding and compressing all the log files older than 30 days. Depending on your requirements, you can include more file types in the search. Similarly, time duration can also be adjusted. And, in the second command, compressed log files older than 60 days are permanently deleted.
0 7 * * * /bin/bash /home/username/compress_delete_logs.sh
Finally, add a crontab entry to execute the shell script at your preferred time to trigger the compression and deletion tasks. In the example above, a daily 7:00 AM time is chosen. You can customize it.
4. Automate Shutdowns and Restarts
Shutting down or restarting your Linux system can be automated as well. Restarting the system is sometimes necessary after an update. Similarly, power management may require a system shutdown or a system restart.
Here's how to do it.
sudo shutdown -h 22:00
This is manual shutdown command scheduling that'll shut down the system at 10:00 PM. If you want to fully automate this process, add the following entry in the crontab file.
0 22 * * * /usr/sbin/shutdown -h now
This crontab entry fires the shutdown command every day at 10:00 PM. The only difference is that it'll be repeated perpetually until you remove this entry.
0 16 * * * /usr/sbin/shutdown -r now
And, here's an example of a crontab entry that'll restart your Linux system every day at 4:00 PM. Feel free to modify it as per your needs. Notice the use of the -r switch in this command.
5. Automate Disk Space Monitoring and Associated Alerts
Unless you're using multiple high-capacity hard drives in your Linux system, monitoring disk space is a must. Failing to do so can lead to both performance degradation and system crashes.
You can create a script to monitor disk space and send email alerts. Let's give monitor_disk.sh name to this shell script.
#!/bin/bash
# Set threshold percentage
THRESHOLD=90
# Set email recipient
EMAIL="email@your-domain.com"
# Check disk usage and filter out lines where usage exceeds the threshold
df -h | awk -v threshold=$THRESHOLD '
$5 ~ /[0-9]+%/ {
usage = int($5)
if (usage > threshold) {
print $0
}
}
' > /tmp/disk_usage_alert.txt
# If the file is not empty, send an alert email
if [[ -s /tmp/disk_usage_alert.txt ]]; then
mail -s "Warning: Disk Space Exceeded ${THRESHOLD}%" $EMAIL < /tmp/disk_usage_alert.txt
fi
# Clean up temporary file
rm -f /tmp/disk_usage_alert.txt
The script shown above checks all the mounted hard drives and sends an alert email if more than 90% of space is consumed in one or more disks. You can customize the recipient email as well as the threshold of warning as per your needs.
0 9 * * * /bin/bash /home/username/monitor_disk.sh
This crontab entry will trigger the script execution every day at 9:00 AM. You can change the time if required.
6. Create Custom Keyboard Shortcuts for Common Jobs
Each one of us has some common routines we follow when working on a computer. Some of these common tasks are generally completed in multiple steps though a keyboard shortcut can do it in one go. And, that's what we're going to do.
To get started, first, we'll install the xbindkeys package to easily create custom keyboard shortcuts.
sudo apt-get install xbindkeys xbindkeys-config
Thereafter, specify the xbindkeys configuration file.
xbindkeys --defaults > ~/.xbindkeysrc
Finally, open this configuration file in your favorite text editor and start adding the custom keyboard shortcuts.
# Open Terminal
"gnome-terminal"
Control+ Alt + t
# Open Firefox
"firefox"
Control+ Alt + f
# Take Screenshot
"gnome-screenshot"
Control+ Alt + s
Here you can see the addition of multiple keyboard shortcuts. For example, Ctrl + Alt + s keyboard shortcut has been configured to take a screenshot of the desktop. You can add and remove more keyboard shortcuts as per your need.
7. Automate Trash Cleanup
One of the locations contributing to disk space consumption is the trash bin. Depending on the volume of files deleted, you can schedule a task to automatically clean the trash bin at regular intervals. Here's a simple script to do it.
#!/bin/bash
rm -rf ~/.local/share/Trash/files/*
Let's assume the name of this script file is clean_trash_bin.sh, here's a crontab entry to do the job.
0 9 * * SUN /bin/bash /home/username/clean_trash_bin.sh
This entry ensures the trash cleaning activity is triggered every Sunday at 9:00 AM. Customize the time as per your requirement. If the junk files are located at more than one location, feel free to add more delete commands in the script file.
8. Automate Power Management for Laptops
The default power management system of Linux doesn't work that nice with laptops. To automate power management for a laptop, we can use the following package.
sudo apt install tlp tlp-rdw
sudo tlp start
The TLP system automatically optimizes and manages the power settings for a laptop keeping it working in a top-notch condition. It also helps in conserving energy. It quietly works in the background and doesn't require any kind of manual intervention.
9. Automate Screenshots for Tutorials or Monitoring
Whether you're preparing a tech tutorial or simply monitoring your Linux system, taking screenshots is one of the best methods to complete these jobs. Instead of manually taking these screenshots, we can automate the entire process.
To do that, we'll need the scrot application.
sudo apt-get install scrot
Write a shell script to take screenshots through this application.
#!/bin/bash
scrot '%Y-%m-%d_$wx$h.png' -e 'mv $f ~/Screenshots/'
In this script, we're taking screenshots of the entire desktop, and the current timestamp is included in the filename. The Screenshots directory is where all the screenshots are saved.
*/1 * * * * /bin/bash /home/username/take_screenshot.sh
In the last step, add a crontab entry to take screenshots after every one minute. For system monitoring, this time should be anywhere between 5 to 10 minutes.
10. Automate Email Alerts for Critical System Events
One of the critical events associated with any computer system is high CPU usage for a longer period. This can lead to performance degradation and may leave the system in an unhealthy state. On Linux, you can easily check for high CPU usage and can send email alerts for the same. Here's how to do it.
#!/bin/bash
CPU_THRESHOLD=90
CPU_USAGE=$(top -bn1 | grep "Cpu(s)" | awk '{print $2 + $4}')
if (( ${CPU_USAGE%.*} > CPU_THRESHOLD )); then
echo "High CPU usage detected: $CPU_USAGE%" | mail -s "CPU Alert" you@email-domain.com
fi
This script checks for high CPU usage. The variable CPU_THRESHOLD can be fine-tuned to adjust the maximum CPU usage threshold that'll trigger an alert email.
*/5 * * * * /bin/bash /home/username/high_cpu_usage.sh
The crontab entry (shown above) will check for high CPU usage every 5 minutes and will send an email alert if the threshold is crossed.
Conclusion
With just a few commands and scripts, you can automate almost any repetitive task in Ubuntu Linux. Whether it's keeping your system updated, managing backups, or sending alerts, these examples give you a strong foundation to build your automated workflows.
Which automation task do you think will save you the most time? Let me know in the comments!