Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

The problem: I'm sharing a directory with my wife. I've placed us both in a 'shared' group and the directory belongs to the 'shared' group as well. Whenever one of us creates a file, this file belongs to user:user, instead of user:shared...

The solution: I can do sudo chown, but my wife can't. So, I want to run a script when I logout of the session.

If I understand correctly, the startup scripts go in /etc/init.d/ and the runlevel scripts go /etc/rc0.d/ where 0 is the runlevel (0-6).

Questions: Do the runlevel scripts execute only on exit/logout? Do these depend on the user, that is, I'd like to run it only for my user (not so important in this case, mind)? Which runlevel should I choose (my guess is 5)? Should I place the script somewhere else?

Also, I imagine that the script will be run by root, so there's no need for sudo within the script, is that correct?

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

I believe you can use setgid bit?

From man 2 chmod:

S_ISGID (02000) set-group-ID (set process effective group ID on execve(2); mandatory locking, as described in fcntl(2); take a new file's group from parent directory, as described in chown(2) and mkdir(2))

Runlevel scripts are executed only on runlevel changes.

See also this thread: http://superuser.com/questions/65460/create-a-logoff-script-task-for-linux

share|improve this answer
1  
Wikipedia article. This is exactly what I need. Thanks. –  GUI Junkie Mar 19 '12 at 11:15
    
Tested and it works. Perfect. –  GUI Junkie Mar 19 '12 at 18:49
add comment
  • Do the runlevel scripts execute only on exit/logout?

Those in /etc/init.d only on startup/shutdown, not while switching user/logout/login.

  • Do these depend on the user, that is, I'd like to run it only for my user (not so important in this case, mind)?

Since they run before login: No, they don't depend on the user.

  • Which runlevel should I choose (my guess is 5)?

Ubuntu uses normally 2 as standard runlevel. You can test it with

runlevel

in the shell.

  • Should I place the script somewhere else? according to brownian, you could use ~/.logout, but a better solution is, to produce the files with a proper Group-ID. This can be done with starting the editor with sg.

    sg share gedit

sg acts similar to su - switch user/switch group. Maybe you can put a starter into your panels, for easy access. Brownians solution for the whole directory looks even better.

  • Also, I imagine that the script will be run by root, so there's no need for sudo within the script, is that correct?

That's correct.

share|improve this answer
    
runlevel returns 2. I was thinking of reforming this question so that Brownian's answer makes sense and creating another question for the logout script, as there is no easy gooooogle answer available. –  GUI Junkie Mar 19 '12 at 12:51
add comment

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.