Setting up a cronjob

Discussion in 'Commonly Asked Questions and Their Solutions' started by renlok, Aug 13, 2016.

Thread Status:
Not open for further replies.
  1. renlok

    renlok Administrator Staff Member

    Joined:
    Oct 20, 2008
    Messages:
    2,858
    Likes Received:
    329
    What is a cronjob?
    A cronjob is a command you can set up to be run by your server at set intervals it can be very useful to preform important tasks that are resource intensive.

    What does a cronjob look like?
    It will look something like
    Code:
    * * * * * /usr/bin/somedirectory/somecommand
    it will consist of 5 time/date settings which I will go on to explain later (01 04 1 1 *) and the command you want to run (/usr/bin/somedirectory/somecommand). The commands will make sence to anyone who has experience with using the command line, but for our purpose they are quite simple (running a php file)

    How do I set up the timing?
    So the 5 time/date fields correlate to minute (0-59), hour (0-23, 0 = midnight), day (1-31), month (1-12), weekday (0-6, 0 = Sunday).
    An asterisk (*) can also be used to denote at every instance so in the above example (* * * * *) would run every minute of every day, 365 days a year.
    You can also use comma separated values to run at multiple time instances and a dash to run it though a range of instances for example
    Code:
    10,40 12-15 * * * /usr/bin/somedirectory/somecommand
    would be run twice an hour at 10 past and at 40 past the hours of midday, 1pm, 2pm and 3pm.
    And finally if you want it run every x units of time you can use */x so if you want it to run every 10 days you could use
    Code:
    * * */10 * * /usr/bin/somedirectory/somecommand
    Here's some useful examples to help
    Code:
    Run once a year - "0 0 1 1 *"
    
    Run once a month - "0 0 1 * *".
    
    Run once a week - "0 0 * * 0"
    
    Daily - "0 0 * * *"
    
    Hourly - "0 * * * *"
    
    Every 15 minutes - "*/15 * * * *"
    What command do I need to run?
    The command will look something like this
    Code:
    /location/of/php /path/to/my/cronfile.php
    This depends a lot on your server setup as you may need to know the location of your php install.
    Commonly you may be able to use php or /usr/local/bin/php you can test those or just ask your host provider they will know.

    By default cron is setup to email you the results each time it runs which can be useful when setting it up but is generally quite annoying, nobody whats the same email sent to them every minute. You can stop it from doing this by adding >/dev/null to the end of the command. This means instead of being send via email all the outputs are sent to /dev/null which acts like a black hole.
    Code:
    10,40 12-15 * * * /usr/bin/somedirectory/somecommand  >/dev/null
     
    Inspired, Karla and david62311 like this.
  2.  
Thread Status:
Not open for further replies.

Share This Page