Back to blogs

Why Cron Jobs Will Save Your App from Crashing

October 3, 2025
7 min read
Why Cron Jobs Will Save Your App from Crashing

Whenever I build an app, one thought keeps coming back: what happens when something goes wrong at night, or when there is a sudden traffic spike, or when background tasks pile up and never get cleaned? The truth is, many crashes or slowdowns happen because we forget to take care of the boring, repetitive maintenance tasks. Over the years, I have learned that a simple solution like setting up cron jobs can save your app from crashing and make it much more reliable.


In this post, I’ll explain in my own way why cron jobs are important, how I use them, common mistakes to avoid, and even how you can run cron jobs inside Supabase if you already use it for your app.


What Exactly is a Cron Job

To put it in the simplest way, a cron job is just a scheduled task that runs automatically at the time you choose. On Linux and other Unix systems, you write a cron expression (like 0 3 * * *) and point it to a command or script. The system checks the schedule and when the time matches, it runs the task.

The magic is that once you set it, you do not have to manually trigger the job again. It keeps running in the background, taking care of the tasks you planned for it.


How Cron Jobs Help Prevent Crashes

Here are some real scenarios where cron jobs have helped me or could help you.


1. Cleaning Up Resources

Your app may create temporary files, logs, or caches. If you never clean them up, the disk usage keeps growing until the server runs out of space. That can lead to your app crashing.

A cron job can run every night and clean up for you. For example:

0 2 * * * /usr/bin/php /path/to/cleanup.php >> /var/log/cleanup.log 2>&1

This job runs every day at 2 AM and removes old files or compresses logs. Simple, but powerful.


2. Database Maintenance

Databases grow fast. Imagine you are storing user activity logs every second. If you keep all of them forever, queries will get slower, indexes will become heavy, and eventually your app will start failing.

A cron job can remove old records automatically. Example:

0 4 * * * /usr/bin/psql mydb -c "DELETE FROM logs WHERE created_at < now() - interval '30 days';"

This runs every day at 4 AM and deletes logs older than 30 days.


3. Retrying Failed Jobs

Sometimes jobs fail because of temporary issues like an API not responding or a network glitch. If you do not retry them, some parts of your app remain stuck.

A cron job can check failed jobs every few minutes and retry them:

*/5 * * * * /usr/bin/php /path/to/retry_jobs.php

This runs every 5 minutes and makes sure failed jobs get a second chance.


4. Health Checks

Cron jobs can be used to regularly check if your system is healthy. For example, you can ping your services, check database connections, or verify important rules like “no user balance is negative.” If something is wrong, the cron job can send you an alert.

*/10 * * * * /usr/bin/python /path/to/health_check.py

This runs every 10 minutes. If the check fails, you can send an email or push a notification.


How Cron Timing Works

Cron uses a simple five-part timing pattern:

┌ minute (0–59)
│ ┌ hour (0–23)
│ │ ┌ day of month (1–31)
│ │ │ ┌ month (1–12)
│ │ │ │ ┌ day of week (0–6, Sunday=0)
* * * * *


Some quick examples:

  1. 0 0 * * * means every day at midnight
  2. */5 * * * * means every 5 minutes
  3. 0 3 * * 1 means every Monday at 3 AM
  4. 0 0 1 * * means the first day of every month at midnight
  5. * * * * * means every minute (not a good idea for heavy jobs)


One thing I learned is to always leave enough gap between jobs. If a job takes longer to run than the interval you set, it might overlap with the next run. This can eat resources and even bring down your app. Add enough buffer time or use a lock so only one instance runs at a time.

Also, cron jobs do not use the same environment as your shell. That means you need to set full paths and add any environment variables needed. Always redirect output to a log file so you can see what happened.


Mistakes I Have Seen with Cron Jobs

Cron jobs are useful but they are not perfect. Here are common problems I have seen:

  1. Jobs fail silently because no one set up logging.
  2. A small typo in the cron expression stops the job from running at all.
  3. Scripts expect environment variables that do not exist in cron.
  4. Jobs overlap when scheduled too close together.
  5. Too many heavy jobs run at the same time and overload the server.
  6. Infrastructure changes, but the cron config is never updated.

The fix is simple. Add proper logging, stagger the timing of heavy jobs, keep jobs short, and always test them before scheduling.


Supabase Cron

If you are already using Supabase, you do not need to set up a separate server for cron jobs. Supabase has Supabase Cron, which is built on top of pg_cron.

Some things I like about it:

  1. You can schedule jobs directly from SQL or from the dashboard.
  2. Jobs can run as SQL statements, database functions, or even call HTTP endpoints and Edge Functions.
  3. Logs are available so you know what is happening.
  4. It supports flexible schedules, even sub-minute.
  5. Supabase recommends not running more than 8 jobs at the same time and keeping jobs under 10 minutes.


Example: Cleaning up Sessions

select cron.schedule(
'cleanup-sessions-daily',
'0 3 * * *',
$$ DELETE FROM sessions WHERE last_active < now() - interval '30 days'; $$
);


Example: Calling an Edge Function Every Minute

select cron.schedule(
'sync-every-minute',
'* * * * *',
$$
select net.http_post(
url := 'https://your-project.supabase.co/functions/v1/sync',
headers := jsonb_build_object('Content-Type', 'application/json'),
body := jsonb_build_object('time', now())
);
$$
);


And if you need to cancel a job later:

select cron.unschedule('sync-every-minute');


You can even check the history of job runs inside Supabase tables dashboard.


Best Practices I Follow

  1. Keep jobs short and simple.
  2. Avoid overlapping runs.
  3. Log everything to files or tables.
  4. Set up alerts when jobs fail.
  5. Spread out heavy jobs instead of running them all at the same time.
  6. Store cron definitions in version control so you never lose them.
  7. Always test with a dry run before scheduling.
  8. Make cron tasks safe to run multiple times, so if they retry, nothing breaks.


Why Cron Jobs Really Matter

Cron jobs are not just “extra features.” They are your safety net. They quietly handle cleanup, retries, monitoring, and database care while you focus on building features. They reduce the chances of sudden crashes caused by resource exhaustion or forgotten maintenance.

If you use Supabase, you already have a simple and reliable way to schedule cron jobs without managing servers. It is worth taking a look if you want peace of mind.

The bottom line is this: cron jobs are like the maintenance you do for your car or your house. Skip it for too long and you will face bigger problems later. Set them up, and your app will run smoother and crash less often.

cron jobscron job exampleswhat is a cron jobschedule tasks in appprevent app crashingapp maintenance automationcron job best practicessupabase croncron job for database cleanupscheduled tasks in linuxcron job for health checksretry failed jobs cronwhy cron jobs are importantsupabase scheduled functionscron job tutorial

Recent Blogs

View All