How to Create and Manage Cron Jobs on Linux

  260 views   6 months ago Ubuntu

Cron job is time based job schedule which runs periodically at fixed set of time in linux and unix-like computer systems. With the help of Cron tab, you can run commans periodically and do task in your server.

In this article, we will show you how to views cron jobs and create new cron job.

To view cron jobs file, run below command:

sudo crontab -e

This will open cron job file:

# Edit this file to introduce tasks to be run by cron.

# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task

# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').

# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.

# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).

# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/

# For more information see the manual pages of crontab(5) and cron(8)

# m h  dom mon dow   command

If you only want to see cron jons without openning file, run command:

crontab -l

To see cron job for specific user, run command:

crontab -u username -e

To set new cron job, add the line at the end of file  

* * * * * command --arguments output

The first 5 fields are for specific time of the job. You can see and set the time for cronjob at CronGuru.
The sixth field is the command which you want to execute given time period.
The last output field is optional. If you want to output cron results, then you can specify.

Here are the few examples of cron jobs:

Description Command
Run every 1 minute * * * * * /root/backup.sh
Run every hour 0 * * * * wget https://website.com/
Run every 12 hour 0 */12 * * * curl -s http://website.com/backup.php
Run every month 1st date 0 0 1 * * command

If you want graphical UI to learn and set time for cronjob, you can see Crontab generator

In this article, we have discussed how to view and create new Cron job. I hope you liked this article.

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]