Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

Wednesday, June 5, 2013

logrotate

logrotate -f /etc/logrotate.conf
Nothing more to say.

Tuesday, January 12, 2010

Profiling shared library on Linux using sprof

It's not as easy as one would/should expect. (Read here about valgrind if you don't want to use sprof.)

First - FORGET ABOUT gprof. gprof is used for the applications but will NOT WORK for shared libraries. Sorry about that. I learned about it the hard way. What you need to use is sprof.

Second - sprof may make you bite your fingers if your glibc library is not up-to-date. It will be displaying the nasty message: "sprof: failed to load shared object"
If this is what you see then you HAVE TO update your glibc. Read about the bug: here. It may not be as simple to upgrade as well... If I have time I will write how to do it through the .iso file (if you have one.)

Third - Yes, as it is not enough, there is an other surprize! The -pg option works well for gprof, but screws up things for sprof! You need to remove this option in order to get proper results from the shared libraries. Otherwise you will get only zeroes as the execution time for the functions.

So, at the end, what to do?

Assuming you have the patch installed or you have glibc-2.5-34 or newer version installed do as follows (the best, in my opinion, way, assuming you have write access to the current directory.)

1) Compile your shared library (libmylib.so) in debug (-g) mode. No -pg.
2) export LD_PROFILE_OUTPUT=`pwd`
3) export LD_PROFILE=libmylib.so
4) rm -f $LD_PROFILE.profile
4) execute your program that loads libmylib.so
5) sprof PATH-TO-LIB/$LD_PROFILE $LD_PROFILE.profile -p >log
6) See the log.

I hope it will help you.

Thursday, September 25, 2008

How do I find the name of the current shell that I am working in

There is a command that will work on all platforms I tested (HP-UX, SunOS, Linux, MacOS) and shells (sh,ksh,bash,csh,zsh)

ps -p $$ | grep $$ | awk '{ print $NF }'

Be aware that the shell name can be bash or -bash

Friday, May 30, 2008

Script Shell Syntax checking

I'm writing a lot of stuff in shell (Korn,Bourne,Bash) and I had a need for syntax checker in the build time. Something I could use either in the command line (like a compiler) or in the make time which should stop the build whenever there is a syntax error in my script.

Here is what I came up to. Simply copy/paste the code below to a file named shck.sh (or whatever you wish to call it), do chmod+x shck.sh and off you go!

Usage:
shck.sh myscript.sh
-or-
cat myscript.sh | shck.sh

You can also use it in the makefile. It will return value 1 if there was an error.
-----------------------------------------------------
shck_return() {
    return $1
}
shck_errno=0
echo "set -n ">random.$$.sh
cat $1 >>random.$$.sh
chmod +x random.$$.sh
./random.$$.sh >errors.$$.err 2>errors.$$.err
rm -f random.$$.sh
grep -v ": warning: line" errors.$$.err | sed -e s/random.$$.sh/$1/g >errors.$$.err2
[ -s errors.$$.err2 ] && {
    shck_errno=1
    cat errors.$$.err2
}
rm -f errors.$$.err errors.$$.err2
shck_return $shck_errno

Friday, May 2, 2008

poll: protocol failure in circuit setup (Linux)

To tell the truth, I'm not sure it's the Linux issue only. It can be on other platforms as well.

If you saw something like this in telnet/ssh:

connect to address 1.2.3.4 port 544: Connection refused
Trying krb4 rsh...
connect to address 1.2.3.4 port 544: Connection refused
trying normal rsh (/usr/bin/rsh)
poll: protocol failure in circuit setup

then what probably causes it is firewall (iptables)
The quicky method is simply to shutdown the iptables firewall:
> su -
> services iptables stop

However, if you reboot your machine, the iptables will start and you will hit the problem again.

To permanently shut down the firwall service then, do the following:
> su -
> chkconfig --list iptables

- you will probably see this:
iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off

- then do:
> chkconfig --level 345 iptables off
> chkconfig --list iptables

- now you should see this:
iptables 0:off 1:off 2:on 3:off 4:off 5:off 6:off

Of course, for the current session, you still will have to call service iptables stop, but I guess you already know it.

Wednesday, April 9, 2008

Returning error code from a script in Linux

Most people moving to Linux are getting surprised by the fact that a simple operation "return 1" can cause an error:

./a.sh: line 1: return: can only `return' from a function or sourced script

Not all Linuxes return this problem, however, it's enough anoying if you want to keep the script for all the platforms.

There is a simple solution for this, though!


# $1 - error code
fun_return() {
return $1
}

fun_return 1



You can check this out by yourself. Write a sctipt with the above example, run it and check the value of $?

UNIX/Linux shell and returning values from functions

Here is an interesting fact.

Let's assume you want to write a function myfun_initialize()

If the function is supposed to initialize some variables then DO NOT RETURN result of this function through the `function caller` (well, I have just made up this name.)

In other words...

right:
myfun_initialize parameters
[ "$var_initialize" = "ok" ] {
echo "Oops!"
return
}

wrong:
[ "`myfun_initialize parameters`" = "ok" ] {
echo "Oops!"
return
}


Why is it wrong? I wish I had a simple explanation, but if the myfun_initialize function initializes some external variables you would like to use later on, then by calling `myfun_initialize` you will initialize temporary variables, and these outside of the call will be unchanged!

Test it yourself:


Here is the text

MYVAR="nothing"

# $1 - value
myfun() {
MYVAR="$1"
}

echo "MYVAR before calling myfun is $MYVAR"
if [ "`myfun something`" = "" ]
then
echo "myfun returned an empty string"
echo "MYVAR after calling \`myfun something \` is $MYVAR"
else
echo "myfun hasn't returned an empty string?!"
echo "MYVAR after calling \`myfun something\` is $MYVAR"
fi

myfun something
echo "MYVAR after calling myfun something is $MYVAR"



So what is the solution?
Use a variable to return the result.
Example:


myfunc() {
....
var_myfunc="ok"
}

myfunc
[ "$myfunc" = "ok" ] && {
...
}