0

I have created an init.d script to control a process as a service, in RHEL 6.x. In the script, I am using /sbin/runuser to start the process as an unprivileged user. This part of the script is working fine.

The script is operated as sudo service scriptname operation

example, sudo service httpd restart

Additionally, I want to logout the user who ran the above command. In normal bash scripts, I have used variously $USER, $SUDO_USER and $logname to get the user running the script. The same does not seem to work when the script is run using the "service" executable.

What am I missing? How can I get the name of the user running the service command inside the script being run by the service command?

Thanks.

1 Answer 1

0

service runs a System V init script in as predictable environment as possible, removing most environment variables and with current working directory set to /.

LANG, TERM
The only environment variables passed to the init scripts.

I don't really get the point of what you want to achieve but you might want to try the whoami or logname commands.

4
  • There are many users on my system who are authorized to run the "service", say the pingpong service. I would like a log of who executed the service by writing the same out to a file (for example, User: $USER executed service $SERVICE_NAME with options $OPTIONS).
    – sujitv
    May 3, 2013 at 23:16
  • ...continued: While the "whoami" command returned root, the "logname" worked and returned the originally logged in user. Thanks!
    – sujitv
    May 3, 2013 at 23:26
  • 1
    @sujitv sudo itself will log that for you, if you ask it to. May 4, 2013 at 0:50
  • @MichaelHampton - I understand that sudo will log both the user & user requested ops. The reasons I went with this additional logging approach was: (a) This is intended for a limited set of application components that will be deployed as services - and hence I want to keep the administration of the log file for these separate and accessible to the application admins; (b) some of the service operations will be accessible to non-privileged users too.
    – sujitv
    May 5, 2013 at 11:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .