LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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
 
LinkBack Search this Thread
Old 06-29-2005, 12:22 AM   #1
rajsun
Member
 
Registered: Mar 2005
Posts: 61

Rep: Reputation: 15
Daemon programming.....


Hi All,
Is there any way to daemonize a process without forking in program code. I mean to ask is there any command which will daemonize simple program.

Tahnking u all,

With Regard
Rajsun.
 
Old 06-29-2005, 01:00 AM   #2
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,506

Rep: Reputation: 116Reputation: 116
You could just run it with an ampersand afterwards, to background it:
Code:
$ ./myProgram &
 
Old 06-29-2005, 02:57 AM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5
Posts: 16,086

Rep: Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994
don't forget to nohup it as well, else it'll terminate when you logout ie
$> nohup ./prog &
 
Old 06-29-2005, 03:04 AM   #4
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,506

Rep: Reputation: 116Reputation: 116
Unless you ignore SIGHUP in your program. But your point is well taken. I'd forgotten that from my days of running a MUD.
 
Old 06-29-2005, 06:02 AM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Or did you mean: daemonize inside the program itself?

You cannot do that without forking really. But you can do:
Code:
#include <stdio.h>
#include <unistd.h>

/* ... */

if (daemon(0, 0) < 0) {
    perror(errno, "daemon()");
    return 1;
}
The daemon() function will do the forking properly for you.
 
Old 06-29-2005, 08:56 AM   #6
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,506

Rep: Reputation: 116Reputation: 116
I'm not sure daemon() is ANSI C, but I could be wrong.
 
Old 06-29-2005, 11:37 AM   #7
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Quote:
Originally posted by Matir
I'm not sure daemon() is ANSI C, but I could be wrong.
I'm quite sure it isn't.
But AFAIK fork() isn't either...
 
Old 06-29-2005, 01:19 PM   #8
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,506

Rep: Reputation: 116Reputation: 116
I thought fork() was ANSI C, but I could be wrong.
 
Old 06-29-2005, 06:18 PM   #9
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
IIRC not ANSI C, but fork() is POSIX.
 
Old 06-29-2005, 07:07 PM   #10
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,506

Rep: Reputation: 116Reputation: 116
Ah, I didn't know that. I guess since I try to avoid programming for Win environments, and write all my code to (attempt to be) GNU and POSIX compatible, I've never stumbled across that.
 
Old 07-01-2005, 06:10 AM   #11
64bitprocessor
LQ Newbie
 
Registered: Jul 2005
Location: Bangalore
Distribution: Debian
Posts: 15

Rep: Reputation: 0
HI,
If you are writing a daemon you should follow some strict rules.
1) Create a new process by using fork() and exit the parent process there itself (normally using the exit() call).
2) Change the current directory of the daemon (child process) to /. Some daemon use its own directory as the current directory
3) Close all the file descriptors to stdin, stdout and stderr. A daemon should not write anything or read anything from these devices.
4) The Inputs to the daemons should be read from its configuration files.
5) Out put should be logged in the log files and should not be displayed in the shell.
The daemon() is a library function to do all these things automatically. But still creating a daemon manually is much better.
regards,
64Bit

Last edited by 64bitprocessor; 07-01-2005 at 06:12 AM.
 
Old 07-01-2005, 06:12 AM   #12
64bitprocessor
LQ Newbie
 
Registered: Jul 2005
Location: Bangalore
Distribution: Debian
Posts: 15

Rep: Reputation: 0
HI,
If you are writing a daemon you should follow some strict rules.
1) Create a new process by using fork() and exit the parent process there itself (Normally using the exit()).
2) Change the current directory of the daemon (child process) to /. Some daemon use its own directory as the current directory
3) Close all the file descriptors to stdin, stdout and stderr. A daemon should not write anything or read anything from these devices.
4) The Inputs to the daemons should be read from its configuration files.
5) Out put should be logged in the log files and should not be displayed in the shell.
The daemon() is a library function to do all these things automatically. But still creating a daemon manually is much better.
regards,
64Bit

Last edited by 64bitprocessor; 07-01-2005 at 06:13 AM.
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
which programming language is used to do tcp/ip programming?? gajaykrishnan Linux - Networking 9 12-21-2012 05:16 AM
Daemon programming.... Help rajsun Programming 7 02-01-2006 11:23 PM
Difference between Top Down programming & Bottom up programming minil Programming 1 06-17-2005 02:42 AM
Daemon Server Programming.... HELP rajsun Programming 2 04-05-2005 03:28 AM
What is a daemon? nutshell Linux - General 3 02-26-2002 06:25 AM


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

Main Menu
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