• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

How to Setup Cron Job on Ubuntu 18.04

Written by Admin, Updated On June 1, 2019
cron, cronjob, schedule, ubuntu
Setup Cron Jobs in Ubuntu 18.04

CronJob is a task scheduler in Linux. It will schedule a task at a specific time or repeat automatically itself after a duration. Using this you can schedule scripts to be executed periodically. It is usually used for system admin jobs such as backups or cleaning temp directories and more. This tutorial will describe how to set up a Cron Job on Ubuntu system.

Prerequisites#

Before you starting, you should have logged in with non-root user account with sudo privileges.

Install Crontab#

By default, Ubuntu 18.04 server have pre-installed crontab so you do not need to install it again. For any reason you removed or you are working on desktop then you need to following below steps to install crontab.

First of all, you should update package manager index list by typing:

sudo apt update

Next, install crontab by executing the below command:

sudo apt install cron

You can verify installation by running below command:

sudo systemctl status cron

It should display output as below:

● cron.service - Regular background program processing daemon
    Loaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: enabled)
    Active: active (running) since Mon 2019-05-27 03:44:33 UTC; 4 days ago
      Docs: man:cron(8)
  Main PID: 814 (cron)
     Tasks: 1 (limit: 1152)
    CGroup: /system.slice/cron.service
            └─814 /usr/sbin/cron -f

Edit crontab with text editor#

Issue below command to open crontab with text editor:

crontab -e

If you are first time to run this command it might ask you to select text editor. You can choose editor as per your choice. It will show output as given below:

After making changes you need to use Ctrl+X and press y to quit and save.

Crontab lines#

Each line has five time-and-date fields, followed by a command, followed by a newline character. The fields are separated by spaces.

The syntax of crontab entry should be as follow:

* * * * * command to be executed
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)

Examples of Cron Jobs#

Cron which will run at every minute:

 * * * * /usr/bin/YourDirectory/YourCommand 

To run cron at every 10 minutes:

10 * * * * /usr/bin/YourDirectory/YourCommand

Cron will run every hour (when the minute will become zero)

0 * * * * /usr/bin/YourDirectory/YourCommand

To set cron at midnight

0 0 * * * /usr/bin/YourDirectory/YourCommand

The cron should run at 7 AM

0 7 * * * /usr/bin/YourDirectory/YourCommand

Advanced examples of cron#

Comma-separated values can be used to run more than one instance of a particular command within a time period. Dash-separated values can be used to run a command continuously.

To execute command at 6:30 am on 10th days of month January, April, July and October you can set as below:

30 6 10 Jan,Apr,Jul,Oct * /usr/bin/YourDirectory/YourCommand

The dash represents range so below crontab will run from 21st to 25th day of every month:

0 0 21-25 * * /usr/bin/YourDirectory/YourCommand

You also can use division operator in crontab. When you have requirement such as run cron after every 10th minute then you can use as below:

*/5 * * * * /usr/bin/YourDirectory/YourCommand

Conclusion#

You have learned how to set up cron job on Ubuntu 18.04. If you want to learn more example you can get it here

If you have any query or suggestion you can leave comment below.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article Tail Command in Linux
Next Article   How to Install Yarn on CentOS 7

Related Posts

  • How to Install Apache, MySQL, PHP (LAMP) on Ubuntu 22.04

    How to Install LAMP on Ubuntu 22.04

    March 20, 2023
  • How to Install LEMP Stack on Ubuntu 22.04

    How to Install LEMP Stack on Ubuntu 22.04

    March 18, 2023
  • How to Install Memcached on Ubuntu 22.04

    How to Install Memcached on Ubuntu 22.04

    March 16, 2023

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook