Daemon process | Unix Linux Forums | Programming

  Go Back    


Programming Post questions about C, C++, Java, SQL, and other programming languages here.

Daemon process

Programming


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-25-2013
sundaresh sundaresh is offline
Registered User
 
Join Date: Jun 2006
Last Activity: 7 January 2014, 9:04 PM EST
Posts: 14
Thanks: 1
Thanked 0 Times in 0 Posts
Daemon process

I wish to make a process run in the background, but only one instance of it, and not many,
so when the program is loaded, it has to check whether another instance of the same
program is running and if so to exit. How do I do this ?
Sponsored Links
    #2  
Old 09-26-2013
Don Cragun's Avatar
Don Cragun Don Cragun is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 2 September 2014, 5:19 PM EDT
Location: San Jose, CA, USA
Posts: 4,502
Thanks: 177
Thanked 1,511 Times in 1,283 Posts
Quote:
Originally Posted by sundaresh View Post
I wish to make a process run in the background, but only one instance of it, and not many,
so when the program is loaded, it has to check whether another instance of the same
program is running and if so to exit. How do I do this ?
If your system supports the semget() and semop() calls that are included in one of the optional features in the POSIX standards, you can use semget() to get access to a semaphore with a key that is known to your daemon. After getting the semaphore ID you can use semop() with an array of two operations at the start of your program. These operations are:

Code:
sem_num   sem_op   sem_flg
=======   ======   =======
   0         0     IPC_NOWAIT
   0         1     SEM_UNDO

If the semop() call fails with EAGAIN, another daemon is already running and owns the semaphore. If the semop() call succeeds, this daemon owns the semaphore until it terminates. (It doesn't matter if this daemon terminates due to receipt of an uncaught signal or terminates normally by calling exit, the SEM_UNDO flag will release ownership of the semaphore when the daemon terminates.)

If you don't have access to a similar set of semaphore operations, a common approach is to atomically create (with open flags O_CREAT | O_EXCL ) a lock file and write the PID of the daemon that created the lock into the lock file. The daemon should then remove the lock file before it exits. (If the daemon is killed before it can remove the lock file, another daemon won't be able to start until you manually remove the lock file.)
The Following User Says Thank You to Don Cragun For This Useful Post:
sundaresh (09-26-2013)
Sponsored Links
    #3  
Old 09-26-2013
inventsekar inventsekar is offline
Registered User
 
Join Date: Nov 2012
Last Activity: 15 October 2013, 10:50 PM EDT
Posts: 19
Thanks: 1
Thanked 0 Times in 0 Posts
Hi Sundaresh,
I am not sure exactly if this helps...but let me give an idea...
I assume you have a script which you wanted to run on background, only one instance of it.

# inside the script, you will add these few lines to check if the script is already running

Code:
COUNT=`ps -ef SCRIPT_NAME | wc -l`
if [ "$COUNT" -eq 0 ]
then
  # run the script
 /full/path/SCRIPT_NAME &
else
  echo "Already an instance of the script is running"
fi
exit 0


Last edited by Franklin52; 09-26-2013 at 02:11 AM.. Reason: Please use code tags
    #4  
Old 09-26-2013
MadeInGermany MadeInGermany is offline Forum Advisor  
Registered User
 
Join Date: May 2012
Last Activity: 1 September 2014, 5:32 PM EDT
Location: Simplicity
Posts: 1,852
Thanks: 119
Thanked 537 Times in 486 Posts
A correction

Code:
COUNT=`ps -ef | grep -wc '[S]CRIPT_NAME'`

or

Code:
COUNT=`pgrep 'SCRIPT_NAME'
| wc -l`

A comment for Don's pidfile proposal:
a standard pathname is /var/run/SCRIPT_NAME.pid
Sponsored Links
    #5  
Old 09-26-2013
sundaresh sundaresh is offline
Registered User
 
Join Date: Jun 2006
Last Activity: 7 January 2014, 9:04 PM EST
Posts: 14
Thanks: 1
Thanked 0 Times in 0 Posts
Thanks Don, my daemon does use shared memory, but the semaphore seems to be the right way.
I did'nt think of it.
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
How to find if a process a daemon ? vino Programming 7 07-27-2008 09:20 AM
daemon process suresh_rupineni Linux 1 08-21-2006 02:14 AM
zombie daemon process!! rish2005 UNIX for Advanced & Expert Users 1 11-25-2005 08:59 AM
What is a daemon process? Kanu77 Programming 6 03-08-2002 09:47 AM
Daemon process s_chordia Programming 2 01-17-2002 01:09 PM



All times are GMT -4. The time now is 05:24 PM.