How to schedule crontab in Unix Operating Systems

How to schedule crontab in Unix Operating Systems.

 
If you need to run some script at a specific time in a Unix box and wish to do so without manually executing it you could leverage on a program called cron. Through cron you can schedule a script to be executed at any specific time you wish. However the lowest value of time that can be scheduled is minute.

Crontab - Crontab is where you configure what interval and which script to execute.

To see crontab entry.
[root@myserver]# crontab -l

To edit crontab entry.
[root@myserver]# crontab -e


How to schedule cron to execute a script.
You have to edit the crontab to the below format in order for cron to execute your script at your desired time.
*  *  *  *  *  /root/script.sh
| | |  | |
| | |  | |
| | |  | |_____  day of week (0 - 6) (0 to 6 are Sun to Sat)
| | |  |_______ month (1 - 12)
| | |_________ day of month (1 - 31)
| |___________ hour (0 - 23)
|_____________ min (0 - 59)


Examples.
To execute script /root/script.sh every Saturday 1am.
0 1 * * 5 /root/script.sh

To execute script /root/script.sh on every weekdays at 2am.
0 2 * * 1-5 /root/script.sh

To execute script /root/script.sh every 15min daily.
0,15,30,45 * * * * /root/script.sh

To execute script /root/script.sh every 15min daily and output the output of /root/script.sh to /root/script_output.txt.
0,15,30,45 * * * * /root/script.sh >> /root/script_output.txt 2>&1
*Note: 2>&1 means standard error, STDERR (2) store to standard output, STDOUT (1). & infront of 1 (STDOUT) means
           the standard output is stored to the file stated which in this case is /root/script_output.txt.

To execute script /root/script.sh every 15min daily and tell it not to show any output.
0,15,30,45 * * * * /root/script.sh >> /dev/null 2>&1
*Note: /dev/null is commonly referred to black hole where everything directed to it is discarded.


Default location of crontab for all users in most Unix operating system.
The default location where crontab is stored is at /var/spool/cron*
For Linux it is at /var/spool/cron/
For Solaris, AIX and HP-UX it is at /var/spool/cron/crontabs/
0 (0)
Article Rating (No Votes)
Rate this article
Attachments
There are no attachments for this article.
Comments
There are no comments for this article. Be the first to post a comment.
Full Name
Email Address
Security Code Security Code
Related Articles RSS Feed
OpenSSL: Check If Private Key Matches SSL Certificate & CSR
Viewed 4252 times since Mon, Feb 18, 2019
List of 10 Must Know Oracle Database Parameters for Database Administrator
Viewed 147662 times since Thu, Jun 21, 2018
Method 2 – Use shell scripts How to install yum cron on a CentOS/RHEL 6.x/7.x
Viewed 5295 times since Tue, Dec 4, 2018
CentOS / RHEL 7 : Configuring an NFS server and NFS client Linux NFS
Viewed 18816 times since Fri, Feb 21, 2020
How to remove CTRL-M (^M) characters from a file in Linux
Viewed 3887 times since Thu, Feb 7, 2019
HOWTO: Use SSL/port 465 in smarthost stunnel
Viewed 4816 times since Fri, Sep 28, 2018
RHEL: Extending the maximum inode count on a ext2/ext3/ext4 filesystem
Viewed 4774 times since Sun, May 27, 2018
20 Linux YUM (Yellowdog Updater, Modified) Commands for Package Management YUM
Viewed 12315 times since Thu, Oct 25, 2018
Install OpenVPN On CentOS / RHEL 7
Viewed 4037 times since Fri, May 15, 2020
Linux - How to get IP and MAC address of ethernet adapter in Linux
Viewed 3711 times since Fri, Jun 8, 2018