Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Topic: How to Automate Tasks with cron Jobs in Linux (on the basis of time,date,month,year, etc)
- Q. What is CRON JOB?
- A. The cron command-line utility is a job scheduler on Unix-like operating systems. Users who set up and maintain software environments use cron to schedule jobs, also known as cron jobs, to run periodically at fixed times, dates, or intervals.
- # Cron Jobs are stored in a location
- ls /etc/cron #Press tab two times to list all the files
- # Restart cron jobs
- sudo systemctl status cron.service
- # Cron job syntax
- crontab -e: edits crontab entries to add, delete, or edit cron jobs.
- crontab -l: list all the cron jobs for the current user.
- crontab -u username -l: list another user's crons.
- crontab -u username -e: edit another user's crons
- # List crons
- $ cron job example
- # What do **** mean
- * * * * * represents minute(s) hour(s) day(s) month(s) weekday(s), respectively.
- * * * * * sh /path/to/script/script.sh
- | | | | | |
- | | | | | Command or Script to Execute
- | | | | |
- | | | | |
- | | | | |
- | | | | Day of the Week(0-6)
- | | | |
- | | | Month of the Year(1-12)
- | | |
- | | Day of the Month(1-31)
- | |
- | Hour(0-23)
- |
- Min(0-59)
- # To list or check the running task use the command.
- $ crontab -l
- # To establish the task
- $ crontab -e #choose nano and hit enter
- * * * * * (5 start denoted by abcde for explaining)
- # a b c d e command
- # a=minutes (00,..60)
- # b=hour (01,02..12)
- # c=date (01,02..10,11..30,31)
- # d=month (jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec)
- # e=Day (sun,mon,tue,wed,thu,fri,sat)
- # schadule the task
- 11 02 * * * touch /user/Desktop/file.txt
- # It will create a file every day at 2 a.m. for 11 minutes. If you fill rest of the three dots you can set a specific date month and day as well.
- # To stop the task you can use the command
- $ crontab -r #close all schedules without any notification
- $ crontab -i -r #close schedules with notifications
- # Create user-specific task
- $ crontab -u username -e #now again fill the box as shown below
- 11 12 * * * mkdir cool #it will create the folder every day for 11 minutes and 12 pm
- #repeat task after every 5 hours
- 0 */5 * * * touch /usr/bin/script
- #Repeat the task after every 30 minutes
- */30 * * * * command
- #Repeat multiple tasks after 30 minutes
- */30 * * * * command; command
- Using special strings to save time on writing cron jobs
- 1) @hourly timestamp corresponds to 0 * * * *
- It will execute a task in the first minute of every hour.
- @hourly /path/to/script
- 2) @daily timestamp is equivalent to 0 0 * * *
- It executes a task in the first minute of every day (midnight). It comes in handy when executing daily jobs.
- @daily /path/to/script
- 3) @weekly timestamp is the equivalent to 0 0 1 * mon
- It executes a cron job in the first minute of every week, whereby a week starts on Monday.
- @weekly /path/to/script
- 3) @monthly is similar to the entry 0 0 1 * *
- It carries out a task in the first minute of the first day of the month.
- @monthly /path/to/script
- 4) @yearly corresponds to 0 0 1 1 *
- It executes a task in the first minute of every year and is useful in sending New Year greetings ๐
- @monthly /path/to/script
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement