Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 11-30-2004, 10:51 AM   #1
LQ Newbie
Registered: Mar 2004
Location: .ru/moscow
Distribution: Gentoo
Posts: 17

Rep: Reputation: 0
Start program in daemon mode from cgi-bin


I have a simple
program written on c. I start it from http://mysite/cgi-bin/simple_program and it works fine until I close browser. When I click close button my simple_program terminates too.

How to start it in a such way, so it continues working after closing browser?
Old 11-30-2004, 11:00 PM   #2
Registered: May 2004
Posts: 552

Rep: Reputation: 31
This is the code I've used in a CGI to "background" the program so that does not happen. Call this function first thing in your CGI program main().
int daemon_init(void) {
    pid_t pid;
    int j, fd;

    pid=fork(); //child=0, parent=pid of child or -1 on error
    if (pid<0) return 0;    //-1 failed
    if (pid>0) exit(0);     //pid of child, parent terminates
    //pid is 0, child continues
    //setsid();   //become session leader
    chdir("/"); //change working directory
    umask(0);   //clear our file mode creation mask
    for (j=0; j<3; j++) {
        fd=open("/dev/null",j ? O_WRONLY : O_RDONLY);
        if (fd!=j) break;
    return 3==j;
}    //daemon_init()


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
Start a daemon/program at boot SchwipSchwap Linux - Newbie 15 01-24-2006 01:58 AM
How to start my GUI program by a daemon? oldh Linux - Software 3 08-05-2005 02:04 PM
Run program in daemon mode gubak Linux - General 3 06-20-2005 01:23 AM
start daemon from cgi-bin via web-interface Xa! Programming 1 07-26-2004 03:08 PM rverlander LQ Suggestions & Feedback 1 06-07-2002 07:35 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:32 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration