Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Running a daemon process as another user

by toadi (Chaplain)
on Oct 27, 2004 at 07:39 UTC ( #402917=perlquestion: print w/ replies, xml ) Need Help??
toadi has asked for the wisdom of the Perl Monks concerning the following question:

hello,

I know this is not really a perl question but I don't know where to ask else.

I have written a perl daemon that has to be run as a regular user. Not as root.

I have written a Sysv startup script where I start my script when the system boots. But this happens as root so my perl daemon runs as root.

This is something I don't want.

Any Suggestions????



--
My opinions may have changed,
but not the fact that I am right

2004-10-27 Edited by Arunbear: Changed title from 'daemons, Sysv, etc.', as per Monastery guidelines

Comment on Running a daemon process as another user
Re: Running a daemon process as another user
by Zaxo (Archbishop) on Oct 27, 2004 at 08:00 UTC

    At the top of the script, say

    use POSIX qw/setuid setsid/; BEGIN { setuid scalar getpwnam 'toadi' or die $!; }
    I've imported setsid, too, since you need it to manifest a daemon.

    After Compline,
    Zaxo

      ok this is very strange:
      use POSIX qw/setuid setsid/; BEGIN { setuid scalar getpwnam 'qf3' or die $!; # Fork. my $pidFile = '/some/path/pid'; my $pid = fork; if ($pid) # parent: save PID { open PIDFILE, ">$pidFile" or die "can't open $pidFile: $!\n"; print PIDFILE $pid; close PIDFILE; exit 0; } } print "USER: ". getpwuid($<) . "\n";

      USER will be root, but when I do a ps -ef it does:

      toadi 20510 1 14 04:56 pts/0 00:00:01 /usr/bin/perl /some/ +path/daemon


      --
      My opinions may have changed,
      but not the fact that I am right

        print "USER: ". getpwuid($<) . "\n";

        USER will be root, but when I do a ps -ef it does:

        Sure it is, because   $<   is the real uid (you used to be root).

        Use the effective uid   $>   to display the new identity. See  perlvar.

        Cheers, Sören

Re: Running a daemon process as another user
by chb (Chaplain) on Oct 27, 2004 at 08:01 UTC
    Just use the su command in your startup script to call the perl daemon. Read 'man su' for details.
Re: Running a daemon process as another user
by gaal (Parson) on Oct 27, 2004 at 08:22 UTC
    Either make the script voluntarily drop privileges (set both $> and $< to your target uid when the script starts), or change users before invoking the script, for example with the system su -c.
      su -c worked


      --
      My opinions may have changed,
      but not the fact that I am right

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://402917]
Approved by skx
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (10)
As of 2014-09-19 16:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (143 votes), past polls