Back to Blog
Tutorial

How to Run a Cron Job Every 30 Minutes (Complete Guide 2025)

How to Run a Cron Job Every 30 Minutes (Complete Guide 2025)

Need to run a task every 30 minutes? Whether you're monitoring server health, processing queues, or backing up data, running a cron job every half hour is one of the most common scheduling patterns.

In this guide, you'll learn exactly how to set up a cron job to run every 30 minutes with practical examples and troubleshooting tips.

Quick Answer: Cron Expression for Every 30 Minutes

*/30 * * * * /path/to/your/script.sh

This cron expression breaks down as:

  • */30 = Every 30 minutes
  • * = Every hour
  • * = Every day
  • * = Every month
  • * = Every day of the week

Result: Your script runs at :00 and :30 of every hour, 24/7.


Step-by-Step: Setting Up Your 30-Minute Cron Job

Step 1: Open Your Crontab

crontab -e

If this is your first time, you'll be asked to choose an editor (nano is easiest for beginners).

Step 2: Add the Cron Entry

Add this line at the bottom:

*/30 * * * * /home/username/scripts/my-task.sh

Replace:

  • /home/username/scripts/my-task.sh with your actual script path
  • Or use a command directly: */30 * * * * php /var/www/html/cron.php

Step 3: Save and Exit

  • Nano: Press Ctrl + X, then Y, then Enter
  • Vim: Press Esc, type :wq, press Enter

Step 4: Verify It's Scheduled

crontab -l

You should see your new entry listed.


Real-World Examples

Example 1: PHP Script Every 30 Minutes

*/30 * * * * /usr/bin/php /var/www/html/process-queue.php >> /var/log/cron.log 2>&1

What it does:

  • Runs process-queue.php every 30 minutes
  • Logs output to /var/log/cron.log
  • Captures errors with 2>&1

Example 2: Python Script with Virtual Environment

*/30 * * * * cd /home/user/myapp && /home/user/myapp/venv/bin/python scraper.py

What it does:

  • Changes to project directory
  • Uses Python from virtual environment
  • Runs scraper every 30 minutes

Example 3: Database Backup Every 30 Minutes

*/30 * * * * /usr/bin/mysqldump -u root -pPASSWORD mydb > /backups/mydb-$(date +\%Y\%m\%d-\%H\%M).sql

What it does:

  • Creates MySQL backup every 30 minutes
  • Adds timestamp to filename
  • Stores in /backups/ directory

Example 4: API Health Check

*/30 * * * * curl -s https://myapi.com/health | grep -q "ok" || echo "API Down!" | mail -s "Alert" admin@example.com

What it does:

  • Checks API every 30 minutes
  • Sends email if health check fails

Alternative: Run at Specific Minutes

Instead of */30, you can specify exact minutes:

0,30 * * * * /path/to/script.sh

This also runs every 30 minutes (at :00 and :30), but is more explicit.

When to Use Which?

| Expression | Runs At | Best For | |------------|---------|----------| | */30 * * * * | :00, :30 | Simple, easy to remember | | 0,30 * * * * | :00, :30 | Explicit, some prefer this | | 15,45 * * * * | :15, :45 | Run at quarter-past and quarter-to |


Business Hours Only (Every 30 Minutes, 9 AM - 5 PM)

*/30 9-17 * * * /path/to/script.sh

Runs every 30 minutes between 9:00 AM and 5:30 PM, Monday-Sunday.

Weekdays Only (Monday-Friday)

*/30 9-17 * * 1-5 /path/to/script.sh

Runs every 30 minutes during business hours, weekdays only.


Common Mistakes to Avoid

❌ Wrong: Missing Step Value

30 * * * * /script.sh  # Only runs at :30, not every 30 minutes!

✅ Correct: Use Step Notation

*/30 * * * * /script.sh  # Runs every 30 minutes

❌ Wrong: Incorrect Permissions

*/30 * * * * /path/to/script.sh  # Script not executable

Fix:

chmod +x /path/to/script.sh

❌ Wrong: Wrong Path

*/30 * * * * script.sh  # Cron doesn't know where script.sh is

Fix: Always use absolute paths:

*/30 * * * * /home/user/script.sh

Troubleshooting: Cron Job Not Running Every 30 Minutes?

1. Check Cron is Running

sudo systemctl status cron

Or on older systems:

sudo service cron status

2. Check Logs

Ubuntu/Debian:

grep CRON /var/log/syslog | tail -20

CentOS/RHEL:

tail -20 /var/log/cron

3. Test Your Script Manually

/path/to/your/script.sh

If it fails manually, it will fail in cron too.

4. Add Logging

*/30 * * * * /path/to/script.sh >> /tmp/cron-debug.log 2>&1

Then check the log:

tail -f /tmp/cron-debug.log

5. Set Environment Variables

Cron has a minimal environment. Add this at the top of your crontab:

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
SHELL=/bin/bash

*/30 * * * * /path/to/script.sh

Advanced: Running Every 30 Minutes with Offset

Want to run at :15 and :45 instead of :00 and :30?

15,45 * * * * /path/to/script.sh

Or every 30 minutes starting at :10?

10,40 * * * * /path/to/script.sh

How to Stop/Disable Your 30-Minute Cron Job

Option 1: Comment It Out

crontab -e

Add # at the start:

# */30 * * * * /path/to/script.sh

Option 2: Remove the Line

crontab -e

Delete the entire line and save.

Option 3: List and Remove All Cron Jobs

crontab -r  # WARNING: Removes ALL your cron jobs

System-Wide vs User Crontabs

User Crontab (Most Common)

crontab -e
*/30 * * * * /home/user/script.sh

Runs as your user.

System-Wide Crontab (Requires Root)

Edit /etc/crontab:

sudo nano /etc/crontab

Add:

*/30 * * * * root /path/to/script.sh

Notice you must specify the user (root).


Testing Without Waiting 30 Minutes

Method 1: Run Every Minute (Temporarily)

* * * * * /path/to/script.sh

Watch it run every minute, then change back to */30.

Method 2: Set Specific Time

15 14 * * * /path/to/script.sh

Set it to run at 2:15 PM today, wait, then change to */30.

Method 3: Run Manually

/path/to/script.sh

Monitoring Your 30-Minute Cron Jobs

Running critical tasks every 30 minutes? You need monitoring:


Performance Considerations

Will Running Every 30 Minutes Overload My Server?

Depends on what the script does:

Safe:

  • API health checks (< 1 second)
  • Log rotation (< 5 seconds)
  • Email queue processing (< 10 seconds)

⚠️ Watch Out:

  • Database backups (may take minutes)
  • Large file processing
  • Heavy API calls

Best Practice: If a task takes longer than 30 minutes, it will overlap with the next run. Use locking:

*/30 * * * * flock -n /tmp/myscript.lock /path/to/script.sh

This prevents overlapping executions.


Different Intervals Comparison

| Interval | Cron Expression | Runs Per Day | |----------|----------------|--------------| | Every 5 minutes | */5 * * * * | 288 | | Every 15 minutes | */15 * * * * | 96 | | Every 30 minutes | */30 * * * * | 48 | | Every hour | 0 * * * * | 24 | | Every 2 hours | 0 */2 * * * | 12 |


Real-World Use Cases for 30-Minute Cron Jobs

1. Queue Processing

*/30 * * * * php /var/www/artisan queue:work --stop-when-empty

2. Cache Warming

*/30 * * * * curl -s https://mysite.com/cache/warm > /dev/null

3. Social Media Auto-Posting

*/30 * * * * /home/user/scripts/post-to-twitter.py

4. Stock Price Updates

*/30 9-16 * * 1-5 /home/trader/update-stocks.sh

5. Server Monitoring

*/30 * * * * /usr/local/bin/check-server-health.sh

Cron Job Every 30 Minutes: Quick Reference

# Basic - Every 30 minutes, 24/7
*/30 * * * * /path/to/script.sh

# With logging
*/30 * * * * /path/to/script.sh >> /var/log/myscript.log 2>&1

# Business hours only (9 AM - 5 PM)
*/30 9-17 * * * /path/to/script.sh

# Weekdays only
*/30 * * * 1-5 /path/to/script.sh

# With environment variables
*/30 * * * * /bin/bash -c 'source /home/user/.env && /path/to/script.sh'

# Prevent overlapping
*/30 * * * * flock -n /tmp/script.lock /path/to/script.sh

# Alternative notation (explicit minutes)
0,30 * * * * /path/to/script.sh

Need a Reliable Server for Your Cron Jobs?

If you're running critical cron jobs every 30 minutes, you need a reliable VPS:


Conclusion

Running a cron job every 30 minutes is simple:

*/30 * * * * /path/to/your/script.sh

Key Takeaways:

  • ✅ Use */30 for step notation
  • ✅ Or use 0,30 for explicit minutes
  • ✅ Always use absolute paths
  • ✅ Make scripts executable (chmod +x)
  • ✅ Add logging for debugging
  • ✅ Use flock to prevent overlapping runs
  • ✅ Monitor critical jobs

Next Steps:

  1. Generate more cron expressions
  2. Decode existing cron jobs
  3. Save your cron jobs for later

Related Articles:

Keywords: cron job every 30 minutes, crontab every 30 minutes, linux cron 30 minutes, run script every half hour, cron expression 30 minutes, ubuntu cron job 30 minutes