Boosting Productivity: Automating Linux Tasks with Ease
Our exploration begins with automation scripts, but we don't stop at the basics. We'll demonstrate how to leverage cron to schedule tasks at precise intervals, unveiling the power of automation in periodic, timed execution. You'll witness real-world examples of automation scenarios that elevate your Linux system administration game.
Example 1: Let's say you have been asked to write a bash script to create directories.sh that when the script is executed with three given arguments (one is the directory name, second is the start number of directories and third is the end number of directories ) it creates a specified number of directories with a dynamic directory name.
When the script is executed as
./
createDirectories.sh
october 1 31
This command will create directories named october1
, october2
, october3
, and so on, up to october31
, represents each day of October.
Solution:
Example 2: Create a Script to backup all your work done till now.
Creating a backup of your work is essential to safeguard your files and data. Here's a simple Bash script named backup.sh
that you can use to create a backup of your work. This script uses the tar
command to create a compressed archive of specified directories and files.
Don't want to take backups manually? No problem, cron is here to automate your backup task.
What is cron?
Cron is a job schedular in the Linux operating system that allows you to schedule and automate tasks at specific times or intervals. Cron is a powerful tool for automating repetitive tasks, such as backups, system monitoring, and data maintenance. All you need to do is write a cron expression as shown below for your script/command and add it to crontab.
What is crontab?
Crontab (short for "cron table") is a configuration file used to specify the schedule of tasks for the cron daemon to execute. Each user on a Unix-like system can have their own crontab file, which lists the commands or scripts they want to run at specified times. The cron daemon reads these crontab files and ensures that the scheduled tasks are executed accordingly.
To open the crontab file use the command:
crontab -e
Suppose I want to run my backup script every day at 2:00 AM, I would add the following line to my crontab file:
0 2 * /home/ubuntu/backup.sh
User management
In Linux, user management is the process of maintaining user accounts. User management includes the following key tasks:
User Account Creation
Password Management
User Privileges and Permissions
Group Management
Account Deactivation and Removal
Account Monitoring and Auditing
User accounts typically consist of a username, a unique numerical user ID (UID), and a home directory. User accounts can have different levels of privileges on a Linux system. Administrators can grant or restrict access to files, directories, and system resources by configuring file permissions, groups, and sudo (superuser) access.
Example of adding 2 users in Linux:
You can find your users listed in the file /etc/passwd
In this example:
Username:
chinki
Password: Usually an "x" (the actual password hash is stored in
/etc/shadow
)UID:
1001
GID:
1001
Home Directory:
/home/chinki
Login Shell:
/bin/bash
Summary:
This blog explores advanced Linux automation using cron, user management, and more. From creating directories to automated backups and mastering user accounts, we dive into practical examples that enhance your Linux system administration skills. Elevate your automation game with this comprehensive guide and take control of your Linux tasks.
Thank you!
Subscribe to my newsletter
Read articles from Payal Saindane directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Payal Saindane
Payal Saindane
๐จโ๐ป Software Engineer on ๐ ๏ธ DevOps Journey | โ๏ธ AWS Cloud Explorer