April 26, 2011

Writing System V init scripts for Red Hat Linux

Filed under: linux, shell scripts — lancevermilion @ 6:06 pm

I was digging around for the different tags chkconfig/sysv init scripts accepts and what they mean. This was the most complete page I found.


Writing System V init scripts for Red Hat Linux

All System V init scripts are named /etc/rc.d/init.d/<servicename> where <servicename> is the name of the service. There must be no “.init” suffix.

This path will very likely be moved to /etc/init.d in the future. Once Red Hat Linux 7.0 is installed, you can access scripts as /etc/init.d/<servicename>, via symlinks

Sample Script:

#	/etc/rc.d/init.d/<servicename>
#	<description of the *service*>
#	<any general comments about this init script>
# <tags -- see below for tag definitions.  *Every line* from the top
#  of the file to the end of the tags section must begin with a #
#  character.  After the tags section, there should be a blank line.
#  This keeps normal comments in the rest of the file from being
#  mistaken for tags, should they happen to fit the pattern.>

# Source function library.
. /etc/init.d/functions

<define any local shell functions used by the code that follows>

start() {
	echo -n "Starting <servicename>: "
	<start daemons, perhaps with the daemon function>
	touch /var/lock/subsys/<servicename>
	return <return code of starting daemon>

stop() {
	echo -n "Shutting down <servicename>: "
	<stop daemons, perhaps with the killproc function>
	rm -f /var/lock/subsys/<servicename>
	return <return code of stopping daemon>

case "$1" in
	<report the status of the daemons in free-form format,
	perhaps with the status function>
	<cause the service configuration to be reread, either with
	kill -HUP or by restarting the daemons, in a manner similar
	to restart above>
    	<Restarts the servce if it is already running. For example:>
	[ -f /var/lock/subsys/<service> ] && restart || :
	<optional.  If it exists, then it should determine whether
	or not the service needs to be restarted or reloaded (or
	whatever) in order to activate any changes in the configuration
	scripts.  It should print out a list of commands to give to
	$0; see the description under the probe tag below.>
	echo "Usage: <servicename> {start|stop|status|reload|restart[|probe]"
	exit 1
exit $?


  • The restart and reload functions may be (and commonly are) combined into one test, vis:
    restart | reload)
  • You are not prohibited from adding other commands; list all commands which you intend to be used interactively to the usage message.
  • Notice the change in that stop() and start() are now shell functions.

This means that restart can be implemented as


instead of

$0 stop
$0 start

This saves a few shell invocations.

Functions in /etc/init.d/functions

daemon [ –check <name> ] [ –user <username>] [+/-nicelevel] program [arguments] [&]

Starts a daemon, if it is not already running. Does other useful things like keeping the daemon from dumping core if it terminates unexpectedly.

–check <name>:

Check that <name> is running, as opposed to simply the first argument passed to daemon().

–user <username>:

Run command as user <username>

killproc program [signal]

Sends a signal to the program; by default it sends a SIGTERM, and if the process doesn’t die, it sends a SIGKILL a few seconds later.

It also tries to remove the pidfile, if it finds one.

pidofproc program

Tries to find the pid of a program; checking likely pidfiles, and using the pidof program. Used mainly from within other functions in this file, but also available to scripts.

status program

Prints status information. Assumes that the program name is the same as the servicename.


# chkconfig: <startlevellist> <startpriority> <endpriority>

Required. <startlevellist> is a list of levels in which the service should be started by default. <startpriority> and <endpriority> are priority numbers.
For example:
# chkconfig: 2345 20 80

Read ‘man chkconfig’ for more information.

Unless there is a VERY GOOD, EXPLICIT reason to the contrary, the <endpriority> should be equal to 100 – <startpriority>

# description: <multi-line description of service>

Required. Several lines of description, continued with ‘\’ characters. The initial comment and following whitespace on the following lines is ignored.

# description[ln]: <multi-line description of service in the language \
#                                         ln, whatever that is>

Optional. Should be the description translated into the specified language.

# processname:

Optional, multiple entries allowed. For each process name started by the script, there should be a processname entry.
For example, the samba service starts two daemons:
# processname: smdb
# processname: nmdb

# config:

Optional, multiple entries allowed. For each static config file used by the daemon, use a single entry.
For example:

# config: /etc/httpd/conf/httpd.conf
# config: /etc/httpd/conf/srm.conf

Optionally, if the server will automatically reload the config file if it is changed, you can append the word “autoreload” to the line:

# config: /etc/foobar.conf autoreload

# pidfile:

Optional, multiple entries allowed. Use just like the config entry, except that it points at pidfiles. It is assumed that the pidfiles are only updated at process creation time, and not later. The first line of this file should be the ASCII representation of the PID; a terminating newline is optional. Any lines other than the first line are not examined.

# probe: true

Optional, used IN PLACE of processname, config, and pidfile. If it exists, then a proper reload-if-necessary cycle may be acheived by running these commands:

command=$(/etc/rc.d/init.d/SCRIPT probe)
[ -n “$command” ] && /etc/rc.d/init.d/SCRIPT $command

where SCRIPT is the name of the service’s sysv init script.

Scripts that need to do complex processing could, as an example, return “run /var/tmp/<servicename.probe.$$” and implement a “run” command which would execute the named script and then remove it.

Note that the probe command should simply “exit 0” if nothing needs to be done to bring the service into sync with its configuration files.

Copyright (c) 2000 Red Hat Software, Inc.


April 15, 2011

How to properly setup Innodb in MySQL to shrink when optimized.

Filed under: mysql — lancevermilion @ 4:22 pm

Taken from:

There is a more complete answer with regard to InnoDB

Keep in mind the busiest file in the InnoDB infrastructure


This file normally houses four types of information

1) Table Data

2) Table Indexes

3) MVCC (Multiversioning Concurrency Control) Data

4) Table Metadata

Many people create multiple ibdata files hoping for better diskspace management and performance. SORRY, NOT HAPPENING !!!

Unfortunately, OPTIMIZE TABLE against an InnoDB table stored ibdata1 does two things: 1) Makes the table’s data and indexes contiguous inside ibdata1 2) It makes ibdata1 grow because the contiguous data is appended to ibdata1

You can segragate Table Data and Table Indexes from ibdata1 and manage them independently. To shrink ibdata1 once and for all you must do the following

1) MySQLDump all databases into a SQL text file (call it SQLData.sql)

2) Drop all databases (except mysql schema)

3) Shutdown mysql

4) Add the following lines to /etc/my.cnf





Sidenote: Whatever your set for innodb_buffer_pool_size, make sure innodb_log_file_size is 25% of innodb_buffer_pool_size.

5) Delete ibdata1, ib_logfile0 and ib_logfile1

At this point, there should only be the mysql schema in /var/lib/mysql

6) Restart mysql

This will recreate ibdata1 at 10MB, ib_logfile0 and ib_logfile1 at 1G each

7) Reload SQLData.sql into mysql

ibdata1 will grow but only contain table metadata

Each InnoDB table will exist outside of ibdata1

Suppose you have an InnoDB table named mydb.mytable. If you go into /var/lib/mysql/mydb, you will see two files representing the table

mytable.frm (Storage Engine Header) mytable.ibd (Home of Table Data and Table Indexes for mydb.mytable)

ibdata1 will never contain InnoDB data and Indexes anymore.

With the innodb_file_per_table option in /etc/my.cnf, you can run OPTIMIZE TABLE mydb.mytable and the file /var/lib/mysql/mydb/mytable.ibd will actually shrink.

I have done this many time in my career as a MYSQL DBA

In fact, the first time I did this, I collapsed a 50GB ibdata1 file into 50MB.

Give it a try. If you have further questions on this, email me. Trust me. This will work in the short term and over the long haul. !!!

www . logicworks . net

April 7, 2011

How to find the ref type of a variable in Perl

Filed under: perl — lancevermilion @ 10:57 am

For quite a while I have been bothered with the fact that I didn’t know the type or a ref variable.

Original info from:

A simple way to figure out the variable type.

print ref \$scalar;        # SCALAR :: deref a scalar and print ref type
print ref $scalar_ref;    # SCALAR 
print ref ($scalar_ref);  # SCALAR :: same as above; diff way to write it
print ref \@array_ref;   # ARRAY   :: deref an array and print ref type
print ref $array_ref;     # ARRAY
print ref $hash_ref;      # HASH
print ref \%hash;         # HASH    :: deref a hash and print the ref type

Sample Code

$\="\n";                    # Will print as a last char with print stmt

############ scalar ########
$scalar = "A";
$scalar_ref = \$scalar;

# dereference
print $scalar;                                # prints A
print ${$scalar_ref};                         # prints A

print "SCALAR_REF: " . ref $scalar_ref;       # prints SCALAR
print "SCALAR_REF: " . ref($scalar_ref);      # prints SCALAR
print "SCALAR DEREF: " . ref \$scalar;        # prints SCALAR
############# Array ###########
$array_ref = ["a","b","c"];                   # Array Reference
@array=("1","2",3);                           #Array from list

# Array de-reference
print "@{$array_ref}";                        # prints  a b c
print @{$array_ref};                          # prints  abc
print "@{\@array}";                           # prints  1 2 3
print @{\@array};                             # prints  123

print "ARRAY DEREF: " . ref \@array;          # prints  ARRAY
print "ARRAY_REF: " . ref $array_ref;         # prints  ARRAY

print "Count:".@array;                        # prints Count:3
print "Count:".scalar(@array);                # prints Count:3
print "Count:".scalar(@array);                # prints Count:3
print "Count:".scalar(@{$array_ref});         # prints Count:3
print $array_ref->[0];                        # prints a

############ Hashes #####################
#Hash from list
%hash= (
        name => "Ann",
        age  => 23,

#Hash reference
$hash_ref= {
        name => "Bob",
        age  => 24,

print %hash;                                  # prints nameAnnage23
print $hash{name};                            # prints Ann
print $hash{age};                             # prints 23
print "HASH DEREF: " . ref \%hash;            # prints HASH

# Hash de-reference 
print %{$hash_ref};                           # prints nameBobage24
print $hash_ref->{name};                      # prints Bob
print $hash_ref->{age};                       # prints 24
print "HASH_REF: " . ref $hash_ref;           # prints HASH

Output from Sample Code

a b c
1 2 3

Blog at