cron job, crontab -e, -l using

Cron Job  Timing ,

Advanced Crontab

The Crontabs discussed above are user crontabs. Each of the above crontabs is associated with a user, even the system crontab which is associated with the root user. There are two other types of crontab.

Firstly, as mentioned above anacron uses the run-parts command and /etc/cron.hourly/etc/cron.weekly, and /etc/cron.monthly directories. However anacron itself is invoked from the /etc/crontab file. This file could be used for other cron commands, but probably shouldn’t be. Here’s an example line from a ficticious /etc/crontab:

This would run Rusty’s command script as user rusty from his home directory. However, it is not usual to add commands to this file. While an experienced user should know about it, it is not recommended that you add anything to /etc/crontab. Apart from anything else, this could cause problem if the /etc/crontab file is affected by updates! Rusty could lose his command.

The second type of crontab is to be found in /etc/cron.d. Within the directory are small named crontabs. The directory is often used by packages, and the small crontabs allows a user to be associated with the commands in them.

Instead of adding a line to /etc/crontab which Rusty knows is not a good idea, Rusty might well add a file to /etc/cron.d with the name rusty, containing his cron line above. This would not be affected by updates but is a well known location.

When would you use these alternate crontab locations? Well, on a single user machine or a shared machine such as a school or college server, auser crontab would be the way to go. But in a large IT department, where several people might look after a server, then /etc/cron.d is probably the best place to install crontabs – it’s a central point and saves searching for them!

You may not need to look at /etc/crontab or /etc/cron.d, let alone edit them by hand. But an experienced user should perhaps know about them and that the packages that he/she installs may use these locations for their crontabs.

string

meaning

@reboot

Run once, at startup.

@yearly

Run once a year, “0 0 1 1 *”.

@annually

(same as @yearly)

@monthly

Run once a month, “0 0 1 * *”.

@weekly

Run once a week, “0 0 * * 0”.

@daily

Run once a day, “0 0 * * *”.

@midnight

(same as @daily)

@hourly

Run once an hour, “0 * * * *”.

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

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:

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:

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 somecommand in the terminal to find the full path to somecommand. 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.)

which is also equivalent to the more cumbersome

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:

Further Considerations

Crontab commands are generally stored in the crontab file belonging to your user account (and executed with your user’s level of permissions). If you want to regularly run a command requiring administrative permissions, edit the root crontab file:

Depending on the commands being run, you may need to expand the root users PATH variable by putting the following line at the top of their crontab file:

It is sensible to test that your cron jobs work as intended. One method for doing this is to set up the job to run a couple of minutes in the future and then check the results before finalising the timing. You may also find it useful to put the commands into script files that log their success or failure, for example:

For more information, see the man pages for cron and crontab (man is detailed on the Basic Commands page). If your machine is regularly switched off, you may also be interested in at and anacron, which provide other approaches to scheduled tasks. For example, anacron offers simple system-wide directories for running commands hourly, daily, weekly, and monthly. Scripts to be executed in said times can be placed in/etc/cron.hourly//etc/cron.daily//etc/cron.weekly/, and /etc/cron.monthly/. All scripts in each directory are run as root, and a specific order to running the scripts can be specified by prefixing the scripts’ filenames with numbers (see the man page for run-parts for more details). Although the directories contain periods in their names, run-parts will not accept a file name containing a period and will fail silently when encountering them.

/etc/init.d/cron

Search in directories on ubuntu

Locating Files by Name

The most common use of find is in the search for a specific file by use of its name. The following command searches the home directory and all of its subdirectories looking for the file mysong.ogg:

 

It is important to get into the habit of quoting patterns in your search as seen above or your search results can be a little unpredictable. Such a search can be much more sophisticated though. For example if you wished to search for all of the ogg files in your home directory, some of which you think might be named ‘OGG’ rather than ‘ogg’, you would run:

 

Here the option ‘-iname’ performs a case-insensitive search while the wildcard character ‘*’ matches any character, or number of characters, or zero characters. To perform the same search on your entire drive you would run:

 

This could be a slow search depending on the number of directories, sub-directories and files on your system. This highlights an important difference in the way that find operates in that it examines the system directly each time unlike programs like locate or slocate which actually examine a regularly updated database of filnames and locations.

remove dir bash command

Remove / Delete directory called /tmp/foo

Open the terminal. Type the following command:

$ rmdir /tmp/foo

Recursive removal

Remove all files and directories (recursive removal):

$ rm -rf /path/to/directory
$ rm -rf /tmp/foo

Please note that you can also pass -p option to rmdir command. Each directory argument is treated as a pathname of which all components will be removed, if they are empty, starting with the last most component:

$ rmdir -p /tmp/x/y/z

Index of Internet Tools

Host Name to IP Address
For a given host name this utility will use DNS to look up the IP address. If an IP address is found, an attempt will be made to locate the country in which the IP address was allocated.
IP Address to Host Name
For a given IP address this utility will interrogate the machine using that IP address to find out its host name. If the given IP address, an attempt will be made to locate the country in which the IP address was allocated.
Flushing DNS tables in Windows
Windows does not always honour the time-to-live (TTL) for DNS records. This can mean that a host than has moved from on server to another is not found as Windows continues to keep the DNS information about the old IP address of the host name. To force Windows to refresh its host name to IP address tables, the Windows DNS tables need to be flushed.
What is an ?net?
Find out the difference between an Internet, Intranet and Extranet.
Uptime
See how long a LINUX computer has been running since it was last booted.
Find
Find files on a LINUX computer
Job Control
Useful commands and techniques to control LINUX jobs
Lastlog
List last login time for all users on a LINUX system
Wtmp
List login and logouts of various users on a LINUX system
Crontab and %
How to use % in a crontab entry and for it to be used as the text character % and not a new line character
Convert Base64 Encoded Data
Convert Base64 Encoded data to ASCII and vice versa. Also convert Base64 Encoded data to EBCDIC
RSS
Syndication of data from a web site
Encryption
Understanding how modern encryption and decryption works
MSIE Download text file
How to get around the problem with MSIE downloading, and not viewing, plain text files
ISO 9001
An overview of the ISO 9001 quality management systems standard
Contracts
An overview of which party’s terms and conditions applies to a contract
Calendar
Show a calendar for any year from 1 AD to 9999 AD with interesting information about Julian and Gregorian calendars
qpsmtpd
Details of some useful qpsmtpd plugins
Yahoo! Delays E-mail
Details on why Yahoo! can delay e-mails to its customers
Other E-mail delays
An explanation of why some emails take a long time to be delivered
Obfuscate E-mail Address
How to hide an e-mail address, on a web page, from spam robots that harvest e-mail addresses
Short URLs
Convert a long URL into a much shorter URL so that it does not wrap in an e-mail message
Dreamweaver Problems
Some easily solved Dreamweaver problems
Generate a QR Code
Generate a bar code that can be read by a mobile phone with a QR Code reader app