Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Running a daemon process as another user

by toadi (Chaplain)
on Oct 27, 2004 at 07:39 UTC ( [id://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

Replies are listed 'Best First'.
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 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

Re: Running a daemon process as another user
by chb (Deacon) 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2024-03-29 14:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found