LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-04-2005, 06:20 PM   #1
michael_util
Member
 
Registered: Feb 2004
Posts: 47

Rep: Reputation: 15
Bash - background process and exit ??


Hello,

I have a bash script that acks like a wrapper, I start it with a "&" and it runs continuely untill it incounters an error or it shutdown.

It is used to monitor other processes.

I have noticed that after I start the script:

./scriptname start &

and logout ... it appears to exit after some random time, because a command returns an error. I have traps setup to exit if an error occurs:

trap cmd_error ERR

function cmd_error
{
echo "Command failed ${1}"
setterm -bold off
exit 1
}

I have standout being redirected to a file using:

--snip--
##### Start of logging to file
exec 1>&2
exec 6>&1

exec > ${masterhome}/${masterlogs}/log-$$

echo ""
echo "Opening and starting logfile log-$$"
echo "Log file open ... $( date )"
--snip--

I believe that after I logout ... the script tries to write to STDOUT, which is being redirected to a file ... but since I have exited ... my STDOUT is not longer being redirected to a file, so a error occurs causing the trap to exit the script.

I hops this makes sense ... please let me know if this is possible or if I am way off track.

Thanks.

Michael.
 
Old 01-04-2005, 06:50 PM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Maybe stderr is not redirected?

If want to have a program or script running in the background, and continue to run when the shell closes it was started from, use "nohup" ("No Hang Up"). Nohup will redirect its output to a file called "nohup.out". If you don't like that, redirect stdin and stder both to /dev/null (or a file of your choice). Then no file will be created.
Code:
bash$ nohup ./yourscript.sh >/dev/null 2>&1 &
Read "info coreutils nohup" or "man nohup" for more info.

Last edited by Hko; 01-04-2005 at 06:51 PM.
 
Old 01-04-2005, 07:02 PM   #3
michael_util
Member
 
Registered: Feb 2004
Posts: 47

Original Poster
Rep: Reputation: 15
Hello,

I ran some test ... what I was discribing does not seem to be causing the exit. I will continue trouble shooting.

Michael.
 
Old 01-04-2005, 08:31 PM   #4
eigenyeugen
LQ Newbie
 
Registered: Jan 2005
Location: UK
Distribution: Debian
Posts: 11

Rep: Reputation: 0
there's a program called 'screen' that can be used a little like nohup. I think it comes as standard with most distributions of linux.

You type 'screen' and it launches some kind of virtual console thing . You can launch a program in that, then use some keybinding (I can't remember which, look at the man page) to 'detatch' the screen, whereupon it closes the virtual console like thing, but keeps the process running in the background. When you next login you can run screen again and 'reattatch' the previous session and it will act as if you've been sitting there logged on the whole time.

Dosn't really answer your question, I'm afraid, but it might work as a temporary solution.

Matt
 
Old 01-05-2005, 12:03 PM   #5
michael_util
Member
 
Registered: Feb 2004
Posts: 47

Original Poster
Rep: Reputation: 15
Oh ... I like the screen option, because right now the program has a debug mode. Send the process a signal 10 and debug mode starts. Send it signal 10 again and debug mode stops. Debug mode works by printing everything to standard out.

So one I log out and log back in ... there is no more STDOUT for the process.

Thanks for the info.

Michael.
 
  


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
How to know on termination or exit of a perticular process in Unix Pavan mahoorker Linux - Software 1 06-07-2005 07:28 AM
how to run any binary in background - background process tej Linux - Newbie 1 08-16-2004 01:27 AM
Bash Scripting - child process affecting parent process mthaddon Linux - General 1 05-02-2004 02:19 PM
bash script to accept input ONLY until background process completes andrewstr Linux - Software 2 03-17-2004 01:02 PM
child process did not exit kaidoggy Linux - General 0 10-18-2002 10:36 AM


All times are GMT -5. The time now is 08:11 PM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration