Permalink
Browse files

daemon.pl - daemon demo for perl script.

  • Loading branch information...
1 parent b5c6345 commit 8e42c9ae3e9c68bd79ecddd43a1cad33d9f19bec @soarpenguin committed Feb 23, 2014
Showing with 27 additions and 0 deletions.
  1. +27 −0 daemon.pl
View
@@ -0,0 +1,27 @@
+#!/bin/env perl
+
+use POSIX;
+use strict;
+
+sub daemonize {
+ POSIX::setsid or die "setsid: $!";
+ my $pid = fork ();
+ if ($pid < 0) {
+ die "fork: $!";
+ } elsif ($pid) {
+ exit 0;
+ }
+ chdir "/";
+ umask 0;
+ foreach (0 .. (POSIX::sysconf (&POSIX::_SC_OPEN_MAX) || 1024))
+ { POSIX::close $_ }
+ open (STDIN, "</dev/null");
+ open (STDOUT, ">/dev/null");
+ open (STDERR, ">&STDOUT");
+}
+
+&daemonize();
+
+while (1) {
+ sleep 2;
+}

0 comments on commit 8e42c9a

Please sign in to comment.