HOWTO SET-UP A CRONTAB FILE

source: https://corenominal.org/2016/05/12/howto-setup-a-crontab-file/


Foreword: a few days ago, I received an email from Fred with the message:

You used to have an awesome page on crontab. Where’d it go?

The short answer, it got lost when I moved my site to WordPress. Anyhow, prompted by Fred, I thought it might be nice to rescue the content (the page was a good few years old, but still relevant), so I’ve tracked it down and republished it below. I hope someone finds it useful.


In Linux, Cron is a daemon/service that executes shell commands periodically on a given schedule. Cron is driven by a crontab, a configuration file that holds details of what commands are to be run along with a timetable of when to run them.

CREATING A CRONTAB FILE

You can create a crontab file by entering the following terminal command:

crontab -e

Entering the above command will open a terminal editor with a new blank crontab file, or it will open an existing crontab if you already have one. You can now enter the commands to be executed, see syntax below, before saving the file and exiting the editor. As long as your entries were entered correctly your commands should now be executed at the times/dates you specified. You can see a list of active crontab entries by entering the following terminal command:

crontab -l

CRONTAB SYNTAX

A crontab file has six fields for specifying minute, hour, day of month, month, day of week and the command to be run at that interval. See below:

*     *     *     *     *  command to be executed
-     -     -     -     -
|     |     |     |     |
|     |     |     |     +----- day of week (0 - 6) (Sunday=0)
|     |     |     +------- month (1 - 12)
|     |     +--------- day of month (1 - 31)
|     +----------- hour (0 - 23)
+------------- min (0 - 59)

CRONTAB EXAMPLES

Writing a crontab file can be a somewhat confusing for first time users, therefore I have listed below some crontab examples:

* * * * *  #Runs every minute
30 * * * *  #Runs at 30 minutes past the hour
45 6 * * *  #Runs at 6:45 am every day
45 18 * * *  #Runs at 6:45 pm every day
00 1 * * 0  #Runs at 1:00 am every Sunday
00 1 * * 7  #Runs at 1:00 am every Sunday
00 1 * * Sun  #Runs at 1:00 am every Sunday
30 8 1 * *  #Runs at 8:30 am on the first day of every month
00 0-23/2 02 07 *  #Runs every other hour on the 2nd of July

As well as the above there are also special strings that can be used:

@reboot  #Runs at boot
@yearly  #Runs once a year [0 0 1 1 *]
@annually  #Runs once a year [0 0 1 1 *]
@monthly  #Runs once a month [0 0 1 * *]
@weekly  #Runs once a week [0 0 * * 0]
@daily  #Runs once a day [0 0 * * *]
@midnight  #Runs once a day [0 0 * * *]
@hourly  #Runs once an hour [0 * * * *]

MULTIPLE COMMANDS

A double-ampersand && can be used to run multiple commands consecutively. The following example would run command_01 and then command_02 once a day:

@daily command_01 && command_02

DISABLING EMAIL NOTIFICATIONS

By default a cron job will send an email to the user account executing the cronjob. If this is not needed put the following command at the end of the cron job line:

>/dev/null 2>&1

Or, you can disable all email notifications by adding the following to the top of the crontab file:

MAILTO=""

SPECIFYING A CRONTAB FILE TO USE

As mentioned at the top of this post, you can create a new crontab file with the “crontab -e” command. However, you may already have a crontab file, if you do you can set it to be used with the following command:

crontab -u  

Therefore the following command…

crontab -u tux ~/crontab

…would set Tux’s crontab file to that of the file named “crontab” residing in Tux’s home directory.

REMOVING A CRONTAB FILE

To remove your crontab file simply enter the following terminal command:

crontab -r

FURTHER INFORMATION

Refer to the man page for further information about crontab. Enter the terminal command:

man crontab

EXTERNAL LINKS

Some external links for your browsing pleasure:

I think that pretty much covers the subject of cron jobs and crontab. Please feel free to comment if I have missed anything or made any obvious mistakes.

Tags:cronlinuxsystem admin

5 COMMENTS

  1. Leslie  says:

    Thank you – for those of us novices at this – I also found this link to be helpful (http://php-opensource-help.blogspot.com/2010/03/how-to-set-up-cron-job-file-using-putty.html). Basically told me how to use the text editor when editing / creating a cronjob – sp[ecifically commands like ‘o’ which puts the user into editing mode, and ZZ which saves the file. Very helpful stuff.

  2. corenominal  says:

    I’m glad you found it useful, and thank you for the link.

  3. Fred  says:

    Thanks for reposting this. Simplicity is king!

  4. rolandjb  says:

    I create my cronjob like this
    30 7 * * * /xxx/xxx/xxx.sh stop
    45 7 * * * /xxx/xxx/xxx.sh start

    The cronjob is successull to stop but unable to start. What happened. Please explain the error cronjob above for linux.

  5. corenominal  says:

    @rolandjb it’s impossible to tell what happened given the information you provided. That said, I’d probably start by looking at the “start” section of your script and adding some basic logging. Also, do you receive any output via email when the job errors?

LEAVE A COMMENT

Your email address will not be published. Required fields are marked *


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值