NetBSD-Users archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: Daemonizing processes in NetBSD



If you have perl available the following should do what you
need. Wouldn't be too difficult to write something similar
in C as well.

#!/usr/bin/env perl

use strict;
use warnings;

use POSIX qw//;

sub daemonize {
	defined (my $pid = fork()) or die "Can't fork: $!";
	exit if $pid;
	defined (my $ppid = fork()) or die "Can't fork: $!";
	exit if $ppid;
	chdir "/" or die "Can't chdir '/': $!";
	POSIX::setsid or die "Can't start new session: $!";
}

daemonize();

eval {
	my @cmd = @ARGV;
	exec @cmd;
};

warn $@ if $@;

exit;

Edgar


Home | Main Index | Thread Index | Old Index