LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 07-18-2003, 08:43 AM   #1
registering
Member
 
Registered: Jun 2003
Location: Florida, USA
Distribution: Drake 10.1 Download
Posts: 182

Rep: Reputation: 30
Unhappy Modified inittab, now creates infinite daemons.


Okay, I was very proud to have created an application that makes itself a daemon (background, no related console, etc.).
This has been working great. What I want to do now is to have it automatically startup upon boot-up, and restart whenever it dies, so I put this into my /etc/inittab:

m1:12345:respawn:/nerr/src/start_script

in start_script are these lines:

#!/bin/sh
/nerr/src/remora/remora
/nerr/src/comm/scratch/marisys_comm ysi.cfg
/nerr/src/comm/scratch/marisys_comm cr10.cfg

'remora' and 'marisys_comm' are each daemons.

However when I reboot, I get 'remora' processes all over the place, and 3 marisys_comm processes, one that says <defunct>.

Since each of these applications creates 3 processes, and the first
two die (to remove any associated console, etc.), I imagine the OS thinks the application died, so it respawns it, when in actuality there's a ("sub") process that's the real intended process, still running and doing what it's supposed to.

Any ideas what I'm doing wrong?


My daemon code seems to have been working fine, but here it is for reference, in case I'm doing something stupid (not a first or a last!):

Code:
///////////////////////////////////////////////////////////////////////////////
//
//  Procedure: MakeMyselfaDaemon
//
//  Purpose: This will make our current process a daemon
//
//  Returns: 0 always
//
//  Notes: If forking off children fails, exit() will be called, not return()!
//
//  We currently do NOT chdir, so whatever directory we are launched from will not
//  be able to be unmounted!
//
//////////////////////////////////////////////////////////////////////////////
int MakeMyselfaDaemon()
{
   pid_t    myPID;          // my pocess ID


   // try and create a child then kill ourselves
   if ((myPID = fork()) == -1)
   {
      // we could not fork, do something
      cout << "\r\nOur parent could not create an additional process: fork() failed\r\n";
      exit(1);
   } // end if
   else if (myPID != 0)
   {
      // we are the parent so sleep then die
      // give our child time to execute....
      sleep(2);
      exit (1);
   } // end else if
   else
   {
      // now try to run our child in its own process group without any terminal
      if (setsid() == -1)
      {
         // we could not create our own process group or maybe ourr child cannot be theleader?
	 cout << "\r\nWe could not disassociate from the terminal: setsid() returned -1\r\n";
	 _exit(1);
      } // end if
   } // end else

   // if we're here than we're our parent's child and our parent killed itself

   // now fork again so our child (session group leader) can exit
   if ((myPID = fork()) == -1)
   {
      /* we could not fork, do something */
      cout << "\r\nOur first child could not create an additional process: fork() failed\r\n";
      _exit(1);
   } // end if
   else if (myPID != 0)
   {
      // we are the session group leader so sleep then die
      // give our second child time to execute
      sleep(2);
      _exit(1);
   } // end else if

   // we are now the second child, and no longer a session group leader

   /////////////////////////////////////////////////////////////////////////////////////////
   // NOTE: if we ever want to unmount the directory we were run from, we must chdir here //
   /////////////////////////////////////////////////////////////////////////////////////////

   // make sure we have full write permissions, since we don't know WHO invoked us...
   umask(0);

   // now close all of our std streams
   close(STDIN_FILENO);
   close(STDOUT_FILENO);
   close(STDERR_FILENO);

   return (0);

} // MakeMyselfaDaemon()
Thanks for any tips.
 
Old 07-18-2003, 09:04 AM   #2
registering
Member
 
Registered: Jun 2003
Location: Florida, USA
Distribution: Drake 10.1 Download
Posts: 182

Original Poster
Rep: Reputation: 30
Would I be better off using a cron job to check, rather than inittab? Something like this:

crontab -e

0 * * * * cd /nerr/src/remora/; ./remora -nice 19 > /dev/null 2> /dev/null


My concern is since it's a daemon, would even cron see it's running, even though the current process was forked() from the initial process?
 
  


Reply



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
Infinite Loop ewt3y Programming 3 08-16-2005 09:48 AM
Creates unwanted copies~ civ247 Mandriva 1 10-29-2004 10:47 PM
the infinite - firefox? - help z_pcjr Linux - Software 0 09-29-2004 07:46 PM
infinite loop beginner_84 Programming 5 08-15-2004 02:32 AM
Infinite konsole loading Grunty Slackware 4 06-09-2004 08:11 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:27 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
Open Source Consulting | Domain Registration