[SOLVED] How can I stop a function called in a script?
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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".
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).
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.