Cron jobs worth setting up on a small website: seven ideas that save time

A cron job is for anything you would otherwise repeat by hand at the same time every day or week: taking a copy of the database, deleting temporary files, importing a price list, sending a summary by e-mail. If you already know how to create one, here are seven concrete uses for a small site on shared hosting. To create one, see cron jobs: what they are for and how to create one.

Seven ideas, most useful first

1 A database copy every night. A mysqldump into a folder outside public_html, named after the day. It is your insurance against a mistake of yours or a plugin’s. The steps are in automatic backups with cron and mysqldump.
2 Clearing out old files. Exports, temporary files and old copies fill the space without anyone noticing. For example, find /home/ACCOUNT/exports -type f -mtime +7 -delete removes whatever is older than seven days. Run it first with -print in place of -delete to see what would go.
3 Replacing the WordPress scheduler with a real cron job. Scheduled posts go out on time even with no visitors. See swapping WP-Cron for a real server cron job.
4 Calling an address on your own site. Many applications ship a file made for this, such as /usr/bin/curl -s "https://yourcompany.com/cron.php?key=SECRET" >/dev/null 2>&1. Put a key in the address, or anyone can call it.
5 Importing data from outside. A price or stock list a supplier drops every night, read by a PHP script. See running a PHP script from a cron job.
6 Sending a report. Today’s orders, enquiries received, space used. See a scheduled report by e-mail.
7 Warning you when something fails. A script that checks a page or a file and sends a notice. See getting an alert when your site goes down.

How often, and where to see the result

Job A reasonable frequency Where you see it ran
Database copy Once a day, in the small hours. The new file in the backup folder.
Clearing files Once a day or once a week. A smaller folder; use >> for a log.
WordPress cron Every five minutes. A scheduled post that goes out on time.
Importing data When the supplier updates, plus a margin. The modification date of the file or table.
E-mail report Once a day or once a week. The e-mail in your inbox.
A cron job spends your account’s resources like a visitor does. Do not schedule anything every minute unless you must: if one run takes longer than the interval they start piling up. The reasoning is in cron jobs.
Keep scripts outside public_html, in a folder such as /home/ACCOUNT/scripts. Nobody can open them from a browser, and they stop depending on the site’s address. Add >> /home/ACCOUNT/cron.log 2>&1 to the end of the line for the first weeks, so you have a record of what happened.

Want a plan with cPanel and scheduled tasks? See the hosting plans.

See the plans

SEE ALSO

Cron jobs: what they are for and how to create one

A cron job that does not run or sends no output: a checklist

Cron schedule examples: every 15 minutes, weekdays at 8, the first of the month

Hosting plans

RECOMMENDED PRODUCT

Web hosting with cPanel

Domain and SSL included, daily backups and the panel you already know. from ₦9.900,00/mo (3-year plan, with coupon)

See plans
  • 0 Users Found This Useful
Was this answer helpful?