Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

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

A script that requires root privileges needs to be executed when a non-root privileged user signs in/out. How do I handle this?

share|improve this question

Use the PAM session hooks module.

share|improve this answer
    
Thats useful. Now, some of operations in which I want to apply should be done before the memory is allocated for user account. And, the operations need to be done after the user account memory is fully deallocated. When does PAM actually trigger the scripts in concern with user account memory allocation/deallocation? One operation I intend to run is sdmem, which cleans the available ram. I need users who sign out to be cleaned up this way. – bambuntu Feb 7 '12 at 1:18
    
Linux doesn't allocate memory "for a user account" or at login time. Memory is allocated as new processes start. If you run this script from pam hooks, it will start before the user is able to run any programs. – poolie Feb 7 '12 at 1:59
    
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Mitch Jul 8 '13 at 11:23

lightdm, the display manager in Ubuntu allows running session setup scripts as root if you add appropriate entry to /etc/lightdm/lightdm.conf file (Note, that this file doesn't exist as of 14.04, you have to manually create it).

Example of how it might look like

[SeatDefaults]
# session-setup-script = Script to run when starting a user session (runs as root)
session-setup-script=/opt/myscripts/print_hello.sh
# session-cleanup-script = Script to run when quitting a user session (runs as root)
session-cleanup-script=/opt/myscripts/print_hello.sh
share|improve this answer
    
Serg, apparently yr answer only applies to gui sessions (running lightdm). Can it be made to work just before power-down independently of any gui session ? I have two use-cases of interest: --- 1) when any user logs out from either gui or an interactive login shell with no gui --- 2) when a box runs headless and is about to power down with no user session (interactive login or gui). --- This might deserve a new question. – Cbhihe May 17 at 9:42

Your Answer

 
discard

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.