Tuesday, November 22, 2011

Crontab in Linux

Cron is a UNIX, Solaris utility that allows tasks to be automatically run in the background at regular intervals by the cron daemon. These tasks are often termed as cron jobs in unix , solaris.
Crontab (CRON TABle) is a file which contains the schedule of cron entries to be run and at specified times.

1. Crontab Restrictions

You can execute crontab if your name appears in the file /usr/lib/cron/cron.allow. If that file does not exist, you can use
crontab if your name does not appear in the file /usr/lib/cron/cron.deny.
If only cron.deny exists and is empty, all users can use crontab. If neither file exists, only the root user can use crontab. The allow/deny files consist of one user name per line.

2. Crontab Commands

export EDITOR=vi ;to specify a editor to open crontab file.

crontab -e     Edit your crontab file, or create one if it doesn't already exist.
crontab -l      Display your crontab file.
crontab -r      Remove your crontab file.
crontab -v      Display the last time you edited your crontab file. (This option is only available on a few systems.)
 
3. Crontab file

Crontab syntax :-
A crontab file has five fields for specifying day , date and time  followed by the command to be run at that interval.
*     *   *   *    *  command to be executed
-     -    -    -    -
|     |     |     |     |
|     |     |     |     +----- day of week (0 - 6) (Sunday=0)
|     |     |     +------- month (1 - 12)
|     |     +--------- day of month (1 - 31)
|     +----------- hour (0 - 23)
+------------- min (0 - 59)
 
* in the value field above means all legal values as in braces for that column.
The value column can have a * or a list of elements separated by commas. An element is either a number in the ranges shown above or two numbers in the range separated by a hyphen (meaning an inclusive range).

Note: The specification of days can be made in two fields: month day and weekday. If both are specified in an entry, they are cumulative meaning both of the entries will get executed .

4. Crontab Example

A line in crontab file like below  removes the tmp files from /home/someuser/tmp each day at 6:30 PM.
30     18     *     *     *         rm /home/someuser/tmp/*

Changing the parameter values as below will cause this command to run at different time schedule below :
min
hour
day/month
month
day/week
 Execution time
30
0
1
1,6,12
*
-- 00:30 Hrs  on 1st of Jan, June & Dec.
:
0
20
*
10
1-5
--8.00 PM every weekday (Mon-Fri) only in Oct.
:
0
0
1,10,15
*
*
-- midnight on 1st ,10th & 15th of month
:
5,10
0
10
*
1
-- At 12.05,12.10 every Monday & on 10th of every month
:
Note : If you inadvertently enter the crontab command with no argument(s), do not attempt to get out with Control-d. This removes all entries in your crontab file. Instead,

Linux keyboard shortcuts

< Virtual terminals >
Ctrl + Alt + F1
Switch to the first virtual terminal. In Linux, you can have several virtual terminals at the same time. The default is 6.
Ctrl + Alt + Fn
Switch to the nth virtual terminal. Because the number of virtual terminals is 6 by default, n = 1...6.
tty
Typing the
tty command tells you what virtual terminal you're currently working in.
Ctrl + Alt + F7
Switch to the GUI. If you have X Window System running, it runs in the seventh virtual terminal by default. If X isn't running, this terminal is empty.

< X Window System >
Ctrl + Alt + +
Switch to the next resolution in the X Window System. This works if you've configured more than one resolution for your X server. Note that you must use the + in your numpad.
Ctrl + Alt + -
Switch to the previous X resolution. Use the - in your numpad.
MiddleMouseButton
Paste the highlighted text. You can highlight the text with your left mouse button (or with some other highlighting method, depending on the application you're using), and then press the middle mouse button to paste. This is the traditional way of copying and pasting in the X Window System, but it may not work in some X applications.
If you have a two-button mouse, pressing both of the buttons at the same time has the same effect as pressing the middle one. If it doesn't, you must enable 3-mouse-button emulation.
This works also in text terminals if you enable the gpm service.
Ctrl + Alt + Backspace
Kill the X server. Use this if X crashes and you can't exit it normally. If you've configured your X Window System to start automatically at bootup, this restarts the server and throws you back to the graphical login screen.


< Command line - input >
Home or Ctrl + a
Move the cursor to the beginning of the current line.
End or Ctrl + e
Move the cursor to the end of the current line.
Alt + b
Move the cursor to the beginning of the current or previous word. Note that while this works in virtual terminals, it may not work in all graphical terminal emulators, because many graphical applications already use this as a menu shortcut by default.
Alt + f
Move the cursor to the end of the next word. Again, like with all shortcuts that use Alt as the modifier, this may not work in all graphical terminal emulators.
Tab
Autocomplete commands and file names. Type the first letter(s) of a command, directory or file name, press
Tab and the rest is completed automatically! If there are more commands starting with the same letters, the shell completes as much as it can and beeps. If you then press Tab again, it shows you all the alternatives.
This shortcut is really helpful and saves a lot of typing! It even works at the lilo prompt and in some X applications.
Ctrl + u
Erase the current line.
Ctrl + k
Delete the line from the position of the cursor to the end of the line.
Ctrl + w
Delete the word before the cursor.

< Command line - output >
Shift + PageUp
Scroll terminal output up.
Shift + PageDown
Scroll terminal output down.
clear
The
clear command clears all previously executed commands and their output from the current terminal.
Ctrl + l
Does exactly the same as typing the
clear command.
reset
If you mess up your terminal, use the
reset command. For example, if you try to cat a binary file, the terminal starts showing weird characters. Note that you may not be able to see the command when you're typing it.

< Command line - history >
history
When you type the
history command, you'll see a list of the commands you executed previously.
ArrowUp or Ctrl + p
Scroll up in the history and edit the previously executed commands. To execute them, press
Enter like you normally do.
ArrowDown or Ctrl + n
Scroll down in the history and edit the next commands.
Ctrl + r
Find the last command that contained the letters you're typing. For example, if you want to find out the last action you did to a file called "
file42.txt", you'll press Ctrl + r and start typing the file name. Or, if you want to find out the last parameters you gave to the "cp" command, you'll press Ctrl + r and type in "cp".

< Command line - misc >
Ctrl + c
Kill the current process.
Ctrl + z
Send the current process to background. This is useful if you have a program running, and you need the terminal for awhile but don't want to exit the program completely. Then just send it to background with
Ctrl+z, do whatever you want, and type the command fg to get the process back.
Ctrl + d
Log out from the current terminal. If you use this in a terminal emulator under X, this usually shuts down the terminal emulator after logging you out.
Ctrl + Alt + Del
Reboot the system. You can change this behavior by editing
/etc/inittab if you want the system to shut down instead of rebooting.

Manipulating directories in Linux

< Creating directories >

Creating a new, empty directory is very easy. You use the mkdir command:
$ mkdir dir1
That's it. It's really that easy!

< Removing directories >

There are two commands you can use for removing directories. If the directory is empty, you can use rmdir:
$ rmdir dir1
You can use rmdir only if the directory is empty. If you want to remove a directory with all its contents, you can use rm with the -r option. The -r option tells rm to remove a directory recursively:
$ rm -r dir1
It goes without saying that you can cause a lot of trouble with rm -r if you're not careful! In some cases it might be a good thing to use the -i option when deleting a directory with its contents so that you'd be prompted before each file in the directory gets deleted:
$ rm -ir dir1

< Copying and moving directories >

For copying and moving directories you can use the cp and mv commands just like you use them with files. If you've already tried to copy a directory with cp, you've probably noticed that cp just complains at you. Probably it says something like cp: omitting directory yadda yadda. You see, the cp command wants you to use the -r option if you want to copy a directory with its contents. The -r means "copy recursively":
$ cp -r dir1 dir2
The above creates a directory named dir2 whose contents will be identical to dir1. However, if dir2 already exists, nothing will be overwritten: the directory dir1 will be copied into the dir2 directory under the name dir2/dir1.
When renaming directories, you use the mv command exactly the same way as with files:
$ mv dir1 dir2
When dealing with directories, mv works a bit like cp does. If dir2 doesn't exist, the above will rename dir1 to dir2, but if dir2 exists, the directory dir1 will be moved into the dir2 directory under the name dir2/dir1.



Moving around in the file system
Command
Action
pwd
"Print working directory" - show what dir you're in.
ls
List the contents of a dir.
ls -l
List the contents of a dir and show additional info of the files.
ls -a
List all files, including hidden files.
cd
Change directory.
cd ..
Go to the parent directory.
Examining files
Command
Action
file
Determine the type of a file.
cat
Concatenate a file.
less
View text files and paginate them if needed.
Manipulating files and directories
Command
Action
cp
Copy a file.
cp -i
Copy a file and ask before overwriting.
cp -r
Copy a directory with its contents.
mv
Move or rename a file.
mv -i
Move or rename a file and ask before overwriting.
rm
Remove a file.
rm -r
Remove a directory with its contents.
rm -i
Ask before removing a file. Good to use with the -r option.
mkdir
Make a directory.
rmdir
Remove an empty directory.