Re: background w/out fork?
by dave_the_m (Monsignor) on Jun 30, 2004 at 21:17 UTC
|
/usr/local/sbin/relaydelay.pl $CONFIG 2>&1 > /var/log/relaydelay.log &
That should be
/usr/local/sbin/relaydelay.pl $CONFIG > /var/log/relaydelay.log 2>&1 &
And yes, that should give you a reasonable approximation
of running as a daemon, although there are things like
it's still part of a process group, so will still get HUP signals from its controlling tty etc.
Dave. | [reply] [d/l] [select] |
Re: background w/out fork?
by BUU (Prior) on Jun 30, 2004 at 21:16 UTC
|
Why would you need to rewrite the application? Just prepend "fork and exit".
fork and exit;
my $old_code;
(Actually theres some other stuff you should theoretically do, like setsid or something from posix, but none of that should involve rewriting your script. Just prepend it.) | [reply] [d/l] |
|
|
use POSIX qw(setsid);
defined(my $pid = fork) or die "Can't fork: $!";
exit 0 if $pid;
chdir '/' or die "Can't chdir to /: $!";
umask 0;
setsid() or die "Can't start a new session: $!";
print "Started $0 daemon OK\n\n";
# now finish of by redirecting I/O stuff
# we want to ignore STDIN
open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
# send STDOUT, STDERR wherever...
open STDOUT, '>/dev/null' or die "Can't write to /dev/null: $!";
open STDERR, '>/dev/null' or die "Can't write to /dev/null: $!";
| [reply] [d/l] |
Re: background w/out fork?
by sgifford (Prior) on Jun 30, 2004 at 21:27 UTC
|
It should work when the system boots, but if a user restarts it by hand from their terminal, it will end up as part of their session (and their terminal), and so their shell may kill it when they log out.
Personally, I would run the script under daemontools, but BUU's right that it shouldn't be too hard to wrap your code. You can probably even do it in a seperate file:
#!/usr/bin/perl
use POSIX qw(setsid);
fork and exit;
fork and exit;
chdir("/");
setsid;
exec @ARGV
or die "Couldn't exec @ARGV: $!\n";
(there may be other stuff; see a copy of Stevens)
If you call this script daemon.pl, you'd just put in your init.d script:
/usr/local/sbin/daemon.pl \
/usr/local/sbin/relaydelay.pl $CONFIG \
>/var/log/relaydelay.log 2>&1
Update: As dave_the_m points out, the order of redirections is important, and I've fixed it here.
| [reply] [d/l] [select] |
|
|
i'm being cautiously lazy. the script in question is some funky looking Milter interface ... i don't want to start hacking and wrapping ...
| [reply] |
|
|
By running it from init.d and using shell commands, you're already doing some hacking and wrapping. It's just a matter of degree. :)
| [reply] |
|
|
Re: background w/out fork?
by hardburn (Abbot) on Jun 30, 2004 at 21:15 UTC
|
Is your aim to get rid of forking completely, or just to get it out of your code? Because the shell command still forks. That's just the way Unix works.
----
send money to your kernel via the boot loader.. This and more wisdom available from Markov Hardburn.
| [reply] |
|
|
the goal is to detach from the tty ... if the job needs to be invoked via command line, it's still attached.
| [reply] |
|
|
nohup /usr/local/sbin/relaydelay.pl $CONFIG > /var/log/relaydelay.l
+og 2>&1 &
Update: Fix Typoes. It's been a long month....
----
I Go Back to Sleep, Now.
OGB
| [reply] [d/l] |
|
|
|
|
If you want to detach from the tty, and from session group, then use Proc::Daemon. It handles all the details of the double fork, creating new session group, and closing all filehandles. You will need to reopen stderr or stdout after the process becomes a daemon.
| [reply] |