bash: closing file descriptors from a process | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

bash: closing file descriptors from a process

Shell Programming and Scripting


Tags
bash, file descriptors, lvm, syslog

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-12-2010
jelloir jelloir is offline
Registered User
 
Join Date: Sep 2009
Last Activity: 11 July 2012, 7:29 PM EDT
Location: Rye, Victoria, AU
Posts: 49
Thanks: 12
Thanked 0 Times in 0 Posts
bash: closing file descriptors from a process

Below is a test script to illustrate a problem from a larger script I am writing.


Code:
$ 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 vg00/var-data-snapshot 2> >(logger -t "loggingtest.crit") 1> >(logger -t "loggingtest.info")

When I run this I receive the following output in /var/log/syslog.
Specifically I cannot understand where the "File descriptor 62 left open" and "File descriptor 63 left open" errors are coming from. Also the script hangs until I press enter.


Code:
Sep 12 11:57:39 debvelopment loggingtest.crit: File descriptor 62 left open
Sep 12 11:57:39 debvelopment loggingtest.crit: File descriptor 63 left open
Sep 12 11:57:39 debvelopment loggingtest.info:   Logical volume "var-data-snapshot" created
Sep 12 11:57:39 debvelopment loggingtest.crit: File descriptor 62 left open
Sep 12 11:57:39 debvelopment loggingtest.crit: File descriptor 63 left open
Sep 12 11:57:39 debvelopment loggingtest.info:   Logical volume "var-data-snapshot" successfully removed

I have done research but I am finding it difficult to understand whether these errors are related to lvm, syslog or something I am doing wrong in bash? let alone how to close the file descriptors properly.

---------- Post updated at 06:12 PM ---------- Previous update was at 12:01 PM ----------

I can now prevent the errors and know that the descriptors are from "logger" but I still do not understand it.

I added "62>&- 63>&-" to the end of each command and this resolves the errors e.g.


Code:
#!/bin/bash

lvcreate -s -l 100%FREE -n var-data-snapshot vg00/var-data 2> >(logger -t "loggingtest.crit") 1> >(logger -t "loggingtest.info") 62>&- 63>&-

sync &
wait

lvremove -f vg00/var-data-snapshot 2> >(logger -t "loggingtest.crit") 1> >(logger -t "loggingtest.info") 62>&- 63>&-

But what if the file descriptors where to change? How can I identify the relevant file descriptors and then close them? Do I need to even be concerned as they appear to close anyway?

Thanks
Sponsored Links
    #2  
Old 09-12-2010
konsolebox konsolebox is offline
Registered User
 
Join Date: Aug 2010
Last Activity: 30 August 2014, 6:29 AM EDT
Posts: 48
Thanks: 0
Thanked 10 Times in 9 Posts
you just have to close them then:


Code:
#!/bin/bash

{
	exec 62>&-
	exec 63>&-
	lvcreate -s -l 100%FREE -n var-data-snapshot vg00/var-data
} 2> >(logger -t "loggingtest.crit") 1> >(logger -t "loggingtest.info")

sync &
wait

{
	exec 62>&-
	exec 63>&-
	lvremove -f vg00/var-data-snapshot
} 2> >(logger -t "loggingtest.crit") 1> >(logger -t "loggingtest.info")



---------- Post updated at 04:19 PM ---------- Previous update was at 04:16 PM ----------

Thanks to this thread I now know why I have some fds left open even if I close a duplicate fd.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Bash script to process file without regard for case manouche Shell Programming and Scripting 1 04-06-2010 07:34 PM
starting a bash session as child process to another bash session from a process alirezan Shell Programming and Scripting 5 03-13-2009 09:03 PM
Closing ssh when process run on remote tipi UNIX for Dummies Questions & Answers 1 09-03-2008 06:46 PM
how to run a process after closing the terminal lakshmananindia UNIX for Advanced & Expert Users 3 09-03-2007 11:05 AM
Max No of Open File Descriptors in a process lakshmankumar12 UNIX for Advanced & Expert Users 2 12-29-2004 10:54 PM



All times are GMT -4. The time now is 09:43 AM.