Bash – How to execute a command on login for a system user with no home folder and no personal .bashrc file

bashbashrcsuUbuntuusers

I set these users' home folders through the /etc/passwd file, but when they log in I would like them to activate a python virtualenv. If they had a personal .bashrc file, I'd just include the activate command there, and the directory change as a post-activate hook with virtualenv.

However these users don't have home folders, and don't have personal .bashrc files.

How can I execute a different command on login for each of these users? (Only one command needs to be executed.)

These users are only logged into using su, never SSH. The machine is a VPS running Ubuntu 12.04.3 LTS.

Best Answer

You can add a file to the system's /etc/profiile.d directory that includes a if/then statement for each of the users that you want to run the virtualenv for.

Example

Say I create a file like this, /etc/profile.d/me.sh.

if [ "$USER" == "saml" ]; then
  touch /tmp/samsfile
fi

Make it executable:

$ chmod +x /etc/profile.d/me.sh

And then login as saml, using su:

$ su saml

If we check to see if the temp file was created, it was:

$ ls -l /tmp/samsfile 
-rw-rw-r-- 1 saml saml 0 Oct 14 00:31 /tmp/samsfile
Related Question