Execute at login

Polytropon freebsd at edvax.de
Tue Sep 27 18:27:32 UTC 2011

On Mon, 26 Sep 2011 20:41:43 +0000 (UTC), Helmut Schneider wrote:
> Hi,
> which options do I have to execute at login?
> I would like to implement something like update-motd [1] without
> actually modifying /etc/motd. The code snippet is
> if [ -d /etc/motd.d ]; then
>   for FILE in /etc/motd.d/*; do
>     [ -x ${FILE} ] && ${FILE}
>   done
> fi
> It should be executed for all users but only at login (regardless if
> she/he logs in via console or ssh). It also should be independent of
> the login shell. Therefore neither /etc/profile nor ~/.profile nor
> ~/.login seem suitable.
> Where can I put that code? The content of /etc/motd.d/ can change
> anytime.

I'm not sure if this works, but maybe something like this
can be an idea to create a comparable solution:

You can (ab)use the "login shell" property of /etc/passwd
to give all users a login shell that is the above script
which _then_ executes the real login shell for the users
(I assume this will be either bash or csh).

See "man 5 passwd" for details.

However, this approach can cause trouble in combination with
chsh. It also doesn't seem to be limited to interactive logins,
so there should be some test in the script to check if the
current shell is in dialog mode

For csh, this can be done by

	if ($?prompt) then
		... interactive shell stuff ...

But again, this does _not_ apply to different login shells.

An idea to compensate this could be to employ login.conf
instead, per the "shell" environmental setting. This seems
to override the shell defined in /etc/passwd (which can be
subject to a chsh call).

See "man 5 login.conf" for details.

The script mentioned above could therefore include the
following steps:

	1. determinate kind of shell:
	   in case of interactive shell, continue

	2. check for motd.d functionality:
	   if /etc/motd.d/ exists and has executable files
	   in it, execute them (basically your script concept)

	3. determine user's dialog shell
	   read /etc/passwd and start the user's dialog shell
	   by exec <shellname>

You can write this in _any_ (shell) script language you want.
I would suggest plain #!/bin/sh syntax.

Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...

More information about the freebsd-questions mailing list