Closing open file descriptors from /proc/pid/fd


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Closing open file descriptors from /proc/pid/fd
# 8  
Old 09-26-2011
If you means exec 5>&- you close the file descriptor #5 opened by your shell (if it was opened or nothing happens).

Last edited by rbatte1; 01-17-2017 at 07:56 AM..
# 9  
Old 09-26-2011
Quote:
Originally Posted by alpha_romeo
hi ygemici,

thanks for the suggestions Smilie . But i don't want to end the process. the process is a 24*7 running application. i shouldn't kill it.

I need to only close certain open file descriptors.

regards
romeo
if your process always works,why do you need close the opened files belong to your process?
if you want to close fd ın your script you can use this
test still fd is open?
Code:
lsof -a -d3|grep $$

and or you can use like this..
Code:
# ./chkfd filetest

-- chkfd code for 3fd(u)--
Code:
#!/bin/bash
exec 3<> $1
echo "3.FD is opened now.."
lsof -a -d3|grep $$
exec 3>&-
if [[ $(lsof -a -d3|grep $$|grep -v pipe) = "" ]] ; then
echo -e "\n3.FD is closed successfully" ;
else echo -e "\n3.FD maybe is still open, check your script code"
lsof -a -d3|grep $$
fi

# 10  
Old 09-26-2011
ygemici,

so exec 3>&- works.. thanks Smilie.. that answers my question.

i want to close the fds because there are too many open fds in the process and that is causing performance issues. I know it needs to be addressed in application and this is only a work around to close the fds from shell. i am looking for a temporary fix only.

Thanks all for your inputs
# 11  
Old 09-26-2011
Quote:
Originally Posted by alpha_romeo
yazu,

what will happen if i attempt to do a 5>&- from /proc/pid/fd ??.. will the fd not be closed ?

regards
romeo
you cann't/mustn't close fds belong to any process that opened fds( in -> /proc/PID/fd) by with `FDN>&-` command.For this with safely,you must kill the process..

But if you cannot kill it and you are sure what will be happened or wıll be not happened after close fds then you can execute these(below comms/calls) but I dont recommend this way for this job.
Code:
# ls -1 /proc/PID/fd ##(find fds)

Code:
gdb --pid PID #(attach process by debugger)
.......
.......
(gdb) p close(1) 
(gdb) p close(2) 
......
(gdb) p close(X) 
detach
quit

Code:
p close (1) --> print & close(syscall) the "FD 1" belong to PID
....


regards
ygemici
This User Gave Thanks to ygemici For This Post:
# 12  
Old 09-27-2011
thanks ygemici , it matches my requirements
# 13  
Old 09-29-2011
Quote:
Originally Posted by ygemici
you cann't/mustn't close fds belong to any process that opened fds( in -> /proc/PID/fd) by with `FDN>&-` command.For this with safely,you must kill the process..

But if you cannot kill it and you are sure what will be happened or wıll be not happened after close fds then you can execute these(below comms/calls) but I dont recommend this way for this job.
Code:
# ls -1 /proc/PID/fd ##(find fds)

Code:
gdb --pid PID #(attach process by debugger)
.......
.......
(gdb) p close(1) 
(gdb) p close(2) 
......
(gdb) p close(X) 
detach
quit

Code:
p close (1) --> print & close(syscall) the "FD 1" belong to PID
....


regards
ygemici
Hey,

I am triggering the gdb from script like:
gdb -command=file -p pid

it is running but saying hit return to continue.

how can i give gdb command so that it is ran as a silent command with out need for any user input ??
# 14  
Old 09-29-2011
You have got a really poorly designed application if you have to go in and close file handles in the manner you describe.

Why not fix the application?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Closing a graphical application by pid

Hi all, I have a small graphical application whose only purpose is to pop up certain types of messages. Currently, I'm using it in a Bash script like this: ./MyProg "this is my message" while do ... do things ... done What I want to do is after the while loop is over (it does... (4 Replies)
Discussion started by: Zel2008
4 Replies

2. Solaris

Open File Descriptors Current vs. Max

Hello all, I have been tasked with finding the current open file descriptors versus the limit set. In Linux, this can be done like so: cat /proc/sys/fs/file-nr 3391 969 52427 | | | | | | | | maximum open file descriptors | total free allocated... (2 Replies)
Discussion started by: LinuxRacr
2 Replies

3. UNIX for Dummies Questions & Answers

/proc/pid/maps

I think the libc.so is shared between processes, because it is a shared library and OS is engaged for saving memory. But, below, the maps of bash, shows r-xp and r--p rw-p attributes to libc.so which mean private memory space. Can anybody explain this for me? :)cat /proc/$$/maps... (4 Replies)
Discussion started by: vistastar
4 Replies

4. Shell Programming and Scripting

does the pid of background process show in /proc?

Hi all, I'm reading <advanced bash scripting> and there is a example to kill a background process in a limited time,as shown below: #! /bin/bash #set -n TIMEOUT=$1 count=0 hanging_jobs & { while ((count < TIMEOUT));do eval ' && ((count = TIMEOUT))' ((count++)) sleep 1... (6 Replies)
Discussion started by: homeboy
6 Replies

5. Shell Programming and Scripting

closing unwanted open ports using scripts

i have a text file i.e file1.txt which shows open ports on particular system. i have another text file i.e file2.txt which shows a list of allowed ports on a system. for eg: file2.txt 22/tcp ssh 23/tcp telnet. can i have a script which would compare these text files ,file1 and file2 ... (1 Reply)
Discussion started by: anand121
1 Replies

6. Shell Programming and Scripting

bash: closing file descriptors from a process

Below is a test script to illustrate a problem from a larger script I am writing. $ cat /tmp/loggingtest #!/bin/bash lvcreate -s -l 100%FREE -n var-data-snapshot vg00/var-data 2> >(logger -t "loggingtest.crit") 1> >(logger -t "loggingtest.info") sync & wait lvremove -f... (1 Reply)
Discussion started by: jelloir
1 Replies

7. Red Hat

read maps from /proc/pid/

hi, i hav a query abt reading the contents of /proc/pid/maps file.is there any system apis or functions available to get the data from dat file and parse according to my need. i need name of the .so,Create date of the .so file.,Location of .so file etc. please provide a good source. yes i hav... (3 Replies)
Discussion started by: sanjaykhuntia
3 Replies

8. Cybersecurity

Getting PID of Open Connectivity

Hi! I've a Java socket server program that is listening for requests, and using netstat, I can see that the connection is already open. However as the process name (from ps -ef) is very long and I can't grep the program name. Can anyone advise how I find out the PID of the process? Can... (5 Replies)
Discussion started by: swing
5 Replies

9. UNIX for Advanced & Expert Users

Max No of Open File Descriptors in a process

I have set the maximum no of file descriptors open in a process to the value 8192 using the following lines set rlim_fd_max=8192 set rlim_fd_cur=8192 in the /etc/system file. I rebooted the machine and the command ulimit -n / -Hn both display the limits as 8192. However when I run my... (2 Replies)
Discussion started by: lakshmankumar12
2 Replies

10. Cybersecurity

closing open ports

/* Linux Slackware */ Nmap shows the following ports open on the gateway. 21/tcp ftp 22/tcp ssh 23/tcp telnet 25/tcp smtp 37/tcp time 80/tcp http 113/tcp auth 515/tcp printer 587/tcp submission 1024/tcp kdm 6000/tcp x11 ------------------------------- i would like to close as... (10 Replies)
Discussion started by: LowOrderBit
10 Replies
Login or Register to Ask a Question