Portál AbcLinuxu, 1. května 2025 07:40
Možná teď říkám úplnou blbost, ale nevznikne ti zabitím "parent_processu" zombík? Není čistější řešení prostě přesměrovat tomu původnímu skriptu standardní výstup do /dev/null
?
No, v Pythonu zrovna moc neprogramuju, ale teoreticky by se dalo použít tohle, ne?
ad 3: V příkladě na tom linku to dělaj. Ale nepochopil jsem proč a funguje to v pohodě i bez toho.
fork()
so the parent can exit, this returns control to the
command line or shell invoking your program. This step is required so
that the new process is guaranteed not to be a process group leader. The
next step, setsid()
, fails if you're a process group leader.
setsid()
to become a process group and session group
leader. Since a controlling terminal is associated with a session, and
this new session has not yet acquired a controlling terminal our process
now has no controlling terminal, which is a Good Thing for daemons.
fork()
again so the parent, (the session group leader), can exit.
This means that we, as a non-session group leader, can never regain a
controlling terminal.
chdir("/")
to ensure that our process doesn't keep any directory
in use. Failure to do this could make it so that an administrator
couldn't unmount a filesystem, because it was our current directory.
[Equivalently, we could change to any directory containing files
important to the daemon's operation.]
umask(0)
so that we have complete control over the permissions of
anything we write. We don't know what umask we may have inherited.
[This step is optional]
close()
fds 0, 1, and 2. This releases the standard in, out, and
error we inherited from our parent process. We have no way of knowing
where these fds might have been redirected to. Note that many daemons
use sysconf()
to determine the limit _SC_OPEN_MAX
.
_SC_OPEN_MAX
tells you the maximun open files/process. Then in a
loop, the daemon can close all possible file descriptors. You have to
decide if you need to do this or not. If you think that there might be
file-descriptors open you should close them, since there's a limit on
number of concurrent file descriptors.
je ještě "slušnost" pro daemony udělat chdir("/"), a expliciě zavřít stdin, stdout, a stderr; moc sice nevím proč ale dělá se to..Přechod na / je kvůli tomu, že tento adresář je vždycky k dispozici. Jiný být nemusí, třeba po chrootu. Zavírání standardních I/O zase souvisí s podstatou démona, který by neměl mít žádnou vazbu na terminál. Kdyby se to neudělalo a démon by se spustil z terminálu, pořád hrozí, že tam může chrlit nějaké výstupy nebo odtud něco číst.
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.