1753779 Members
7803 Online
108799 Solutions
New Discussion юеВ

Re: shell script

 
SOLVED
Go to solution
Shivkumar
Super Advisor

shell script

Dear sirs,

I have a shell script described below:

# limit the number of open file descriptors
resetFd() {
if [ ! -n "`uname -s |grep -i cygwin || uname -s |grep -i windows_nt`" ]
then
maxfiles=`ulimit -H -n`
if [ ! $? -a "${maxfiles}" != 1024 ]; then
if [ "${maxfiles}" = "unlimited" ]; then
maxfiles=1025
fi
if [ "${maxfiles}" -lt 1024 ]; then
ulimit -n ${maxfiles}
else
ulimit -n 1024
fi
fi
fi
}

In the above code what is the meaning of :-
---

maxfiles=`ulimit -H -n`
if [ ! $? -a "${maxfiles}" != 1024 ]; then
if [ "${maxfiles}" = "unlimited" ]; then
maxfiles=1025
fi
---

Also please explain the final outcome of the script.

Thanks,
Shiv
5 REPLIES 5
James R. Ferguson
Acclaimed Contributor
Solution

Re: shell script

Hi Shiv:

The script's variable 'maxfiles' is established with:

# maxfiles=`ulimit -H -n`

The HARD limit of the kernel parameter 'maxfiles_lim' is requested with '-H -n' [ see the man pages for sh-posix() ].

If the request was successful and the returned value does not equal 1024 [line-2], then a test is made to see if the returned value is "unlimited" [line-3]. If it is "unlimited" then the script's 'maxfiles' variable is set to 1025.

The script continues and finally sets the SOFT limit to the smaller of 1024 or the value of $maxfiles.

The idea is that the kernel parameter 'maxfiles' is the number of files a process is allowed to have open at any one time. This is called the SOFT limit. This value may be increased by a user, but not beyond the HARD limit.

Regards!

...JRF...
CSG Office
Frequent Advisor

Re: shell script

Hello Shivkumar,

In general this code is attempting to test for the ulimit's upper limit. It goes as follows:

maxfiles=`ulimit -H -n` - This sets the maxfiles to the value returned by the command "ulimit -H -n" which will be a numberic value.

if [ ! $? -a "${maxfiles}" != 1024 ]; - This is testing the return code of the previous command ($?) to see if it is not (!) 0. And (-a) the maxfiles returned by "ulimit -H -n" is not 1024 (!= 1024).

if [ "${maxfiles}" = "unlimited" ]; - This checks to see if "ulimit -H -n" returned "unlimited". If the above if and this if are both true, maxfiles will be set to 1025.
Rodney Hills
Honored Contributor

Re: shell script

Sets "maxfiles" to the number of file descriptors. If their are unlimited it sets it to 1025, so that the numeric comparison later in the script has no problem.

The final result is to set the max number of open file descriptors to a maximum of 1024.

PS- That form of the ulimit command is not only available in the posix shell.

HTH

-- Rod Hills
There be dragons...
Rodney Hills
Honored Contributor

Re: shell script

Oops-

Meant to say "only available in posix sh"

Rod Hills
There be dragons...
RAC_1
Honored Contributor

Re: shell script

this code is specific to sh-posix shell.
ulimit -H -n is availabe in sh shell only. If you plan to run it on ksh, then you will have to do /usr/bin/ulimit -H -n
There is no substitute to HARDWORK