VPS

How to Setup a Cron Job on VPS

Introduction

Are you regularly doing some tasks in Linux, like mailing disk usage to yourself, archiving logs, deleting temp directories, etc.? In today’s world time is precious, why waste it on tasks which could be scheduled to run on a recurring basis? Cron is here to help! Cron is a time-based job scheduler, system administrators use cron to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates, or intervals. This tutorial will show how to setup Cron Job on Linux VPS.

What you’ll need

Before you begin this guide you’ll need the following:

  • SSH access to VPS

Step 1 — Understanding The Syntax

Cron service uses Crontab (Cron table) file which is usually located in /etc/crontab, /var/cron/tabs or /var/spool/cron/ directories. Each line in the crontab represents a scheduled job. Just run this command in your shell to list all cron jobs:

crontab -l

If the file is empty, do not worry, it seems that you do not have any scheduled jobs. An example of cron job would be like:

0 1 * * * /usr/local/cpanel/scripts/cpbackup

In this sample, a cPanel backup script is executed every hour. As you see, the actual job line consists of six fields:

┌───────────── min (0 - 59)
│ ┌────────────── hour (0 - 23)
│ │ ┌─────────────── day of month (1 - 31)
│ │ │ ┌──────────────── month (1 - 12)
│ │ │ │ ┌───────────────── day of week (0 - 6) (0 to 6 are Sunday to
│ │ │ │ │                  Saturday, or use names; 7 is also Sunday)
│ │ │ │ │
│ │ │ │ │
0 1 * * * /usr/local/cpanel/scripts/cpbackup

If you still have doubts, check out manual pages of Cron by executing man cron on your console.

Step 2 — Setting up a Cron job

Edit the crontab by running following command:

crontab -e

IMPORTANT! Do not ever edit the crontab file directly, always use the crontab -e command.

Now just list your scripts which should be scheduled or run on a specific time. Use the structure provided in Step 1. Here are some examples:

  1. Run on a recurring basis:
    • 0 0 1 * * – once a month on the first day
    • 0 0 * * 0 – once a week
    • 0 0 * * * – once a day at midnight
  2. Run on a specific time:
    • 20 22 3 * * – every 3rd day of each month at 22:20
    • */30 * * * * – every 30 minutes
    • 0 8 * * 1,2,3,4,5 from Monday to Friday at 08:00

Following the schedule do not forget to write the actual path to your script. You can generate any timetable you want via http://crontab-generator.org

Once you are finished, close the crontab by writing this command in the editor:

:wq

Conclusion

From now on you have the knowledge how to setup cron jobs in order to automate scripts or commands on Linux. You can specify an exact time, date or just reoccurrence.

About the author

Edgaras G.

Edgaras is a veteran server administrator at Hostinger. He makes sure that every server runs at full throttle and has all the latest technological advancements. When he's not working, Edgaras enjoys skiing and exploring the world.

Add Comment

Click here to post a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.