Thursday, January 26, 2012

Cron Vs Anacron: How to Setup Anacron on Linux (With an Example)

Anacron is the cron for desktops and laptops.
Anacron does not expect the system to be running 24 x 7 like a server.
When you want a background job to be executed automatically on a machine that is not running 24 x 7, you should use anacron.
For example, if you have a backup script scheduled everyday at 11 PM as a regular cron job, and if your laptop is not up at 11 PM, your backup job will not be executed.
However, if you have the same job scheduled in anacron, you can be sure that it will be executed once the laptop come back up.

Anacrontab Format

Just like how cron has /etc/crontab, anacron has /etc/anacrontab.
/etc/anacrontab file has the anacron jobs mentioned in the following format.
period   delay   job-identifier   command
Field 1 is Recurrence period: This is a numeric value that specifies the number of days.
  • 1 – daily
  • 7 – weekly
  • 30 – monthly
  • N – This can be any numeric value. N indicates number of days
Note: You can also use ‘@monthly’ for a job that needs to be executed monthly.
Field 2 is Delay: This indicates the delay in minutes. i.e X number of minutes anacron should wait before executing the job after the the machine starts.
Field 3 is Job identifier: It is the name for the job’s timestamp file. It should be unique for each job. This will be available as a file under the /var/spool/anacron directory. This file will contain a single line that indicates the last time when this job was executed.
# ls -1 /var/spool/anacron/

# cat /var/spool/anacron/test.daily
Field 4 is command: Command or shell script that needs to be executed.
Just like shell scripts, comments inside anacrontab file starts with #
Note: For /etc/crontab file format, refer to our Linux Crontab: 15 Awesome Cron Job Examplesarticle.

Anacron Example

The following example executes the /home/sathiya/ script once in every 7 days.
On the day when the job is supposed to executed, if the system is down for some reason, anacron will execute the script 15 minutes after the system comes back up (without having to wait for another 7 days).
# cat /etc/anacrontab
7       15      test.daily      /bin/sh /home/sathiya/


The above example indicates that the script should be executed every day, with a delay of 15 mins. i.e When the laptop was started, executed it only after 15 minutes.
What happens when the laptop or desktop was not shutdown? When does the job gets executed? This is specified by the START_HOURS_RANGE environment variable in the /etc/anacrontab file.
By default this is set to 3-22 in the file. This indicates the time range from 3 a.m to 10 p.m.
# grep START /etc/anacrontab
On top of the user defined delay specified in the 2nd field of the /etc/anacrontab file, anacron also randomly adds x number of minutes. The x is defined by the RANDOM_DELAY variable in the /etc/anacrontab file.
By default this is set to 45 in the file. This means that anacron will add x minutes (randomly picked from 0 and 45), and add this to the user defined delay.
# grep RANDOM /etc/anacrontab

Cron Vs Anacron

Cron and anacron has its own advantages and disadvantages. Depending on your requirement, use one of them.
Minimum granularity is minute (i.e Jobs can be scheduled to be executed every minute)Minimum granularity is only in days
Cron job can be scheduled by any normal user ( if not restricted by super user )Anacron can be used only by super user ( but there are workarounds to make it usable by normal user )
Cron expects system to be running 24 x 7. If a job is scheduled, and system is down during that time, job is not executed.Anacron doesn’t expect system to be running 24 x 7. If a job is scheduled, and system is down during that time, it start the jobs when the system comes back up.
Ideal for serversIdeal for desktops and laptops
Use cron when a job has to be executed at a particular hour and minuteUse anacron when a job has to be executed irrespective of hour and minute

Subversion Tutorial: 10 Most Used SVN Commands with Examples

SVN stands for Subversion.
Subversion is a free/open-source version control system. Subversion manages files and directories over time. A tree of files is placed into a central repository. The repository is much like an ordinary file server, except that it remembers every change ever made to your files and directories. This allows you to recover older versions of your code, or examine the history of how your code was changed.
This article explains some basic SVN commands with examples.

SVN Working Copy

SVN is a repository that holds all our versioned data, which is also called as SVN server. SVN client program which manages local reflections of portions of that versioned data which is called as working copy. SVN client can access its repository across networks. Multiple users can access the repository at the same time.

1. SVN Checkout – Create working copy

Checkout command is used to download sources from SVN repository to working copy. If you want to access files from the SVN server, checkout is the first operation you should perform.
SVN checkout creates the working copy, from where you can do edit, delete, or add contents. You can checkout a file, directory, trunk or whole project. To checkout you should know URL of the components you want to checkout.

$ svn checkout/co URL PATH
  • URL is the URL of the components to checkout
  • If PATH is omitted, the basename of the URL will be used as the destination. If multiple URLs are given each will be checked out into a subdirectory of PATH, with the name of the subdirectory being the basename of the URL.
The following example checks out the directory to the given target directory.
$ svn co /home/sasikala/cfg/
A    /home/sasikala/cfg/ftp_user.cfg
A    /home/sasikala/cfg/inventory.cfg
A    /home/sasikala/cfg/email_user.cfg
A    /home/sasikala/cfg/svn-commands
Checked out revision 811.

$ ls /home/sasikala/cfg
.  ..  .svn  email_user.cfg  ftp_user.cfg  inventory.cfg  svn-commands
When you do a checkout, it creates hidden directory named .svn, which will have the repository details.

2. SVN Commit – Save changes to the repository

Whenever you do changes to the working copy, it will not reflect in SVN server. To make the changes permanent, you need to do SVN commit.

$ svn commit -m "log messages"
Explain why you are changing the file in the -m option.
For example, in my working copy, the file named “svn-commands” has the following content.
$ cat /home/sasikala/cfg/svn-commands

$ ls -l /home/sasikala/cfg/svn-commands
-rw-r--r--  1 root root 41 Apr 16 11:15 svn-commands
I made a change in this file (for example, making this file empty).
$ ls -l svn-commands
-rw-r--r--  1 root root 0 Apr 16 11:20 svn-commands
Now commit the file to make the changes permanent in the server.
$ svn commit -m "Making the file empty" svn-commands
Sending        svn-commands
Transmitting file data .
Committed revision 813.
After this whenever you update your working copy or checkout, the changes will appear in the server.

3. SVN List – Lists directory entries

svn list is useful when you want to view the content of the SVN repository, without downloading a working copy.

$ svn list
The following example lists all the files available in the given URL in the repository without downloading a working copy. When you execute svn list command with –verbose option it displays the following information.
  • Revision number of the last commit
  • Author of the last commit
  • Size (in bytes)
  • Date and time of the last commit
$ svn list --verbose
 16 sasikala 28361  Apr 16 21:11 README.txt
 21 sasikala  0  Apr 18 12:22 INSTALL
 22 sasikala  Apr 18 10:17 src/

4. SVN Add – Add a new file to SVN repository

When you want to add a new file (or directory) to the repository you need to use SVN add command. The repository will have newly added file, only when you do SVN commit. Now let us add a new file called “sureshstuff” to our repository.
  • Create a file in local working copy
  • $ ls -l /home/sasikala/cfg/sureshstuff
    -rw-r--r--  1 sasikala root 8 Apr 16 11:33 sureshstuff
  • Add the file into SVN repository
  • svn add filename will add the files into SVN repository.
    $ svn add sureshstuff
    A        sureshstuff
  • Commit the added the file
  • Until you commit, the added file will not be available in the repository.
    $ svn commit -m "Adding a file sureshstuff" sureshstuff
    Adding         sureshstuff
    Transmitting file data .
    Committed revision 814.

5. SVN Delete – Removing a file from repository

SVN delete command deletes an item from the working copy (or repository). File will be deleted from the repository when you do a SVN commit.
$ svn delete URL
Now let us remove the recently created file called “sureshstuff”.
$ svn delete sureshstuff
D         sureshstuff

$ svn commit -m "Removing sureshstuff file" sureshstuff
Deleting       sureshstuff
Committed revision 814.
Now you can do svn list and check whether the file was deleted from the repository.

6. SVN Diff – Display the difference

SVN diff displays the differences between your working copy and the copy in the SVN repository. You can find the difference between two revisions and two paths etc.,
$ svn diff filename

$ svn -r R1:R2 diff filename
The above example compares the filename@R1 and filename@R2.
Now the content of the file sureshstuff looks like this,
$ cat /home/sasikala/cfg/sureshstuff
I edited the content of sureshstuff file from testing to tester, which is shown below using the svn diff command.
$ svn diff sureshstuff
Index: SURESHstuff
--- sureshstuff   (revision 815)
+++ sureshstuff   (working copy)
@@ -1 +1 @@

7. SVN Status – Status of the working copy

Use svn status command to get the status of the file in the working copy. It displays whether the working copy is modified, or its been added/deleted, or file is not under revision control, etc.

$ svn status PATH
The following example shows the status of my local working copy,
$ svn status /home/sasikala/cfg
M     /home/sasikala/cfg/ftp_user.cfg
M       /home/sasikala/cfg/sureshstuff
‘M’ represents that the item has been modified. “svn help status” command will explain various specifiers showed in SVN status command.

8. SVN Log – Display log message

As we discussed in the beginning of this article, SVN remembers every change made to your files and directories. To know all the commits made in a file or directory, use SVN log command.

$ svn log PATH
The following displays all the commits made on sureshstuff file
$ svn log sureshstuff
r815 | sasikala | 2011-04-16 05:14:18 -0700 (Sat, 16 Apr 2011) | 1 line

Adding a file sureshstuff
Since we made only one commit in the file sureshstuff, it shows only one log message with the details.

9. SVN Move – Rename file or directory

This command moves a file from one directory to another or renames a file. The file will be moved on your local sandbox immediately (as well as on the repository after committing).
$ svn move src dest
The following command renames the file “sureshstuff” to “tgs” in a single stroke.
$ svn move sureshstuff tgs
A         tgs
D         sureshstuff

$ ls
.#  ..  .svn  email_user.cfg  ftp_user.cfg  inventory.cfg  tgs
Now the file is renamed only in the working copy, not in the repository. To make the changes permanent, you need to commit the changes.
$ svn commit -m "Renaming sureshstuff to tgs" tgs
Adding         tgs
Transmitting file data .
Committed revision 816.

10. SVN Update – Update the working copy.

svn update command brings changes from the repository into your working copy. If no revision is specified, it brings your working copy up-to-date with the HEAD revision. Otherwise, it synchronizes the working copy to the revision given in the argument.
Always before you start working in your working copy, update your working copy. So that all the changes available in repository will be available in your working copy. i.e latest changes.

$ svn update PATH
In case some other user added/deleted file in URL,, your working copy will not have those files by default, until you update your working copy.
$ svn update
A  new/usercfg
A  new/webcfg
Updated to revision 819.
In the above svn update command output, A represents that this file is “Added” to the working copy.

5 Methods to Identify Your Linux File System Type (Ext2 or Ext3 or Ext4)

Method 1: Use df -T Command

The -T option in the df command displays the file system type.
# df -T | awk '{print $1,$2,$NF}' | grep "^/dev"
/dev/sda1 ext2 /
/dev/sdb1 ext3 /home
/dev/sdc1 ext3 /u01

Method 2: Use Mount Command

Use the mount command as shown below.
# mount | grep "^/dev"
/dev/sda1 on / type ext2 (rw)
/dev/sdb1 on /home type ext3 (rw)
/dev/sdc1 on /u01 type ext3 (rw)
As shown in the above example:
  • /dev/sda1 is ext2 file system type. (mounted as /)
  • /dev/sdb1 is ext3 file system type. (mounted as /home)
  • /dev/sdc1 is ext3 file system type. (mounted as /u01)

Method 3: Use file Command

As root, use the file command as shown below. You need to pass the individual device name to the file command.
# file -sL /dev/sda1
/dev/sda1: Linux rev 1.0 ext2 filesystem data (mounted or unclean) (large files)

# file -sL /dev/sdb1
/dev/sda1: Linux rev 1.0 ext3 filesystem data (needs journal recovery)(large files)

# file -sL /dev/sdc1
/dev/sda1: Linux rev 1.0 ext3 filesystem data (needs journal recovery)(large files)
Note: You should execute the file command as root user. If you execute as non-root user, you’ll still get some output. But, that will not display the file system type as shown below.
$ file -sL /dev/sda1
/dev/sda1: writable, no read permission

Method 4: View the /etc/fstab file

If a particular mount point is configured to be mounted automatically during system startup, you can identify its file system type by looking at the /etc/fstab file.
As shown in the example below, / is ext2, /home is ext3, and /u01 is ext3.
# cat /etc/fstab
LABEL=/r       /        ext2    defaults    1 1
LABEL=/home    /home    ext3    defaults    0 0
LABEL=/u01     /u01     ext3    defaults    0 0

Method 5: Use fsck Command

Execute the fsck command as shown below. This will display the file system type of a given device.
# fsck -N /dev/sda1
fsck 1.39 (29-May-2006)
[/sbin/fsck.ext2 (1) -- /] fsck.ext2 /dev/sda1

# fsck -N /dev/sdb1
fsck 1.39 (29-May-2006)
[/sbin/fsck.ext3 (1) -- /home] fsck.ext3 /dev/sdb1

# fsck -N /dev/sdc1
fsck 1.39 (29-May-2006)
[/sbin/fsck.ext3 (1) -- /u01] fsck.ext3 /dev/sdc1
If you don’t have the root access, but would like to identify your file system type, use /sbin/fsck -N as shown above.

9 Tips to Use Apachectl and Httpd like a Power User

After you have installed Apache2, if you want to use apachectl and httpd to it’s maximum potential, you should go beyond using start, stop and restart. The 9 practical examples provided in this article will help you to use apachectl and httpd very effectively.

Apachectl acts as SysV init script, taking arguments like start, stop, restart and status. It also acts as front-end to httpd command, by simply passing the command line arguments to httpd.  So, all the commands you execute using apachectl, can also be executed directly by calling httpd.

1. Pass different httpd.conf filename to apachectl

Typically you’ll modify the original httpd.conf to try out different Apache directives. If something doesn’t work out, you’ll revert back the changes. Instead of playing around with the original httpd.conf, copy it to a new httpd.conf.debug and use this new httpd.conf.debug file with Apache for testing purpose as shown below using option -f.
# apachectl -f conf/httpd.conf.debug
# httpd -k start -f conf/httpd.conf.debug
[Note: you can use either apachectl or httpd as shown above]

# ps -ef | grep http
root   25080     1  0 23:26 00:00:00 /usr/sbin/httpd -f conf/httpd.conf.debug
apache 25099 25080  0 23:28 00:00:00 /usr/sbin/httpd -f conf/httpd.conf.debug
[Note: ps shows the httpd running with httpd.conf.debug file]
Once you are satisfied with the changes and Apache runs without any problem with httpd.conf.debug,  you can copy the changes to httpd.conf and start the Apache normally as shown below.
# cp httpd.conf.debug httpd.conf
# apachectl stop
# apachectl start

# ps -ef | grep httpd
root     25114     1  0 23:28 00:00:00 /usr/sbin/httpd -k start
daemon   25115 25114  0 23:28 00:00:00 /usr/sbin/httpd -k start
[Note: ps indicates that the httpd is running using the default config file]

2. Use a temporary DocumentRoot without modifying httpd.conf

This is very helpful, when you are trying out different layout for your website and don’t want to modify the original files under the default DocumentRoot. Take a copy of your original DocumentRoot directory (/var/www/html) to a new temporary DocumentRoot directory (/var/www/html_debug). Make all your changes under this temporary DocumentRoot directory (/var/www/html_debug) and start the Apache with this temporary directory as shown below using option -c.
# httpd -k start -c "DocumentRoot /var/www/html_debug/"
If you want to go back to original configuration using the default DocumentRoot (/var/www/html), simply restart the Apache as shown below.
# httpd -k stop
# apachectl start

3. Increase the LogLevel temporarily

While you are debugging an issue, you can change the LogLevel of the Apache temporarily, without modifying the LogLevel directive in the httpd.conf as shown below using option -e. In this example, the LogLevel is set to debug.
# httpd -k start -e debug
[Sun Aug 17 13:53:06 2008] [debug] mod_so.c(246): loaded module auth_basic_module
[Sun Aug 17 13:53:06 2008] [debug] mod_so.c(246): loaded module auth_digest_module
Possible values you can pass to option -e are: debug, info, notice, warn, error, crit, alert, emerg

4. Display the modules compiled inside Apache using option -l

# httpd -l
Compiled in modules:

5. Display both static and dynamic module loaded by Apache

When you pass option -l, to httpd, it will display only the static modules. Passing option -M, will display both static and shared modules as shown below.
# httpd -M
Loaded Modules:
core_module (static)
mpm_prefork_module (static)
http_module (static)
so_module (static)
auth_basic_module (shared)
auth_digest_module (shared)
authn_file_module (shared)
authn_alias_module (shared)
Syntax OK

6. Show all accepted directives inside httpd.conf

This is like an extended help for httpd, which will display all the httpd.conf directives and the places where they are valid. For a specific directive, it tells all the possible values and where it can be used inside the httpd.conf.  This can be very helpful, when you want to quickly know about a particular Apache directive.
# httpd -L
HostnameLookups (core.c)
"on" to enable, "off" to disable reverse DNS lookups, or "double" to enable double-reverse DNS lookups
Allowed in *.conf anywhere

ServerLimit (prefork.c)
Maximum value of MaxClients for this run of Apache
Allowed in *.conf only outside ,  or 

KeepAlive (http_core.c)
Whether persistent connections should be On or Off
Allowed in *.conf only outside ,  or 

LoadModule (mod_so.c)
a module name and the name of a shared object file to load it from
Allowed in *.conf only outside ,  or 

7. Validate the httpd.conf after making changes

Use option -t to validate whether there are any issues with a specific Apache configuration file. In the example shown below, it displays that there is a problem at line 148 in the httpd.conf.debug. mod_auth_basicso is missing a . (period) before the so.
# httpd -t -f conf/httpd.conf.debug
httpd: Syntax error on line 148 of /etc/httpd/conf/httpd.conf.debug:
Cannot load /etc/httpd/modules/mod_auth_basicso into server:
/etc/httpd/modules/mod_auth_basicso: cannot open shared object file: No such file or directory
Once you fix the issue, it will display Syntax OK.
# httpd -t -f conf/httpd.conf.debug
Syntax OK

8. Display the httpd build parameters

Use option -V (upper-case V), to display Apache version number and all the parameters that are used while building the Apache.
# httpd -V
Server version: Apache/2.2.9 (Unix)
Server built:   Jul 14 2008 15:36:56
Server's Module Magic Number: 20051115:15
Server loaded:  APR 1.2.12, APR-Util 1.2.12
Compiled using: APR 1.2.12, APR-Util 1.2.12
Architecture:   32-bit
Server MPM:     Prefork
threaded:     no
forked:     yes (variable process count)
Server compiled with....
-D APACHE_MPM_DIR="server/mpm/prefork"
-D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
-D HTTPD_ROOT="/etc/httpd"
-D SUEXEC_BIN="/usr/sbin/suexec"
-D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
-D DEFAULT_LOCKFILE="logs/accept.lock"
-D DEFAULT_ERRORLOG="logs/error_log"
-D AP_TYPES_CONFIG_FILE="conf/mime.types"
-D SERVER_CONFIG_FILE="conf/httpd.conf"
If you want display only the Apache version number, use the option -v (lower-case v) as shown below.
# httpd -v
Server version: Apache/2.2.9 (Unix)
Server built:   Jul 14 2008 15:36:56

9. Load a specific module only on demand.

Sometimes you may not want to load all the modules in the Apache. For e.g. You may want to load ldap related modules to Apache, only when you are testing LDAP. This can be achieved as shown below.
Modify the httpd.conf and add IfDefine directive called load-ldap (you can name this anything you want).

LoadModule ldap_module modules/
LoadModule authnz_ldap_module modules/
When you are testing ldap and would like to Load the ldap related modules, pass the load-ldap to Option -D, as shown below:
# httpd -k start -e debug -Dload-ldap -f /etc/httpd/conf/httpd.conf.debug
[Sun Aug 17 14:14:58 2008] [debug] mod_so.c(246): loaded module ldap_module
[Sun Aug 17 14:14:58 2008] [debug] mod_so.c(246): loaded module authnz_ldap_module
[Note: Pass -Dload-ldap, to load the ldap modules into Apache]

# apachectl start
[Note: Start the Apache normally, if you don't want to load the ldap modules.]