π Automating Nginx & Custom Scripts with Cronjobs on EC2 Ubuntu


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
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 |
π 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 keySecurity 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 hitEnter
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:
Noticed the permission error related to
myscript.log
.Checked the file ownership it was owned by root.
Changed the ownership from
root
to theubuntu
user usingchown
.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
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 |
π 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
π§ Email: gujjarapurv181@gmail.com
π GitHub: github.com/ApurvGujjar07
πΌ LinkedIn: linkedin.com/in/apurv-gujjar
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."