How to run a cron job: Tutorial

Use the command crontab -e to edit the crontab. This uses the editor specified in the environment variable$EDITOR, and if the variable is empty, this defaults to vi(1) or vim(1)
The format of a crontab is 6 columns, space (or tab) separated. The first 5 columns detail the time when to run a command, which is specified in the last column. The last "column" makes up all the remaining characters, inclusive of spaces. Here is an example:
5 * * * * /home/aspera/my_script.sh
What the above shows, per column:
  1. Minute to run on, a value of 0-59: 5 means on the 5th minute of the hour
  2. The Hour, a value of 0-23: * means every hour
  3. Day of the Month, 1-31: * means every day of the month
  4. The Month, 1-12: * means every month
  5. Day of the Week, 0-7 (0 and 7 are both Sunday): * means every day
  6. The Command: In this case, a command at/home/aspera/my_script.sh
With older crontabs, like the one on legacy Solaris, this is mostly all that can be done. One variation is to use comma separated lists. Modern crontabs can use string names for days, and wildcards with increments. So, to create an entry that runsmy_script.sh every 5 minutes would be as follows on Linux:
*/5 * * * * /home/aspera/my_script.sh
The */5 notation in the first column means "every 5 minutes". To make this every 10 minutes, */10 is used. To do the same 5 minute interval on a Solaris system, try the following:
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /home/aspera/my_script.sh

Using Cron

To use cron for tasks meant to run only for your user profile, add entries to your own user's crontab file. Start the crontab editor from a terminal window:
crontab -e
Edit the crontab using the format described in the next sections. Save your changes. (Exiting without saving will leave your crontab unchanged.)
Note that a great source of information about the format can be found at:
man 5 crontab
Commands that normally run with administrative privileges (i.e. they are generally run using sudo) should be added to the root user's crontab (instead of the user's crontab):
 sudo crontab -e

Crontab Sections

Each of the sections is separated by a space, with the final section having one or more spaces in it. No spaces are allowed within Sections 1-5, only between them. Sections 1-5 are used to indicate when and how often you want the task to be executed. This is how a cron job is laid out:
minute (0-59), hour (0-23, 0 = midnight), day (1-31), month (1-12), weekday (0-6, 0 = Sunday), command
01 04 1 1 1 /usr/bin/somedirectory/somecommand
The above example will run /usr/bin/somedirectory/somecommand at 4:01am on January 1st plus every Monday in January. An asterisk (*) can be used so that every instance (every hour, every weekday, every month, etc.) of a time period is used. Code:
01 04 * * * /usr/bin/somedirectory/somecommand
The above example will run /usr/bin/somedirectory/somecommand at 4:01am on every day of every month.
Comma-separated values can be used to run more than one instance of a particular command within a time period. Dash-separated values can be used to run a command continuously. Code:
01,31 04,05 1-15 1,6 * /usr/bin/somedirectory/somecommand
The above example will run /usr/bin/somedirectory/somecommand at 01 and 31 past the hours of 4:00am and 5:00am on the 1st through the 15th of every January and June.
The "/usr/bin/somedirectory/somecommand" text in the above examples indicates the task which will be run at the specified times. It is recommended that you use the full path to the desired commands as shown in the above examples. Enter which some command in the terminal to find the full path to some command. The crontab will begin running as soon as it is properly edited and saved.
You may want to run a script some number of times per time unit. For example if you want to run it every 10 minutes use the following crontab entry (runs on minutes divisible by 10: 0, 10, 20, 30, etc.)
*/10 * * * * /usr/bin/somedirectory/somecommand
which is also equivalent to the more cumbersome
0,10,20,30,40,50 * * * * /usr/bin/somedirectory/somecommand

Crontab Options

  • The -l option causes the current crontab to be displayed on standard output.
  • The -r option causes the current crontab to be removed.
  • The -e option is used to edit the current crontab using the editor specified by the EDITOR environment variable.
After you exit from the editor, the modified crontab will be checked for accuracy and, if there are no errors, installed automatically. The file is stored in /var/spool/cron/crontabs but should only be edited via the crontab command.

Enable User Level Cron

If the /etc/cron.allow file exists, then users must be listed in it in order to be allowed to run the crontab command. If the /etc/cron.allow file does not exist but the /etc/cron.deny file does, then users must not be listed in the /etc/cron.deny file in order to run crontab.
In the case where neither file exists, the default on current Ubuntu (and Debian, but not some other Linux and UNIX systems) is to allow all users to run jobs with crontab.
No cron.allow or cron.deny files exist in a standard Ubuntu install, so all users should have cron available by default, until one of those files is created. If a blank cron.deny file has been created, that will change to the standard behavior users of other operating systems might expect: cron only available to root or users in cron.allow.
Note, userids on your system which do not appear in /etc/shadow will NOT have operational crontabs, if you desire to enter a user in /etc/passwd, but NOT /etc/shadow that user's crontab will never run. Place an entry in /etc/shadow for the user with a * for the password crypt,ie:

joeuser:*:15169::::::

Sample Script:

echo "------------------------------Server Monitoring Started-------------------------" >> test.txt
now=$(date +"%T")
echo "Current time : $now" >> test.txt

echo "ps -fe |grep httpd | wc -l" >> test.txt
ps -fe |grep httpd | wc -l >> test.txt

Comments

Popular posts from this blog

JMeter – Working with JSON

How to create an XML Log file in Java using JAXB (Customized)

Hyperlocal Startups Creating a Better Living