Today I am going to tell you guys how you can schedule a job/file/command in Linux. Recently I have created a shell script to table backup of OBIA components. I need to run the script manually once in a week. But now my client wanted to schedule the shell script, so that it will run the file automatically and take the backup.
There are some tools available which can be used to schedule the scripts. But here I am going to tell you, through command prompts how can you do the same.
"crontab" is is file where we can write down all the commands whatever we want to run on a schedule basis. Each user has their own crontab and commands in any given crontab will be executed as the user who owns the crontab. Though these files are in /var/spool/cron/crontabs, they are not intended to be edited directly. crontab file will be executed by cron program.
1. crontab -l : To view the commands into crontab file
2. crontab -e : To edit the file.
3. crontab -r : To remove the file.
An example of crontab format with commented fields is as fellows:
# Minute Hour Day of Month Month Day of Week Command
# (0-59) (0-23) (1-31) (1-12 or Jan-Dec) (0-6 or Sun-Sat)
0 2 12 * * /u01/BI_Bckup/backup.sh
Examples:
Run /usr/bin/test.sh at 12.59 every day and suppress the output (redirect to null)
59 12 * * * simon /usr/bin/test.sh
# Run demo.sh everyday at 9pm (21:00)
0 21 * * * demo.sh
# Run demo.sh every Tuesday to Saturday at 1 am (01:00)
0 1 * * 2-7 demo.sh
# Run demo.sh at 07:30, 09:30 13:30 and 15:30
30 07,09,13,15 * * * demo.sh