Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well - it's better if you could state what this daemon is supposed to do. As there are specialized frameworks/libraries for various tasks.</p> <p>For simplest daemon that does nothing, just exists, you can easily do this:</p> <pre><code>#!/usr/bin/perl use strict; use warnings; use Carp; use POSIX qw( setsid ); daemonize(); do_your_daemon_stuff(); exit; sub daemonize { chdir '/' or croak "Can't chdir to /: $!"; open STDIN, '/dev/null' or croak "Can't read /dev/null: $!"; open STDOUT, '&gt;/dev/null' or croak "Can't write to /dev/null: $!"; defined(my $pid = fork) or croak "Can't fork: $!"; exit if $pid; setsid or croak "Can't start a new session: $!"; open STDERR, '&gt;&amp;STDOUT' or croak "Can't dup stdout: $!"; } </code></pre> <p>sub daemonize() was liften from <a href="http://perldoc.perl.org/perlipc.html#Complete-Dissociation-of-Child-from-Parent" rel="nofollow noreferrer">perldoc perlipc</a> (with minor change).</p> <p>That's all - the code now properly daemonizes, and can do anything you want.</p> <p>I just read your edit, that you want TCP server.</p> <p>OK. Here is simplistic code:</p> <pre><code>#!/usr/bin/perl use strict; use warnings; use Carp; use POSIX qw( setsid ); use IO::Socket; my $server_port = get_server_port(); daemonize(); handle_connections( $server_port ); exit; sub daemonize { chdir '/' or croak "Can't chdir to /: $!"; open STDIN, '/dev/null' or croak "Can't read /dev/null: $!"; open STDOUT, '&gt;/dev/null' or croak "Can't write to /dev/null: $!"; defined(my $pid = fork) or croak "Can't fork: $!"; exit if $pid; setsid or croak "Can't start a new session: $!"; open STDERR, '&gt;&amp;STDOUT' or croak "Can't dup stdout: $!"; } sub get_server_port { my $server = IO::Socket::INET-&gt;new( 'Proto' =&gt; 'tcp', 'LocalPort' =&gt; 31236, 'Listen' =&gt; SOMAXCONN, 'Reuse' =&gt; 1, ); die "can't setup server" unless $server; return $server; } sub handle_connections { my $port = shift; my $handled = 0; while ( my $client = $port-&gt;accept() ) { $handled++; print $client "Hi, you're client #$handled\n"; chomp ( my $input = &lt;$client&gt; ); my $output = reverse $input; print $client $output, "\n"; print $client "Bye, bye.\n"; close $client; } return; } </code></pre> <p>Just remember that this is blocking tcp server, so it will be able to handle 1 connection at the time. If you want more than 1 - it becomes more complex, and you have to ask yourself if you prefer multithreading (or multi-processing), or you prefer single-process, event based server.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload