LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-30-2010, 07:25 AM   #1
rylphs
LQ Newbie
 
Registered: Aug 2010
Posts: 17

Rep: Reputation: 0
Question How can I stop a function called in a script?


Hello,

I made the following script:

Code:
#!/bin/bash

logErrors(){
exec 2>/tmp/errorlogfifo
while true
do
cat /tmp/errorlogfifo |echo $(date "+[%Y-%m-%d %H:%m:%S]") Error: >/var/log/error.log
done
} logErrors ls inexistentFile exit 0
If I execute the script I see the formated error message in /var/log/error.log, as to be expected. After the script execution, I hoped "logErrors" was not executing anymore, but when, in another terminal, I call "echo something > /tmp/errorlogfifo", I still see the formated error message "something" in "/var/log/error.log". How can I finalize "logErrors" before the script ends? Or, maybe someone knows a better way to format and log the erros in a script.

I forgot to say that the file "/tmp/errorlogfifo" is a named pipe created by the command "mkfifo /tmp/errorlogfifo".

Sorry for my bad English.
Thanks.

Last edited by rylphs; 09-30-2010 at 07:34 AM.
 
Old 09-30-2010, 07:50 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
Well the way I understand it, you need to save your fd to another and then call it back later.
Code:
exec 3>&2 2>/tmp/errorlogfifo

<do your stuff>

exec 2>&3 3>&-
 
Old 09-30-2010, 08:23 AM   #3
rylphs
LQ Newbie
 
Registered: Aug 2010
Posts: 17

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by grail View Post
Well the way I understand it, you need to save your fd to another and then call it back later.
Code:
exec 3>&2 2>/tmp/errorlogfifo

<do your stuff>

exec 2>&3 3>&-
Sorry if I was not clear enough, but what I want, is to catch all the errors in the script, format them and send to a log file. To do that, I redirected the stderr to a namedpipe and I used a function that is allways reading (in a infinite loop) the content of that pipe, formatting and sending it to a log file. But the problem is that the function still running after the script ends. Even If i close the shell that executed the script, the function keeps running.

I am already doing what you suggested inside the "logErros" function (without recovering the default state of the descriptor).

Thank you for you suggestion.
 
Old 09-30-2010, 08:34 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
hmmm ... so if it an infinite loop, how is the script stopping?
 
Old 09-30-2010, 08:54 AM   #5
doublejoon
Member
 
Registered: Oct 2003
Location: King George, VA
Distribution: RHEL/CentOS/Scientific/Fedora, LinuxMint
Posts: 370

Rep: Reputation: 44
Could you add at end of your function?

Code:
echo && > /tmp/logerrors.pid
kill $(cat /tmp/logerrors.pid)
 
Old 09-30-2010, 09:29 AM   #6
rylphs
LQ Newbie
 
Registered: Aug 2010
Posts: 17

Original Poster
Rep: Reputation: 0
Red face

Quote:
Originally Posted by grail View Post
hmmm ... so if it an infinite loop, how is the script stopping?
I really sorry because I missed something. I was calling the function with the "&" symbol. I had changed my script to test, and I forgot to undo the change. So, the correct script is:

Code:
#!/bin/bash

logErrors(){

    exec 2>/tmp/errorlogfifo

    while true

    do

        cat /tmp/errorlogfifo |echo $(date "+[%Y-%m-%d %H:%m:%S]") Error: >/var/log/error.log

    done

}

logErrors& #I missed the "&" I added.
ls inexistentFile
exit 0
It seems that in a call of a function in background, kernel keeps a process, with the same name of the script, running the function in memory. Am I correct?

I noticed I don't need the while loop. If I just have the "cat" call everything works fine.

I have another doubt, Is there some way to discover the ID of the function I called in background. I tried the $PPID variable, but the $PPID of the script is different from the PID of the process kept running.

Thank you and sorry for the trouble.
 
Old 09-30-2010, 09:32 AM   #7
rylphs
LQ Newbie
 
Registered: Aug 2010
Posts: 17

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by doublejoon View Post
Could you add at end of your function?

Code:
echo && > /tmp/logerrors.pid
kill $(cat /tmp/logerrors.pid)
Sorry, but what exactly these call do?
 
Old 09-30-2010, 10:54 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
So a little searching and playing I found this:
Code:
MYPID=$(jobs -p)
Now you should be able to kill it in the appropriate place before exiting your script.
 
Old 09-30-2010, 11:52 AM   #9
rylphs
LQ Newbie
 
Registered: Aug 2010
Posts: 17

Original Poster
Rep: Reputation: 0
Thumbs up

Quote:
Originally Posted by grail View Post
So a little searching and playing I found this:
Code:
MYPID=$(jobs -p)
Now you should be able to kill it in the appropriate place before exiting your script.
I've found another solution:

Code:
#!/bin/bash

logErrors(){
    scriptPid=$$
    while ps -p $scriptPid
    do
        cat /tmp/errorlogfifo |echo $(date "+[%Y-%m-%d %H:%m:%S]") Error: >/var/log/error.log
    done&
}

logErrors
ls inexistentFile
exit 0
Instead of have to kill the process, the function runs only while the script is running.

I still am having some problems, but it's another problem. I'll mark this thread as solved. Thank you everybody for the help.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sleeping function called from invalid context kushneeraj Programming 8 04-16-2013 09:25 PM
mdio probe function not called. rashyd80 Programming 3 07-20-2010 02:45 AM
mdio function not called.. =( rashyd80 Linux - Kernel 1 07-18-2010 06:11 AM
JavaScript function disappears after getting called for once mohtasham1983 Programming 1 01-15-2008 09:31 AM


All times are GMT -5. The time now is 08:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration