AMP AMP

How to schedule a job using crontab

To schedule a job using crontab

Introduction

Cron is used to scheduling commands; it is a standard Unix utility. It helps to maintain the software environment and also known as job crons. Crons is a long-running process to schedule work at a fixed time and date. It is connected to the server to complete the task automatically and also a resourceful tool that helps to save time.

Installation Procedure

Checking the installed OS version

linuxhelp@linuxhelp:~$ lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 21.04
Release:	21.04
Codename:	hirsute

Log in as a Root user by the following command

linuxhelp@linuxhelp:~$ sudo -s
[sudo] password for linuxhelp:

Editing the crontab file by the following command

root@linuxhelp:/home/linuxhelp# crontab -e
no crontab for root - using an empty one

Select an editor.  To change later, run 'select-editor'.
  1. /bin/nano      <---- easiest
  2. /usr/bin/vim.tiny
  3. /bin/ed

Choose 1-3 [1]: 

On Ubuntu based system it will ask for the editor for the first time to choose one or by default it uses the nano editor. Opened the Crontab file

Snap 1

Add the following lines, this will execute

10 14 * * * touch file
11 14 * * * mkdir folder
12 14 * * * cp file file1

Cron job scheduled

Changing directory by using following command

root@linuxhelp:/home/linuxhelp# cd /root/

Long listing the home directory to view the files created by crontab

root@linuxhelp:~# ls -la
total 40
drwx------  6 root root 4096 Oct  6 13:54 .
drwxr-xr-x 20 root root 4096 Sep  8 16:50 ..
-rw-------  1 root root  627 Oct  6 13:47 .bash_history
-rw-r--r--  1 root root 3106 Aug 15  2019 .bashrc
drwx------  2 root root 4096 Apr 20 16:20 .cache
-rw-r--r--  1 root root    0 Oct  6 13:52 file
-rw-r--r--  1 root root    0 Oct  6 13:54 file1
drwxr-xr-x  2 root root 4096 Oct  6 13:53 folder
drwxr-xr-x  3 root root 4096 Oct  6 13:48 .local
-rw-r--r--  1 root root  161 Sep 16  2020 .profile
-rw-r--r--  1 root root   66 Oct  6 13:48 .selected_editor
drwxr-xr-x  3 root root 4096 Sep  8 17:00 snap

By this schedule a job using crontab comes to end

FAQ
Q
How to Remove a crontab File
A
Remove the crontab file. $ crontab -r [ username ] where username specifies the name of the user's account for that you want to remove a crontab file.
Q
How do I list all cron jobs?
A
In Ubuntu or debian, you can view crontab by /var/spool/cron/crontabs/
Q
What is cron job schedule?
A
Cron jobs schedule is recurring intervals, specifically used by Unix-cron format.
Q
How does a cron job work?
A
Cron Jobs are providing automate commands or scripts on your server to complete repetitive tasks spontaneously.
Q
What is cron used for?
A
Cron is used to scheduling commands for an automatic course of action at specific intervals.