Tuesday, October 2, 2018

crontab command examples

crontab (Job Scheduler) 

Crontab :- Crontab is a job scheduler in Linux.

Crontab Options:-
crontab -l :- Listing all scheduled jobs
crontab -r :- Remove all scheduled job
crontab -e :- Editing the scheduled job
crontab -l -u username :- Listing of all scheduled jon for User


Syntax:-


1) How can we take a backup of all scheduled jobs ?
crontab -l>jobs.txt

2) How we can install jobs in cron by text file ?
crontab jobs.txt

3) How can we schedule a monitoring.sh script for every 10 minutes ?
*/10 * * * * sh /path/monitoring.sh

Note:- path is the path where monitoring.sh script is placed

4) How can we schedule a dailyreport.sh for daily early morning at 3:00 AM ?
00 03 * * * sh /path/dailyreport.sh

5) How can we schedule report.sh for every Monday morning 10.15 AM ?
15 10 * * 1 sh /path/report.sh



6) How can we schedule a job.sh for First Friday of every month at 8:20 AM ?
20 08 01-07 * 5 sh /path/job.sh

7) How can we schedule a.sh for 2:15 PM, 4:15 PM and 6:15 PM ?   
15 02,04,06 * * * sh /path/a.sh

8) How can we schedule fullbackup.sh for 15th Apr 08:30 AM ?
30 08 15 04 * sh /path/fullbackup.sh

9) How can we schedule a cronjob for weekdays during working hours (9:00AM to 6:00PM) ?

00 09-18 * * 1-5 sh /path/os_monitoring.sh

10) How can we scheduled a command for daily ? 
@daily command

Note:- Same way, we can schedule any command/job for below as well

@monthly
@yearly
@reboot

11) How can we disable a scheduled cronjob ?
By adding "#" at the begining of scheduled jon line

#00 09-18 * * 1-5 sh /path/os_monitoring.sh


Problem :- "You are not allowed to use this program"
When we tried to access the cron, we got an error.

$ crontab -l
You  are not allowed to use this program (crontab)
See crontab(1) for more information

Solution :-This error is due to user don’t have access to crontab.

There are two files on server, to allow & deny access to crontab, Files are named as

/etc/cron.allow & /etc/cron.deny

check that if user is present in cron.deny file. if it is present remove user from cron.deny file & add user to cron.allow file.

If user is not present in any file then add user in /etc/cron.allow file.

# cat /etc/cron.allow
root

so, i haved added oracle user to cron.allow file, file will look as follows:

# cat /etc/cron.allow
root
myuser

Now user will be able to access the crontab.

$ crontab -l
no crontab for oracle

NOTE – If the files“/etc/cron.allow” and “/etc/cron.deny” files are not present on server / system, we can create both files manually.

# touch /etc/cron.allow /etc/cron.deny

No comments:

Post a Comment

Remove CTRL-M characters from a file in UNIX

 We can remove ^M by below methods 1st Method - Using sed command Type below command:    sed -e "s/^M//" filename > newfilena...