Tag Archives: Linux - Page 7

A quick intro to crontab

Every now and then I need to create jobs that run on intervalls or at special times. In *nix there is a handy command called crontab that takes care of this. However the crontab command is not always self-explanatory so I made this quick intro to the command so I don’t have to keep it in my head

Command line options:

crontab -l   # Show my current cron jobs
crontab -e   # Edit my cron jobs. Opens your default editor (e.g. VIM)
crontab -r   # Removes all cron jobs

Crontab syntax (user):

#Example
#min hour day month weekday command
  5   *    *    *      *    echo Hi #Runs 5 min past every hour
 */5  *    *    *      *    echo Hi #Runs in 5 min intervall
  *  8,20  *    *      *    echo Hi #Runs at 08:00 and 20:00  
  *  12   */2   *      *    echo Hi #Runs every other day at 12:00
 30  23    *    *      0    echo Hi #Runs every Sunday at 23:30   

Crontab syntax (root):

#Example
#min hour day month weekday user command
  0   *    *    1      *    root   echo Hi #Runs every hour in January
 */10 *    *    *      *    niklas echo Hi #Runs in 10 min intervall  

Only differens between root crontab and a user crontab is that root needs to tell cron who will be running the job

Syntax details:
min = minutes (0-59)
hour = hours (0-23)
day = day of month (1-31)
month = month (1-12)
weekday = day of week (0-6) (0 is Sunday)
user = the user that runs the job
command = command to run. Same syntax as if you would have run it on the command line. More then one command can be run, just separate the commands with a semicolon (;)

Run a cron job in “silent mode”:
Cron likes to mail that everything has gone well after running the command. If you do not want these mails you can put a cryptic pice of code at the end of your command like this:

#min hour day month weekday command
  0   0    *    *      *    rm -rf /home/niklas/tmp/* >/dev/null 2>&1

This will delete everything in my /home/niklas/tmp directory every night at 00:00. No mail will be sent

Tested on Red Hat Enterprise Linux AS release 3 (Taroon Update 9)

Find files with the find command

Find is a very useful commando that is found on most Linux systems – at least all the ones I have worked with. I keep forgetting the syntax for it so I decided to put it here

Find a file with a specific name

find /home -name "filename"

This will search the /home directory and all sub directories for a file called ‘filename‘. If you omit the path find will search in your current directory and all sub directories.

Using the tar command to archive files and directories

This is again a command that I keep forgetting the syntax for, so I will put the most basic syntax here:

Tar command syntax:

tar <options> <output-file> <input-file>

Creating a tar file from wildcard matching:

tar -cvf file.tar *.xml

This will create a tar archive with all files ending with ‘.xml’ in it. The -c option stands for create, -v option for verbose (echo what files being used) and -f option for file (standard is stdin)

Creating a tar file from a directory:

tar -cvf dir.tar dir/

This will create a tar file of all files in directory dir.

Extracting tar files:

tar -xvf file.tar

This will extract the contents of the file.tar file to the current directory. The -x option stands for extract

Extracting a gzip tar archive:

tar -xvzf myfile.tar.gz

The -z option here tells tar to run the archive through gzip

Creating a tar file with gzip compression:

tar -czvf dir.tar.gz dir/