The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to set cron job through shell ?

Discussion in 'General Discussion' started by billau, Jul 31, 2004.

  1. billau

    billau Well-Known Member

    Joined:
    Dec 24, 2003
    Messages:
    65
    Likes Received:
    0
    Trophy Points:
    6
    Location:
    Brisbane, Australia
    An entry in cron is made up of a series of fields, much like the /etc/passwd
    file is, but in the crontab they are separated by a space. There are normally
    seven fields in one entry. The fields are:

    minute hour dom month dow user cmd

    minute This controls what minute of the hour the command will run on,
    and is between '0' and '59'
    hour This controls what hour the command will run on, and is specified in
    the 24 hour clock, values must be between 0 and 23 (0 is midnight)
    dom This is the Day of Month, that you want the command run on, e.g. to
    run a command on the 19th of each month, the dom would be 19.
    month This is the month a specified command will run on, it may be specified
    numerically (0-12), or as the name of the month (e.g. May)
    dow This is the Day of Week that you want a command to be run on, it can
    also be numeric (0-7) or as the name of the day (e.g. sun).
    user This is the user who runs the command.
    cmd This is the command that you want run. This field may contain
    multiple words or spaces.

    If you don't wish to specify a value for a field, just place a * in the
    field.

    eg:

    01 * * * * root echo "This command is run at one min past every hour"
    17 8 * * * root echo "This command is run daily at 8:17 am"
    17 20 * * * root echo "This command is run daily at 8:17 pm"
    00 4 * * 0 root echo "This command is run at 4 am every Sunday"
    * 4 * * Sun root echo "So is this"
    42 4 1 * * root echo "This command is run 4:42 am every 1st of the month"
    01 * 19 07 * root echo "This command is run hourly on the 19th of July"


    cron files have been kept in /var/spool/cron/. Every user can potentially have a crontab file for running jobs on a regular basis. Those files are kept here named as the username. The system crontab file has been named root, which, of course, is also a user on the system.


    If you want to set cron job for root user please follow the given instructions:

    #cd /var/spool/cron
    #vi root

    59 11 * * * root backup.sh

    (Will run backup.sh at 11:59 every day)


    59 11 * * 1,2,3,4,5 root backup1.sh

    (Will run backup.sh at 11:59 Monday, Tuesday, Wednesday, Thursday and Friday,
    as will -> 59 11 * * 1-5 root backup.sh )


    If you want to set cron job for virtual user please follow the given instructions:


    #cd /var/spool/cron
    #vi User_Name

    5 0 * * * php $HOME/daily_job.php >> $HOME/tmp/out 2>&1

    (This says run this job once a day (5 min after midnight). The job to run is $HOME/daily_job.php Any output from this job is appended (>>) to the file $HOME/tmp/out and Standard error (2) is also sent to standard output which means it also gets appended to the file.
     
  2. chirpy

    chirpy Well-Known Member

    Joined:
    Jun 15, 2002
    Messages:
    13,475
    Likes Received:
    20
    Trophy Points:
    38
    Location:
    Go on, have a guess
    No. You should never edit a crontab file directly like that within /var/spool/cron (don't you read the warnings at the top of the file :rolleyes: ).

    If you want to edit a users crontab from the root shell, you should always use:

    crontab -e -u <USERNAME>
     
  3. PWSowner

    PWSowner Well-Known Member

    Joined:
    Nov 10, 2001
    Messages:
    2,948
    Likes Received:
    3
    Trophy Points:
    38
    Location:
    ON, Canada
    If you edit a cron file directly, it won't even take effect until you restart the cron daemon. Chirpy is correct in how to edit a cron file.

    Like I said in the longer thread, I'm all for how-to's, but only post a how-to if you know what you are posting.
     
  4. Roy@ENHOST

    Roy@ENHOST Well-Known Member

    Joined:
    Mar 5, 2002
    Messages:
    495
    Likes Received:
    0
    Trophy Points:
    16
    Location:
    Los Angeles California
    So if I edit my cron file using Notepad then uploaded it, all it takes is a /etc/rc.d/init.d/crond restart am I right?
    :)

     
  5. PWSowner

    PWSowner Well-Known Member

    Joined:
    Nov 10, 2001
    Messages:
    2,948
    Likes Received:
    3
    Trophy Points:
    38
    Location:
    ON, Canada
    Correct. It's not the proper way, but it's how I did it for a bit until I learned the right way.
     
Loading...

Share This Page