[prog] check if script parent process is "init"

John Clarke johnc+linuxchix at kirriwa.net
Thu Apr 15 20:26:32 EST 2004


On Wed, Apr 14, 2004 at 06:33:14 +0200, Wolfgang Petzold wrote:

> The init process usually runs scripts in /etc/init.d/ (or a similar 
> location) by calling symlinks in "runlevel directories", /etc/rc?.d/ in 

Only if your distro uses SysV init.  Slackware used not to do it that
way; I haven't used it for a very long time (Slackware 1.1 in about
1993 IIRC) so it might have changed since then.

> The scripts themselves can then tell by the name they were called with 
> if they are run automatically or manually. 

No, they can't.  If they're called as /etc/init.d/rc?.d/[SK]* then
they're *probably* being run by init, but you can't guarantee that.
What's to stop me running them manually by that name?  Nothing at all.

$PPID should always be set, but it won't be 1.  In the SysVInit
scripts, init runs /etc/rc.d/rc which in turn runs the various startup
scripts.  Using runlevel 3 as an example, /etc/inittab has one line like
this for each runlevel:

    l3:3:wait:/etc/rc.d/rc 3

/etc/rc.d/rc contains essentially this loop (cutting out all the
irrelevant stuff):

    # Now run the START scripts.
    for i in /etc/rc$runlevel.d/S*; do
        $i start
    done

So when the scripts are run, they'll have $PPID set to the pid of the
shell which is running /etc/rc.d/rc.


Cheers,

John
-- 
> All programs evolve until they can send email.
Except Microsoft Exchange. - Art Hagen vs. Rick Letts in a.s.r


More information about the Programming mailing list