πŸš€ Automating Nginx & Custom Scripts with Cronjobs on EC2 Ubuntu

Gujjar ApurvGujjar Apurv
7 min read

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

🟒 . Cron Time Format Explanation

# β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ 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>

πŸ”§ Useful Crontab Commands for Linux Automation

CommandDescription
crontab -eEdit the current user’s crontab file to schedule tasks
crontab -lList/show all scheduled cron jobs for the current user
crontab -rRemove the current user’s crontab (all jobs)
crontab -u <user> -lView cron jobs of a specific user (run as root)
sudo service cron statusCheck the status of the cron service
sudo service cron startStart the cron service (if stopped)
sudo service cron stopStop the cron service
sudo service cron restartRestart the cron service

πŸ“ Task Objective

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

1️⃣ Launching an EC2 Ubuntu Instance

  • 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

🧩 Step 2: Manually Installing and Verifying Nginx Web Server on EC2

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.

βš™οΈ Commands Used for Nginx Installation

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

βœ… How I Verified the Installation

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:

πŸš€ Step 3: Creating the Shell Script (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.

πŸ”§ 1. Open a new script file using Vim

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.


πŸ“ 2. Script Content

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.


πŸ’Ύ 3. Save and Exit the File

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.


βœ… 4. Make the Script Executable

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.

Fixing Permission Error (optional 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:

  1. Noticed the permission error related to myscript.log.

  2. Checked the file ownership it was owned by root.

  3. Changed the ownership from root to the ubuntu user using chown.

  4. After that, the script executed successfully and started logging entries as expected.

πŸ•“ Step 4: Automating Tasks Using Cron Jobs

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.


βœ… Automation Goals for This Project

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


πŸ› οΈ Editing the Crontab

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.


πŸ“„ Cron Job Entries I Added

# 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

πŸ“Œ What Each Cron Job Does

TimeTask DescriptionCommand Executed
0 7 * * *Daily at 7:00 AMRestarts the Nginx service via systemctl
0 2 * * *Daily at 2:00 AMExecutes the custom script myscript.sh
* * * * *Every minute (for testing/debug only)Appends a timestamp to /home/ubuntu/testcron.log

πŸ” Verifying If Cron Jobs Are Working

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:

πŸ” Viewing Scheduled Cron Jobs Using crontab -l

When 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

βœ… Command:

crontab -l

πŸ“Œ What it does:

This command lists all the cron jobs currently scheduled for the logged-in user.

πŸ“Š Confirming Cron Daemon Status

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.

πŸ‘¨β€πŸ’» About the Author

This series isn't just about using AWS; it's about mastering the core services that power modern cloud infrastructure.


πŸ“¬ Let's Stay Connected

1
Subscribe to my newsletter

Read articles from Gujjar Apurv directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Gujjar Apurv
Gujjar Apurv

Gujjar Apurv is a passionate DevOps Engineer in the making, dedicated to automating infrastructure, streamlining software delivery, and building scalable cloud-native systems. With hands-on experience in tools like AWS, Docker, Kubernetes, Jenkins, Git, and Linux, he thrives at the intersection of development and operations. Driven by curiosity and continuous learning, Apurv shares insights, tutorials, and real-world solutions from his journeyβ€”making complex tech simple and accessible. Whether it's writing YAML, scripting in Python, or deploying on the cloud, he believes in doing it the right way. "Infrastructure is code, but reliability is art."