Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 569813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:20:06+00:00 2026-05-13T13:20:06+00:00

Is it possible that I would be able to spawn a detached daemon like

  • 0

Is it possible that I would be able to spawn a detached daemon like process, from a CGI script
that stores read text files in memory, then re-access the memory in the next cgi execution, reading the data using a pipe?

Would most hosting ISP’s, allow detached processes? Are memory pipes fast, and easy to code/work with on a unix/linux system?

Is there a solution, that can be done without using any extra CPAN modules? This is a CGI process, so I want to keep it to a minimum.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:20:07+00:00Added an answer on May 13, 2026 at 1:20 pm

    Say you have a simple resource.cgi:

    #! /usr/bin/perl
    
    use warnings;
    use strict;
    
    use Reader;
    use CGI qw/ :standard /;
    
    print header("text/plain"),
          "Contents:\n",
          Reader::data,
          "-" x 40, "\n";
    

    Its output is

    Content-Type: text/plain; charset=ISO-8859-1
    
    Contents:
    This is a data file
    with some very interesting
    bits.
    ----------------------------------------

    The fun part is in Reader.pm, which begins with familiar-looking boilerplate:

    package Reader;
    
    use warnings;
    use strict;
    
    use Fcntl qw/ :DEFAULT :flock :seek /;
    use POSIX qw/ setsid /;    
    

    Next it defined rendezvous points:

    my $PIDFILE = "/tmp/reader.pid";
    my $DATA    = "/tmp/file.dat";
    my $PIPE    = "/tmp/reader.pipe";
    

    The sub import is called as part of use Module. If the daemon is already running, then there’s nothing to do. Otherwise, we fork off the daemon and write its process ID to $PIDFILE.

    sub import {
      return unless my $fh = take_lock();
    
      my $child = fork;
      die "$0: fork: $!" unless defined $child;
    
      if ($child) {
        print $fh  "$child\n" or die "$0: write $PIDFILE: $!";
        close $fh             or die "$0: close $PIDFILE: $!";
        return;
      }
    
      # daemonize
      close $fh;
      chdir "/";
      open STDIN,  "<", "/dev/null";
      open STDOUT, ">", "/dev/null";
      open STDERR, ">", "/dev/null";
      setsid;
    
      open $fh, "<", $DATA or die;
      undef $/;
      my $data = <$fh>;
      close $fh;
    
      while (1) {
        open my $fh, ">", $PIPE or die;
        print $fh $data         or die;
        close $fh;
      }
    }
    

    Every client needs to wait its turn getting a lock on $PIDFILE. Once we have the lock, we then check that the process identified is still running and create the named pipe if necessary.

    sub take_lock {
      sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT or die "$0: open $PIDFILE: $!";
      flock $fh => LOCK_EX                       or die "$0: flock $PIDFILE: $!";
    
      my $pid = <$fh>;
    
      if (defined $pid) {
        chomp $pid;
    
        if (kill 0 => $pid) {
          close $fh;
          return;
        }
      }
      else {
        die "$0: readline $PIDFILE: $!" if $!;
      }
    
      sysseek  $fh, 0, SEEK_SET or die "$0: sysseek $PIDFILE: $!";
      truncate $fh, 0           or die "$0: truncate $PIDFILE: $!";
    
      unless (-p $PIPE) {
        system("mknod", $PIPE, "p") == 0
                                or die "$0: mknod exited " . ($? >> 8);
      }
    
      $fh;
    }
    

    Finally, reading the pipe is trivial:

    sub data {
      open my $fh, "<", $DATA or die "$0: open $DATA: $!";
      local $/;
      scalar <$fh>;
    }
    

    Don’t forget to return a true value from the module:

    1;
    

    You’ll note that operations can still fail in the daemon. For your sanity, you’ll want to log events somehow rather than choking silently.

    As to whether hosts will permit long-running processes, that will vary from provider to provider, but even if your daemon is killed off from time to time, the code above will restart it on demand.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to be able to spawn a process in python and have
I have a string that I would like to print . Is it possible
Would it be possible to write a class that is virtually indistinguishable from an
Question So I've recently came up with some new possible projects that would have
I'm looking for some kind of a resource (website) that would list all possible
Here is my question, Would it be possible, knowing that classic asp support server-side
Is it possible that Microsoft will be able to make F# programs, either at
Is that possible to switch an Open Source Project license from GPL to LGPL
I would like to write an application, for my own interest, that graphically visualizes
How is it possible that .NET is finding the wrong MyType in this scenario?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.