LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 11-30-2004, 10:51 AM   #1
Xa!
LQ Newbie
 
Registered: Mar 2004
Location: .ru/moscow
Distribution: Gentoo
Posts: 17

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


Hello!

I have a simple
Code:
while(statement){}
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
randyding
Member
 
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().
Code:
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++) {
        close(j);
        fd=open("/dev/null",j ? O_WRONLY : O_RDONLY);
        if (fd!=j) break;
    }
    return 3==j;
}    //daemon_init()
 
  


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
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
http://www.burstnet.com/cgi-bin/ads/ad7954a.cgi/3980/RETURN-CODE rverlander LQ Suggestions & Feedback 1 06-07-2002 07:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:47 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