Friday, 3 March 2017

15 great examples to create a cron job on Linux



Linux Format Crontab

MIN HOUR DOM MON DOW CMD
Table: Fields and allowable ranges Crontab (Linux Crontab syntax)
FieldDescriptionPermissible value
MINminutesFrom 0 to 59
HOURClockFrom 0 to 23
DOMDay of the month1-31
MONMonth1-12
DOWDay of the week0-6
CMDCommandAny command that you want to perform

1. Planning tasks for a certain time

The basis of the use of cron - tasks at a particular time, as shown below. This script will perform a full backup (full-backup) on June 10 at 8.30.
Please note that the time field uses a 24 hour clock, so that 8 AM - 8 PM 8 - 20 hours.

30 08 10 06 * /home/developer/full-backup
  • 30 - 30th minute
  • 08 - 08 AM
  • 10 - Day 10
  • * - Each day of the week

2. Schedule tasks to perform more frequent (eg twice a day)

The following script uses the additional backup twice a day every day. This example further backups (incremental-backup) at 11:00 and 16:00 every day. Values ​​separated by commas in the field indicate that the command should be executed at every given time.
00 11,16 * * * /home/developer/bin/incremental-backup
00- 0-th minute (early hours) 11, 16- 11 and 16 hours *- every day *- every month *- every day of the week

3. Schedule of work for a certain period of time (for example, only on weekdays)

If you want to routines performed during each hour with a certain time interval, use the following.
Cron subroutine for each day during working hours
This example checks the status of each day database (including weekends) during working hours from 9 am to 6 pm.
00 09-18 * * * /home/developer/bin/check-db-status
00- 0-th minute (early hours) 09-18- 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 (hours) *- every day *- each month *- each weekday

Cron subroutine for each working day during working hours
This example checks the state of the database every day (except Saturday and Sunday) from 9 am to 6 pm.
00 09-18 * * 1-5 /home/ramesh/bin/check-db-status
00- 0-th minute (early hours) 09-18- 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 (hours) *- every day *- every month 1-5 - Monday, Tuesday, Wednesday, Thursday and Friday (every week)

4. To view the recording Crontab?

View the current user's Crontab-files.
To view your files crontab-lfrom vyshey unix account, do the following.
developer@dev-db$ crontab -l
@yearly /home/developer/annual-maintenance
*/10 * * * * /home/developer/check-disk-space

[Note: This displays crontab of the current logged in user]
Browse root crontab entries
Please login as root ( su - root) and do crontab-las shown below.
root@dev-db# crontab -l
no crontab for root
Practical recommendations crontab: check other records user crontab linux.
To view the files of other users who sign in as root and use the -u and {username} -l as shown below.
root@dev-db# crontab -u username -l
@monthly /home/username/monthly-backup
00 09-18 * * * /home/username/check-db-status

5. How to edit the crontab entry?

Edit current user cron-records.
Use to edit entries cronab -eas shown below. By default, the current recording will be edited user crontab.
developer@dev-db$ crontab -e
@yearly /home/developer/centos/bin/annual-maintenance
*/10 * * * * /home/developer/debian/bin/check-disk-space
~
"/tmp/crontab.XXXXyjWkHw" 2L, 83C

[Note: This will open the crontab file in Vim editor for editing.
Please note cron created a temporary /tmp/crontab.XX... ]
When you save the file, it will retain the crontab, and will show the following message, where it will be said that the crontab changed successfully.
~
"crontab.XXXXyjWkHw" 2L, 83C written
crontab: installing new crontab
Editing a root crontab entries
Please login as a rootuser ( su - root) and do crontab -eas shown below.
root@dev-db# crontab -e
Records Editing crontab-file of another user
To edit a crontab entry of another Linux user, sign in as root and use -u {имя пользователя} –е, as shown below.
root@dev-db# crontab -u username -e
@monthly /home/username/fedora/bin/monthly-backup
00 09-18 * * * /home/username/ubuntu/bin/check-db-status
~
~
~
"/tmp/crontab.XXXXyjWkHw" 2L, 83C

6. Implementation of Cron every minute

Ideally, you may not need to perform the destination cron every minute. But understanding this example will help you understand the other examples in this article.
bash
* * * * * CMD
*- Means the possibility of a unit - that is, every minute of every hour throughout the year. Furthermore, the use of *directly, would be more useful function in the following examples.
Note */5in the minutes field means every 5 minutes. Note 0-10/2in the minutes field means every 2 minutes in the first 10 minutes. Thus, this convention can be used in all 4 fields.

7. Running background tasks Cron every 10 minutes

Use the following if you want to check the disk space every 10 minutes.
*/10 * * * * /home/ramesh/check-disk-space
This task performs the specified command check disk every 10 minutes throughout the year. But you may need to perform validation only during business hours, or vice versa. The above examples show how to do it.
Instead of specifying the values ​​in fields 5, we can point them using a keyword, as shown below.
There are special conditions when, instead of 5, you can use the fields @and then the keyword - such as a reboot, midnight, every year, every hour.
Special keywords Cron and their meaning
KeywordEquivalent
@yearly0 0 1 1 *
@daily0 0 ***
@hourly0 ****
@rebootStarting system

8. Timetable for the first minute of each year using @yearly

If you want the cron tasks are performed in the first minutes of each year, you can use the keyword @yearly, as shown below. In the stacks, the system will carry out annual maintenance, annual maintenance program using the script at 00:00 on 1 January each year.
@yearly /home/developer/red-hat/bin/annual-maintenance

9. Running tasks each month using the keyword @monthly

It is similar to the word @yearly, shown above. But the command is executed on a monthly basis. The team will be backed up at 00:00 of the first day of each month.
@monthly /home/ramesh/suse/bin/tape-backup

10. Running tasks every day using @daily

Using a keyword @daily, will create a log clearing a log file every day at 00:00 every day.
@daily /home/developer/arch-linux/bin/cleanup-logs "day started"

11. How to perform a Linux command after each reboot using @reboot?

Using a keyword @reboot, will run the specified command after every system boot.
@reboot CMD

13. 12. How do I disable / redirect outbound mail Crontab, using the keyword MAIL?

By default, crontab sends output (output) the task to the user who has set this task. If you wish to redirect them to another user, add or update the MAIL variable in the crontab, as shown below.
developer@dev-db$ crontab -l
MAIL="developer"

@yearly /home/developer/annual-maintenance
*/10 * * * * /home/developer/check-disk-space

[Note: Crontab of the current logged in user with MAIL variable]
If you want to e-mail does not set off, ie to stop sending crontab output files, add or update the MAIL variable in the crontab, as shown below.
MAIL=""

13. How to Perform Linux Cron task every second using Crontab

You can not configure the cron execution per second. Because the minimum unit performing the cron - minute. In a typical scenario, there is no need to run cron execution per second.

14. Note the PATH variable in the Crontab

In all the examples above, we pointed out the absolute path of the Linux command or shell script to be executed.
For example, instead of specifying /home/developer/tape-backup, if you want to just point tape-backup, add the path /home/developerto the PATH variable crontab, as shown below.
developer@dev-db$ crontab -l

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/home/developer

@yearly annual-maintenance
*/10 * * * * check-disk-space

[Note: Crontab of the current logged in user with PATH variable]

15. Installation of Crontab Cron-file

Instead of directly editing the file crontab, you can add all the entries in cron-file. If the file contains these entries, you can download or install them in the cron, as shown below.
Note: cron-file.txt file in your crontab, cron which will remove the old record will be created. So please be careful when downloading cron entries from cron-file.txt:
developer@dev-db$ crontab -l
no crontab for developer
$ cat cron-file.txt
@yearly /home/developer/annual-maintenance
*/10 * * * * /home/developer/check-disk-space
developer@dev-db$ crontab cron-file.txt
developer@dev-db$ crontab -l
@yearly /home/developer/annual-maintenance
*/10 * * * * /home/developer/check-disk-space

2 comments:

  1. very nice. you seem to have covered everything

    ReplyDelete
  2. In root crontab I have among others:
    */15 * * * * sync && echo 3 > /proc/sys/vm/drop_caches

    ReplyDelete