In this guide, we will explain how to schedule and run tasks (cron jobs) automatically with the help of cron command. Our servers do support the use of cron jobs. Please take note that cron jobs are a Unix-based utility, so you can only run cron jobs on Linux-based servers.
What is a Cron Job?
A cron is a time-based scheduler that runs a command or script on your server automatically. Cron jobs are used for scheduling jobs (commands or scripts) to run periodically at fixed times, dates, or intervals.
It is basically a task scheduler daemon (or process) that runs under a Linux / Unix-like OS. It wakes up every minute and checks planned tasks in CRON TABLE aka crontab. Crontab is a configuration file containing shell commands to run periodically on a given schedule.
Before you start, let's have a look at a few examples of cron job time scheduling and the allowed cron job operators:
┌───────────── min (0 – 59)
│ ┌────────────── hour (0 – 23)
│ │ ┌─────────────── day of the month (1 – 31)
│ │ │ ┌──────────────── month (1 – 12)
│ │ │ │ ┌─ day of week (0 – 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat)
│ │ │ │ │
│ │ │ │ │
* * * * * /path/to/your/script.sh
* * * * * => Run every minute
0 * * * * => Run every Hour
0 0 * * * => Run every midnight
0 0 0 * * => Run every Month
0 0 0 0 * => Run every Weekdays
How to Add a Cron Job via SSH
Step 1: Access your Server via SSH
You need to connect your server via SSH. You can follow this guide on how to connect your server via SSH.
Step 2: Create a Cron Job (Scheduled Task)
Once you are connected to your server through SSH, type the following command to open a crontab file.
# crontab –e
A crontab screen will appear. Now append the entry to schedule a task of your choice.
For example, run /path/to/command at 6:00 AM, every day, enter:
0 6 * * * /path/to/command
Make your desired changes and save by pressing “Ctr+X” and then type “Y”.
Note that all times are in UTC, so set your desired time of execution accordingly.
That’s it! We hope this article was helpful. If you need any help, then feel free to search your query on Cloudways Support Center or contact us via chat (Need a Hand > Send us a Message). Alternatively, you can also create a support ticket.