Using crontab

The crontab is a list of commands that you want to run on a regular schedule, and also the name of the command used to manage that list.

crontab stands for “cron table,” because it uses the job scheduler cron to execute tasks;cron itself is named after “chronos,” the Greek word for time.

Overview

cron is the system process which will automatically perform tasks for you according to a set schedule. The schedule is called the crontab, which is also the name of the program used to edit that schedule.

Let’s say you have a script which backs up important files, or creates a report about system statistics, for example. Let’s say the script is called/home/myname/scripts/do-every-day.sh, and you want to run it every morning at 5 A.M.

To edit the crontab, use this command:

crontab -e

This will open the crontab in a text editor (Usually this is vi or vim, but it may be something else depending on your Linux distribution).

The default crontab file looks like this:

# Edit this file to introduce tasks to be run by cron.
#·
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#·
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#·
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#·
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#·
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#·
# For more information see the manual pages of crontab(5) and cron(8)
#·
# m h  dom mon dow   command

These lines all start with a # because they are comments; they are ignored by cron, and are just there for you to read.

So, now let’s add our job to the crontab. Each job you add should take up a single line.

But how do we format our job entry line? Above, you can see that the last comment line is there to remind you how to format your entry. The format is very simple: six pieces of information, each separated by a space; the first five pieces of information tell cron whento run the job, and the last piece of information tells cron what the job is.

The information you must include is (in order of appearance):

  1. A number (or list of numbers, or range of numbers), m, representing the minute of the hour;
  2. A number (or list of numbers, or range of numbers), h, representing the hour of the day;
  3. A number (or list of numbers, or range of numbers), dom, representing the day of the month;
  4. A number (or list, or range), or name (or list of names), mon, representing the month of the year;
  5. A number (or list, or range), or name (or list of names), dow, representing the day of the week; and
  6. command, which is the command to be run, exactly as it would appear on the command line.

A “number” is an integer, for example 5. A “list of numbers” is a set of integers separated by commas, for example 15,30,45, which would represent just those three numbers. A “range of numbers” is a set of numbers separated by a hyphen, for example 10-20, which would represent all the numbers from 10 through 20, inclusive.

We want our job to run at 5 A.M., which would be minute 0, hour 5, every day of the month, every month, every day of the week. We need to add a line to the bottom of the file which looks like this:

0 5 * * * /home/myname/scripts/do-every-day.sh

In vi or vim, you can add this line by typing G to go to the end of the file, and o to add a new line and enter insert mode.

The asterisks (“*“) in our entry tell cron that for that unit of time, the job should be run “every”. You can now save the file and exit the text editor. In vi, this is done by pressingESCAPE and then typing :wq (for “write and quit”) and pressing ENTER. crontab will give you the following message:

crontab: installing new crontab

…and return you to the command line. Your script will now run automatically at 5 A.M., every day.

To view your crontab, you can use this command:

crontab -v

…or, to remove your crontab so that there no jobs are ever executed by cron, use this command:

crontab -r

For more examples of how to configure your crontab, see our Examples section below.

Syntax

crontab [-u user] file
crontab [-u user] [-l | -r | -e] [-i] [-s]

Technical Description

crontab is the program used to edit, remove or list the tables used to drive the crondaemon. Each user can have their own crontab. Although these files are located in/var/spool/, they are not intended to be edited directly, and that’s where the crontabcommand comes in.

cron jobs can be allowed or disallowed for individual users, as specified in the filescron.allow and cron.deny, located in the directory /etc. If the cron.allow file exists, a user must be listed there in order to be allowed to use a given command. If thecron.allow file does not exist but the cron.deny file does, then a user must not be listed there in order to use a given command. If neither of these files exists, only the superuserwill be allowed to use a given command. Another option is using PAM (pluggable authentication module) authentication to set up users who may or may not use crontaband system cron jobs, as configured in /etc/cron.d/.

The temporary directory for cron jobs can be set in environment variables (see below); if not, /tmp is used as the temporary directory.

Options

-u Append the name of the user whose crontab is to be tweaked. If this option is not given,crontab examines “your” crontab, i.e., the crontab of the person executing the command. Note that su can confuse crontab and that if you are running it inside of su you should always use the -u option for safety’s sake. The first form of this command is used to install a new crontabfrom some named file, or from standard input if the filename is given as ““.
-l Display the current crontab.
-r Remove the current crontab.
-e Edit the current crontab, using the editor specified in the VISUAL or EDITOR environment variables.
-i Same as -r, but gives the user a “Y/n” prompt before actually removing the crontab.
-s SELinux only: appends the current SELinux security context string as an MLS_LEVEL setting to the crontab file before editing or replacement occurs. See your SELinux documentation for details.

More About crontab Files

Blank lines and leading spaces and tabs are ignored. Lines whose first non-space character is a pound-sign (#) are interpreted as comments, and are ignored. Note that comments are not allowed on the same line as cron commands, since they will be taken to be part of the command. Similarly, comments are not allowed on the same line as environment variable settings.

An active line in a crontab will be either an environment setting or a cron command. An environment setting is of the form

name = value

where the spaces around the equal sign (=) are optional, and any subsequent non-leading spaces in value will be part of the value assigned to name. The value string may be placed in quotes (single or double, but matching) to preserve leading or trailing blanks.

Several environment variables are set up automatically by the cron daemon. SHELL is set to /bin/sh, and LOGNAME and HOME are set from the /etc/passwd line of the crontab’s owner. HOME and SHELL may be overridden by settings in the crontab;LOGNAME may not.

(Another note: the LOGNAME variable is sometimes called USER on BSD systems. On these systems, USER will be set also.)

In addition to LOGNAME, HOME, and SHELL, cron will look at MAILTO if it has any reason to send mail as a result of running commands in “this” crontab. If MAILTO is defined (and non-empty), mail is sent to the named user. If MAILTO is defined but empty (‘MAILTO=””‘), no mail will be sent. Otherwise mail is sent to the owner of the crontab. This option is useful if you decide on /bin/mail instead of /usr/lib/sendmail as your mailer when you install cron, because /bin/mail doesn’t do aliasing.

By default, cron will send mail using the ‘Content-Type:‘ header ‘text/plain‘ with the ‘charset=‘ parameter set to the charmap / codeset of the locale in which crond is started up: either the default system locale (if no LC_* environment variables are set) or the locale specified by the LC_* environment variables. You can use different character encodings for mailed cron job output by setting the CONTENT_TYPE andCONTENT_TRANSFER_ENCODING variables in crontabs.

The MLS_LEVEL environment variable provides support for multiple per-job SELinux security contexts in the same crontab. By default, cron jobs execute with the default SELinux security context of the user that created the crontab file. When using multiple security levels and roles, this may not be sufficient, because the same user may be running in a different role or at a different security level. You can set MLS_LEVEL to the SELinux security context string specifying the SELinux security context in which you want the job to run, and crond will set the execution context of the or jobs to which the setting applies to the specified context. (See the description of crontab -s in the options section.)

cron Command Format

Each cron command in the crontab file has five time and date fields, followed by a user name if it is the system crontab file, followed by a command. Commands are executed bycron when the minute, hour, and month of year fields match the current time, and at least one of the two day fields (day of month, or day of week) match the current time. Note that this means that nonexistent times, such as “missing hours” during daylight savings conversion, will never match, causing jobs scheduled during the “missing times” not to be run. Similarly, times that occur more than once during daylight savings will cause matching jobs to be run twice.

cron examines crontab entries once every minute.

The time and date fields are:

field allowed values
minute 059
hour 023
day of month 131
month 112 (or names; see example below)
day of week 07 (0 or 7 is Sunday, or use names; see below)

A field may be an asterisk (*), which always stands for “first through last”.

Ranges of numbers are allowed. Ranges are two numbers separated with a hyphen. The specified range is inclusive; for example, 8-11 for an “hours” entry specifies execution at hours 8, 9, 10 and 11.

Lists are allowed. A list is a set of numbers (or ranges) separated by commas. Examples: “1,2,5,9“, “0-4,8-12“.

Step values can be used in conjunction with ranges. For example, “0-23/2” can be used in the hours field to specify command execution every other hour. Steps are also permitted after an asterisk, so if you want to say “every two hours”, you can use “*/2“.

Names can also be used for the “month” and “day of week” fields. Use the first three letters of the particular day or month (case doesn’t matter). Ranges or lists of names are not allowed.

The “sixth” field (the rest of the line) specifies the command to be run. The entire command portion of the line, up to a newline or % character, will be executed by /bin/shor by the shell specified in the SHELL variable of the cronfile. Percent-signs (%) in the command, unless escaped with backslash (\), will be changed into newline characters, and all data after the first % will be sent to the command as standard input.

Note that the day of a command’s execution can be specified by two fields: day of month, and day of week. If both fields are restricted (in other words, they aren’t *), the command will be run when either field matches the current time. For example, “30 4 1,15 * 5” would cause a command to be run at 4:30 am on the 1st and 15th of each month, plus every Friday.

Files

/etc/cron.allow
/etc/cron.deny

Example crontab Command

crontab -e

Edit your crontab.

crontab -l

Display (“list”) the contents of your crontab.

crontab -r

Remove your crontab, effectively un-scheduling all crontab jobs.

sudo crontab -u charles -e

Edit the crontab of the user named charles. The -u option requires administrator privileges, so the command is executed using sudo.

sudo crontab -u jeff -l

View the crontab of user jeff.

sudo crontab -u sandy -r

Remove the crontab of user sandy.

Examples Of crontab Entries

15 6 2 1 * /home/melissa/backup.sh

Run the shell script /home/melissa/backup.sh on January 2 at 6:15 A.M.

15 06 02 Jan * /home/melissa/backup.sh

Same as the above entry. Zeroes can be added at the beginning of a number for legibility, without changing their value.

0 9-18 * * * /home/carl/hourly-archive.sh

Run /home/carl/hourly-archive.sh every hour, on the hour, from 9 A.M. through 6 P.M., every day.

0 9,18 * * Mon /home/wendy/script.sh

Run /home/wendy/script.sh every Monday, at 9 A.M. and 6 P.M.

30 22 * * Mon,Tue,Wed,Thu,Fri /usr/local/bin/backup

Run /usr/local/bin/backup at 10:30 P.M., every weekday.

Using FTP and SFTP

FTP is File Transfer Protocol. SFTP is secure FTP. In this article let us review how to connect and login to a remote ftp server for downloading and uploading files using ftp or sftp command. Most of the ftp commands are applicable to sftp. So, wherever ftp is mentioned, you can use sftp also.

1. Connect to a FTP site

Connect to a particular FTP server using ftp command as shown below.
Syntax:

$ ftp IP/hostname

or 

$ ftp
ftp> open IP/hostname

You can directly open connection with a remote host using it’s IP or host name from the command line. You can also go to ftp prompt and use open command to connect with remote host.

It will ask you for the user name and password to login. On some public domain FTP server, you can use “anonymous” username with any email address as the password to connect.

2. Download a file using ftp

Use the get command to download file from a remote ftp server as shown below.

ftp> get FILENAME

You have to be in the right mode to download files. i.e binary or ascii mode. Use ascii mode for transferring text files, and binary mode for all other type of files.

Download the file and save it with another name. In the following example, index.html file will be downloaded and saved as my.html on the local server.

ftp> get index.html my.html
Fetching /home/groups/index.html to my.html
/home/groups/index.html                          100% 2886     1.4KB/s   00:02    

3. Changing FTP Mode to binary or ascii

Go to ftp Ascii mode

ftp> ascii
200 Type set to A.

Go to ftp Binary mode

ftp> binary
200 Type set to I.

4. Uploading a file to FTP server

Use put command to upload a file to a remote ftp server as shown below.

ftp> put filename

5. Changing the remote and local directory

Apart from downloading or uploading a file, you may want to change either the remote or local directory, which you can do using cd and lcd respectively.

Change the remote server current directory using cd command

ftp> pwd
257 "/myftpserver" is current directory.
ftp> cd dir1
250 CWD command successful. "/myftpserver/dir1" is current directory.
ftp> pwd
257 "/myftpserver/dir1" is current directory.

Change the local machine current directory using lcd command

ftp> !
$ pwd
/home/sathiya/FTP
$ exit
exit
ftp> lcd /tmp
Local directory now /tmp
ftp> !
$ pwd
/tmp

Note:

  • executing ! takes you to the shell.
  • prompt starts with ftp> is ftp prompt.
  • prompt starts with $ is shell command line.

6. Listing the contents of remote directory from FTP

You can view the content of a remote directory using the ls / dir command.

ftp> ls

7. FTP Help

Type help or ? to view list of all available ftp commands.

For a detailed help on a particular ftp command use:

ftp> help COMMAND

8. Downloading multiple files with mget command

mget is for fetching multiple files from ftp server. You can use globs to download multiple files. For example, *.html will download all html files. The glob expansion are done on the remote server. So, it depends on the operating system of the remote server.

ftp> mget *.html
Fetching /ftptest/features.html to features.html
/ftptest/features.html                       100% 2256     2.2KB/s   00:01    
Fetching /ftptest/index.html to index.html
/ftptest/index.html                          100% 2886     2.8KB/s   00:01    
Fetching /ftptest/othertools.html to othertools.html
/ftptest/othertools.html                     100% 2282     2.2KB/s   00:01    
Fetching /ftptest/samplereport.html to samplereport.html
/ftptest/samplereport.html                   100%   15KB   7.3KB/s   00:02    
Fetching /ftptest/usage.html to usage.html
/ftptest/usage.html                          100% 2340     2.3KB/s   00:01    

To view the file names before downloading, you can also use mls command as shown below.

ftp> mls *.html -
/ftptest/features.html         
/ftptest/index.html            
/ftptest/othertools.html       
/ftptest/samplereport.html     
/ftptest/usage.html            

9. Uploading multiple files with mput command

Use mput to upload multiple files together. This works similar to the mget command. The following example uploads all the *.html file from local server to remote server.

ftp> mput *.html

10. Close a FTP connection

Without exiting the ftp prompt you may want to open a connection to another server. In that case, execute close command.

ftp> open ftp.your_server.com
Already connected to NNN.com, use close first.
ftp> close
221 Goodbye.
ftp> open ftp.your_server.com

Australian Web Hosting at its finest

You’ve successfully registered a new AUSWEB web hosting account.

logo

Founded in 2002, AUSWEB connects thousands of Australian businesses to the internet, with our services ranging from shared web hosting to virtual private servers, dedicated servers and Enterprise Cloud Solutions.
With all our servers and network based in a Sydney TIER3 Data Center (Equinix/Alexandria), AUSWEB provides a reliable local alternative to your online business needs.

Whether you are just getting started with your first website or are an IT Professional, you’ll appreciate the speed and features we offer with our range of plans with the ability to manage all aspects of your web hosting from the popular and user friendly cPanel Hosting Control Panel.

Our web hosting solutions are targeted to the Australian online market and our Australian servers provide the fastest connection speeds possible, whilst our 99.9% uptime guarantee provides peace of mind.

Why host in Australia?

australia-amag-tags-225x300With an increase of developers and small business looking at offshore alternatives to house their websites, we felt compelled to be frank about the reasons why you should choose an Australian host.

The 4 main pointers are:

  1. Latency
  2. SEO
  3. Support
  4. Economy

1. Latency

What is latency?

As defined by the Freedictionary.com:

latency – the time that elapses between a stimulus and the response to it

The speed of downloading or uploading data is dictated by a sequence of handovers of data between various networks starting at your device.

Your device, via your ISP connects to the server, demands the data from the remote server and receives the data, which is then relayed back to you by a route from the server’s ISP back to you via your ISP in the same manner as the initial connection.

In other words, it is one big daisy chain, which wraps around the Earth. That photo you upload to a social network does trips around the world faster than Superman, before you can ask “What exactly did I do last weekend?”.

Often you may wonder why downloading information from your local ISP is so fast (often reaching the ISP’s advertised speed…it is possible!) whilst downloads of half the size from an overseas FTP site might sluggishly drag along for hours? This is because the physical connection to your local server is shorter – just like the way it is with your phone landline!

If your small and static website is hosted overseas, latency is minimal as the volume of information relayed to and from you and the server is quite small, even though noticeable.

However, imagine hosting a large-scale CRM solution, or perhaps an elaborate Java application, which uses high quantities of bandwidth to run. The amount of data sent to and from the server increases, creating a time lag/delay which may often be problematic enough to cause packet loss or timeouts for users accessing it.

In a mission critical scenario, this is something which no business can afford to experience.

2. SEO

Your address is in Australia, your phone number is in Australia. You advertise in Australia to Australian customers. You’ve launched your shiny new site and put on your cork hat…but wait! Google knows your site is in America and naively assumes that yankees are going to want to purchase your novelty BBQ aprons with the boxing kangaroo.

Having your site hosted either in Australian or overseas is going to be detrimental to your carefully planned and fine-tuned SEO.

3. Support

We all know that calling technical support is everybody’s least favourite thing to do during the day!

Should anything go wrong, you would want to get help from somebody who does business when you do business and speaks the way in which you do. No more staying up until 4am to get a remote reboot, or password reset. Voila.

Timezones and proximity to the data center are crucial when things go pearshaped and the last thing one needs is extended periods of downtime because the datacenter is remote to the callcenter. Or yet worse, when the datacenter staff are in another timezone and sound asleep as you are ringing tech support to report peak-hour downtime in Australia!

Ausweb offers support with the confidence of a 24/7, 365 days a year monitoring of servers located in a Sydney city Datacenter, managed and supported from a Sydney city office, entirely by Sydney city staff.

4. Economy

Encouragement of healthy competition is crucial in our home soil and for competition to thrive, Australian web hosts must thrive too. We support you as an Australian business – we are one ourselves!

That and we eat our own brand of “dogfood”. The speed at which you have been able to access this very page is a testimony to the speed our clients appreciate daily as after all, this is just another website powered by Ausweb’s server cluster.

Get started today with some helpful links below: