Today, I have learned "Advanced Linux Shell Scripting and User Management" as part of my #DevOps journey with the #90DaysOfDevOps challenge by #TrainWithShubham
✅ Automated Directory Creation:
Using bash scripting, I created 90 directories in seconds with a simple loop. Instead of manually creating each folder, a single script did the job efficiently with arguments! For example, I ran ./createDirectories.sh
day 1 90 and instantly generated directories from day1 to day90.
✅ Backup Automation:
I explored how to back up all the work I've done so far using shell scripts. Automating backups is useful for maintaining stability as a #DevOpsEngineer.
✅ Explored Cron VS Crontab
𝗪𝗵𝗮𝘁 𝗶𝘀 𝗖𝗿𝗼𝗻?
As humans, we can't perform repetitive tasks manually all the time. Cron helps us schedule tasks to run automatically at specific times or intervals, whether it’s for a one-time execution or on a recurring basis.
𝗖𝗿𝗼𝗻 𝗶𝘀 𝗮 𝘀𝗲𝗿𝘃𝗶𝗰𝗲 𝘁𝗵𝗮𝘁 𝗮𝗹𝗹𝗼𝘄𝘀 𝗗𝗲𝘃𝗢𝗽𝘀 𝗘𝗻𝗴𝗶𝗻𝗲𝗲𝗿𝘀 𝘁𝗼 𝘀𝗰𝗵𝗲𝗱𝘂𝗹𝗲 𝗮𝗻𝗱 𝗮𝘂𝘁𝗼𝗺𝗮𝘁𝗲 𝘁𝗮𝘀𝗸𝘀 𝗮𝘁 𝘀𝗽𝗲𝗰𝗶𝗳𝗶𝗲𝗱 𝗶𝗻𝘁𝗲𝗿𝘃𝗮𝗹𝘀.
The actual execution of these scheduled tasks is managed through crontab.
𝗪𝗵𝗮𝘁 𝗶𝘀 𝗖𝗿𝗼𝗻𝘁𝗮𝗯?
Crontab refers to files used to schedule jobs (tasks) to run in the background. To set up a crontab, you need to create a file that contains:
1. What task will execute
2. At what time it will execute
3. How frequently it will execute
𝗞𝗲𝘆 𝗖𝗼𝗺𝗽𝗼𝗻𝗲𝗻𝘁𝘀 𝗼𝗳 𝗮 𝗖𝗿𝗼𝗻𝘁𝗮𝗯 𝗘𝗻𝘁𝗿𝘆
There are five fields to remember when writing a crontab:
• Minute
• Hour
• Day of the Month
• Month
• Day of the Week
• Command
𝗘𝘅𝗮𝗺𝗽𝗹𝗲
Suppose you want to run a job on the 1st and 5th days of May, every minute and every hour:
To configure a cron job to run at a specific time, you can use an asterisk (*) for repetition or specify particular times.
* 𝟭,𝟱 𝟱 𝗲𝗰𝗵𝗼 "𝘁𝗵𝗶𝘀 𝗶𝘀 𝗻𝗲𝘄" > /𝗵𝗼𝗺𝗲/𝘂𝗯𝘂𝗻𝘁𝘂/𝗰𝗿𝗼𝗻_𝗳𝗶𝗿𝘀𝘁.𝘁𝘅𝘁
This line schedules a command to create a new file and execute the specified command.
𝗜𝗺𝗽𝗼𝗿𝘁𝗮𝗻𝘁 𝗖𝗿𝗼𝗻𝘁𝗮𝗯 𝗖𝗼𝗺𝗺𝗮𝗻𝗱𝘀
Remember these three key commands related to crontab:
1. 𝗧𝗼 𝘃𝗶𝗲𝘄 𝘁𝗵𝗲 𝗰𝘂𝗿𝗿𝗲𝗻𝘁 𝗰𝗿𝗼𝗻𝘁𝗮𝗯 𝗿𝘂𝗻𝗻𝗶𝗻𝗴:
$crontab -l
2. 𝗧𝗼 𝗰𝗿𝗲𝗮𝘁𝗲 𝗼𝗿 𝗲𝗱𝗶𝘁 𝗮 𝗰𝗿𝗼𝗻𝘁𝗮𝗯:
$crontab -e
𝘕𝘰𝘵𝘦: 𝘐𝘧 𝘺𝘰𝘶’𝘳𝘦 𝘶𝘴𝘪𝘯𝘨 𝘤𝘳𝘰𝘯𝘵𝘢𝘣 𝘧𝘰𝘳 𝘵𝘩𝘦 𝘧𝘪𝘳𝘴𝘵 𝘵𝘪𝘮𝘦, 𝘪𝘵 𝘮𝘢𝘺 𝘢𝘴𝘬 𝘺𝘰𝘶 𝘵𝘰 𝘴𝘦𝘭𝘦𝘤𝘵 𝘢𝘯 𝘦𝘥𝘪𝘵𝘰𝘳 (𝘦.𝘨., 𝘷𝘪𝘮, 𝘷𝘪, 𝘯𝘢𝘯𝘰).
𝗛𝗼𝘄 𝗗𝗼𝗲𝘀 𝗮 𝗗𝗲𝘃𝗢𝗽𝘀 𝗘𝗻𝗴𝗶𝗻𝗲𝗲𝗿 𝗧𝗵𝗶𝗻𝗸?
If you need to check disk space every day, follow these steps:
1. Create a script to check disk space.
2. Create a log file (e.g., log_file.txt) to record the logs.
3. Automate the process using crontab:
* 𝗯𝗮𝘀𝗵 𝘀𝗰𝗿𝗶𝗽𝘁.𝘀𝗵 >> 𝗹𝗼𝗴_𝗳𝗶𝗹𝗲.𝘁𝘅𝘁
𝘛𝘩𝘦 >> 𝘰𝘱𝘦𝘳𝘢𝘵𝘰𝘳 𝘸𝘪𝘭𝘭 𝘢𝘱𝘱𝘦𝘯𝘥 𝘰𝘶𝘵𝘱𝘶𝘵 𝘵𝘰 𝘵𝘩𝘦 𝘭𝘰𝘨 𝘧𝘪𝘭𝘦.
✅ Managed users in Linux by creating and displaying user details.
1. Create a New User
sudo useradd <username>
example:
sudo useradd amit
2. Create a New User with Home Directory
sudo useradd -m <username>
Creates a new user and also sets up a home directory for them.
example:
sudo useradd -m amit
3. Set/Change Password for a User
sudo passwd <username>
example:
sudo passwd amit
4. Modify User Information
sudo usermod <options> <username>
Modifies a user's properties, such as their login shell, home directory, etc.
Example:
sudo usermod -s /bin/bash amit # Changes shell to bash
sudo usermod -d /new/home amit # Changes home directory
5. Add a User to a Group
sudo usermod -aG <groupname> <username>
Adds the user to a supplementary group.
Example:
sudo usermod -aG sudo amit # Adds amit to the sudo group
6. Delete a User
sudo userdel <username>
Deletes a user, but keeps their home directory and files intact.
Example:
sudo userdel amit
7. Delete a User and Their Home Directory
sudo userdel -r <username>
Deletes the user and their home directory.
Example:
sudo userdel -r amit
8. View All Users
cat /etc/passwd
Displays a list of all users in the system along with their information.
9. List User Groups
groups <username>
Lists the groups the user belongs to.
groups amit
10. Lock a User Account
sudo usermod -L <username>