🚀 Automating Nginx & Custom Scripts with Cronjobs on EC2 Ubuntu

Search for a command to run...

No comments yet. Be the first to comment.
Learn AWS from basics to advanced with daily hands-on tasks and real-world projects. Master core services like EC2, S3, IAM, Lambda, and more all explained in a practical, job-ready DevOps format.
🚀 In this blog, we’ll walk through how to connect multiple Amazon VPCs using AWS Transit Gateway, allowing EC2 instances in different VPCs to communicate with each other. This is useful in scenarios where you need scalable, centralized connectivity ...
Master GitLab CI/CD Variables, Secrets Management, Artifacts & Best Practices 📖 Introduction In the previous article, we learned how GitLab Runners execute CI/CD pipelines and how to configure Self-H

Learn GitLab Runners, Hosted vs Self-Hosted Runners, Runner Registration, Tags, Pipeline Editor & Parallel Jobs 📖 Introduction In the previous article, we created our first GitLab CI/CD pipeline and

Learn Continuous Integration, Continuous Delivery, Pipelines, Stages, Jobs & .gitlab-ci.yml 📖 Introduction In the previous articles, we explored GitLab fundamentals, repository management, and collab

Learn Docker Scout, Multi-Stage Builds, Docker Hardened Images (DHI), SBOM, Docker Model Runner, Ask Gordon AI, and production-ready container security through practical, real-world examples. Introduc

Import Repositories, Mirroring & Repository Management Best Practices 📖 Introduction In the previous articles, we learned the fundamentals of GitLab, created projects, and configured secure authentic

Modern server management in the cloud demands repeatability and automation. As part of my DevOps skill development, I successfully set up an AWS EC2 instance, deployed an Nginx web server, automated server maintenance using cronjobs, and built custom shell scripts for scheduled tasks. This blog details my hands-on journey, professional approach, and practical learnings
# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12)
# │ │ │ │ ┌───────────── day of week (0 - 6) (Sunday=0 or 7)
# │ │ │ │ │
# │ │ │ │ │
# * * * * * <command-to-execute>
| Command | Description |
|---|---|
crontab -e |
Edit the current user’s crontab file to schedule tasks |
crontab -l |
List/show all scheduled cron jobs for the current user |
crontab -r |
Remove the current user’s crontab (all jobs) |
crontab -u <user> -l |
View cron jobs of a specific user (run as root) |
sudo service cron status |
Check the status of the cron service |
sudo service cron start |
Start the cron service (if stopped) |
sudo service cron stop |
Stop the cron service |
sudo service cron restart |
Restart the cron service |
Goal:
Deploy and manage an AWS EC2 (Ubuntu) instance
Automate Nginx server installation and daily management
Schedule Nginx “restart” at 7 AM daily
Run a custom shell script every day at 2 AM using cronjobs
Achieve all objectives with pure automation—no manual intervention
OS: Ubuntu 22.04 LTS
Instance Type: t2.micro (Free Tier eligible)
Key Pair: Secure EC2 access using .pem SSH key
Security Group:
Allow port 22 (SSH) for terminal access
Allow port 80 (HTTP) for web access (Nginx)
Connect to your instance:
ssh -i "your-key.pem" ubuntu@your-ec2-public-ip
After launching the EC2 instance, the next step was to manually install the Nginx web server. This helps in quickly testing server availability and basic connectivity.
Below are the individual commands I executed one by one:
# Step 1: Update package list
sudo apt update
# Step 2: Install Nginx
sudo apt install nginx -y
# Step 3: Enable Nginx to start on boot
sudo systemctl enable nginx
# Step 4: Start Nginx service
sudo systemctl start nginx
# Step 5: Check Nginx service status
sudo systemctl status nginx
Once the installation was successful:
I copied the public IPv4 address of the EC2 instance.
Opened it in a web browser like this:
http://<your-ec2-public-ip>
If everything is set up correctly, the default Nginx welcome page appears with the message:
myscript.sh)After setting up Nginx, the next part of the task was to create a shell script that performs a simple operation appending a timestamped log to a file every time it runs.
First, I created a new shell script using the Vim editor by running the following command:
vim myscript.sh
This opened the Vim editor where I could write my script.
Inside the myscript.sh file, I added the following lines of code:
#!/bin/bash
echo "Script ran at $(date)" >> /home/ubuntu/myscript.log
📌 This script appends the current date and time to a log file named myscript.log located in the /home/ubuntu/ directory. Every time the script runs, a new entry is added to the log, which is useful for tracking execution.
To save and exit in Vim:
Press Esc
Then type :wq and hit Enter
This saves the script and brings you back to the terminal.
Before we can run the script manually or via a cron job, we need to give it executable permissions. I used the following command:
chmod +x myscript.sh
Now the script is ready to be run manually or scheduled using cron in the next step.
When I tried to run myscript.sh, I got a "Permission denied" error because the log file /home/ubuntu/myscript.log was owned by the root user. This prevented the script from writing to the file.
To fix this issue, I followed these steps:
Noticed the permission error related to myscript.log.
Checked the file ownership it was owned by root.
Changed the ownership from root to the ubuntu user using chown.
After that, the script executed successfully and started logging entries as expected.
One of the most powerful aspects of Linux-based systems is automation — and Cron Jobs are the go-to solution for scheduling repetitive system-level tasks. In this step, I utilized cron to automate essential backend processes on my EC2 instance.
As per the task requirements, I needed to automate the following:
🔁 Restart the Nginx service daily at 7:00 AM
📜 Run a custom shell script daily at 2:00 AM
🧪 (Optional) Test cron functionality by logging timestamps every minute
To configure these cron jobs, I used the crontab utility:
crontab -e
📝 On first run, it prompts to choose an editor — I went with vim, since I’m already comfortable using it.
# Restart Nginx at 7 AM daily
0 7 * * * /bin/systemctl restart nginx
# Run your script at 2 AM daily
0 2 * * * /home/ubuntu/myscript.sh
# Test Cron (runs every minute) - for debugging only
* * * * * echo "Cron Working ✅ $(date)" >> /home/ubuntu/testcron.log
| Time | Task Description | Command Executed |
|---|---|---|
0 7 * * * |
Daily at 7:00 AM | Restarts the Nginx service via systemctl |
0 2 * * * |
Daily at 2:00 AM | Executes the custom script myscript.sh |
* * * * * |
Every minute (for testing/debug only) | Appends a timestamp to /home/ubuntu/testcron.log |
To check the test cron job, I opened the log file:
cat /home/ubuntu/testcron.log
If successful, you’ll see output like this — a new line for every minute passed:
crontab -lWhen working with automated scripts or scheduled tasks in Linux, cron jobs become your best friend. But what if you want to check which cron jobs are already scheduled? That’s where the simple yet powerful command comes in
crontab -l
This command lists all the cron jobs currently scheduled for the logged-in user.
To ensure that the cron service is running correctly, I checked its status using:
sudo systemctl status cron
✅ The output showed:
Status: Active (running)
Logs confirming each cron job run
✅ Conclusion:-
In this task, I demonstrated how to automate tasks using cron jobs on an Ubuntu server. Starting from manually installing Nginx, writing a shell script using vim, assigning it to crontab, and verifying the scheduled jobs ,this step-by-step guide helps streamline repetitive processes efficiently. Crontab is a powerful Linux utility to schedule scripts, system maintenance, backups, and more ultimately enhancing productivity and automation.
This series isn't just about using AWS; it's about mastering the core services that power modern cloud infrastructure.
📧 Email: gujjarapurv181@gmail.com
🐙 GitHub: github.com/ApurvGujjar07
💼 LinkedIn: linkedin.com/in/apurv-gujjar