Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?


share|improve this question
up vote 0 down vote accepted

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

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.

share|improve this answer
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 '13 at 23:16
...continued: While the "whoami" command returned root, the "logname" worked and returned the originally logged in user. Thanks! – sujitv May 3 '13 at 23:26
@sujitv sudo itself will log that for you, if you ask it to. – Michael Hampton May 4 '13 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 '13 at 11:51

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.