LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   script after login (https://www.linuxquestions.org/questions/linux-newbie-8/script-after-login-671727/)

Jurrian 09-23-2008 01:56 AM

script after login
 
Hi all,

I want to run a script to make some system variables available for users that login, since when I only have them in my rcS, they are not available after a user logs in.

Since I work with embedded Linux, I don have a specific Distro installed, but it is like redhat.

I searched the forums and found a solution to change the .bashrc file, but it does't exist in my home directory. So I created a new one, but it's not really working. This is how it is looking like:


Code:

#cat .bashrc
#.bashrc
#added to make exports from rcS available for user root.

export DISPLAY=:0

After searching some more I also found the /etc/profile to be a possible solution, so i added:

Code:

export DISPLAY=:0
This also doesn't work.

if I after login manually enter export DISPLAY=:0 my Xserver does work.

If it is not entered I get the following error when I want to use my Xserver:

Code:

# xclock
Error: Can't open display:

My rcS file looks the following:

Code:

# cat rcS
#!/bin/sh
#init script. Based on scripts in timesys-init package.

NEED_NETWORKING=
DEVDIR="/dev"

mkdev() {
    echo "--${DEVDIR}/$1"
    [ -e ${DEVDIR}/$1 ] && return 0
    mkdir -p ${DEVDIR}/$(dirname $1)
    mknod ${DEVDIR}/$1 $2 $3 $4
}

echo -n "remounting / read-write ... "
/bin/mount -o remount,rw /dev/root /
echo "done."

echo -n "mounting /proc ... "
/bin/mount -t proc proc /proc
echo "done."

echo -n "mounting /sys ... "
/bin/mount -t sysfs sys /sys
echo "done."


echo -n "cleaning out /tmp ... "
rm -rf /tmp
mkdir -m 1777 /tmp
echo "done."

echo "creating device nodes ..."


cd ${DEVDIR}

[ ! -e ${DEVDIR}/pts ] && mkdir ${DEVDIR}/pts
chmod 655 ${DEVDIR}/pts

/sbin/mdev -s # ask busybox to populate the rest of the devs

echo "done."

echo -n "mounting /dev/pts ... "
/bin/mount -t devpts devpts /dev/pts
echo "done."


if [ ! -e /root/.gpe/gpe-mini-browser/bookmarks ]; then
  mkdir -p /root/.gpe/gpe-mini-browser
  cp /usr/share/gpe-mini-browser/bookmarks  \
    /root/.gpe/gpe-mini-browser/bookmarks
fi

echo -n "bringing up networking ... "
ifconfig lo 127.0.0.1 up
if [ "$NEED_NETWORKING" ]; then
  ifconfig eth0 up
  udhcpc eth0 &
fi
echo "done."

echo -n "starting syslogd ... "
/sbin/syslogd
echo "done."

echo -n "starting klogd ... "
/sbin/klogd
echo "done."

echo -n "starting telnetd ... "
/usr/sbin/telnetd
echo "done."

#echo -n "starting httpd ... "
#start-stop-daemon --start --exec /usr/sbin/thttpd -- -C /etc/thttpd.conf -c "**.cgi"
#echo "done."

#echo -n "starting dbus ... "
#/etc/rc.d/init.d/messagebus start
#echo "done."

echo "mounting removable media ... "
if [ -e /dev/sda1 ]; then
  mount /dev/sda1 /media/usb && echo "/dev/sda1 mounted at /media/usb"
elif [ -e /dev/sda ]; then
  mount /dev/sda /media/usb && echo "/dev/sda mounted at /media/usb"
fi

if [ -e /dev/mmcblk0p1 ]; then
  mount /dev/mmcblk0p1 /media/mmc && echo "/dev/mmcblk0p1 mounted at /media/mmc"
fi
echo "done."

echo -n "unmuting sound device ... "
aumix -w 100 -v 100 -W 50
echo "done."

# gtk %postin fails so we do it again here...

update-gdk-pixbuf-loaders $MACHTYPE
update-gtk-immodules $MACHTYPE

export HOME=/root

#
#
# demo specific stuff

# first, calibrate
#if [[ `dmesg | grep touchscreen | grep input0` ]]
#then
#        echo "Touchscreen Device is /dev/event0"
#        export TSLIB_TSDEVICE=/dev/event0
#        ts_calibrate
#elif [[ `dmesg | grep Touchscreen | grep input1` ]]
#then
#        echo "Touchscreen Device is /dev/event1"
#        export TSLIB_TSDEVICE=/dev/event1
#        ts_calibrate
#else
#        echo "Touchscreen Device Not Found"
#fi
export TSLIB_TSDEVICE=/dev/event0

# then start the X server

Xfbdev -br -pn -mouse tslib -dpi 100 -rgba bgr -screen 640x480@90 :0 -noreset &
#  -fp /usr/share/X11/fonts/misc &
export DISPLAY=:0

#matchbox-session &

# Start simple ftpd
/etc/init.d/simple-ftpd start &

I hope anyone knows a solution,

Feel free to ask more question is my question was not specific enough,

Thanks in advance,

Jurrian Dubbeldam

arckane 09-23-2008 07:50 AM

Assuming it runs through rc/init scripts at bootime, create a script in /etc/init.d and ln -s it in /etc/rc3.d/.

OK, that's not overly indepth but I'm on a time limit, sorry :|

Jurrian 09-23-2008 08:29 AM

Quote:

Originally Posted by arckane (Post 3289214)
Assuming it runs through rc/init scripts at bootime, create a script in /etc/init.d and ln -s it in /etc/rc3.d/.

I created a script in /etc/init.d, however I dont have a dir /etc/rc3.d. I do have rc.d, but I dont think it's the same dir. Can I use rc.d or should I create rc3.d?

And what do you mean with ln -s in rc3.d? Should I just go to rc3.d (or rc.d) and enter ln -s? Or am I missing something?

Jurrian 09-30-2008 01:00 AM

Still can't find a solution to my problem, anyone here that can help me??

i92guboj 09-30-2008 01:06 AM

The .bashrc solution entirely depends on two things:

1.- the shell you use
2.- the way you login

1) bashrc is an init file for bash. If you use another shell (regular sh, csh, ksm, zsh...) then bashrc will not be valid. Check the man page for your shell to get more info about the rc files for that shell. Usually you can find the shell you are running by doing this in command line:

Code:

echo $SHELL
2) depending on how do you login, the rc files that are sourced are not always the same. This is true at least for bash. For example, bash reads ~/.bashrc when you open it in non-login interactive mode, but it doesn't read that file when it is a login shell. So, the file you want to put that into is different depending on how you are starting your shell. More info on the bash man page:

Code:

man bash

Jurrian 09-30-2008 02:16 AM

if I execute the command $SHELL I get the following result:

Code:

# echo $SHELL
/bin/bash

searching the man bash page informs me that bash uses the following loginscripts if they exist:

Code:

Invoked as an interactive login shell, or with --login

When Bash is invoked as an interactive login shell, or as a non-interactive shell with the --login
option, it first reads and executes commands from the file /etc/profile, if that file exists. After
reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and
reads and executes commands from the first one that exists and is readable. The --noprofile option
may be used when the shell is started to inhibit this behavior.

When a login shell exits, Bash reads and executes commands from the file ~/.bash_logout, if it
exists.

I don't have a .bash_login, or bash_profile. I do have etc/profile and .profile:

Code:

# cat /etc/profile
# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc

pathmunge () {
        if ! echo $PATH | /bin/egrep -q "(^|:)$1($|:)" ; then
          if [ "$2" = "after" ] ; then
              PATH=$PATH:$1
          else
              PATH=$1:$PATH
          fi
        fi
}

# ksh workaround
if [ -z "$EUID" -a -x /usr/bin/id ]; then
        EUID=`id -u`
        UID=`id -ru`
fi

# Path manipulation
if [ "$EUID" = "0" ]; then
        pathmunge /sbin
        pathmunge /usr/sbin
        pathmunge /usr/local/sbin
fi

# No core files by default
ulimit -S -c 0 > /dev/null 2>&1

if [ -x /usr/bin/id ]; then
        USER="`id -un`"
        LOGNAME=$USER
        MAIL="/var/spool/mail/$USER"
fi

HOSTNAME=`/bin/hostname`
HISTSIZE=1000

if [ -z "$INPUTRC" -a ! -f "$HOME/.inputrc" ]; then
    INPUTRC=/etc/inputrc
fi

export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC

for i in /etc/profile.d/*.sh ; do
    if [ -r "$i" ]; then
        . $i
    fi
done

unset i
unset pathmunge

#set DISPLAY variable correct
export DISPLAY=":0"

Code:

# cat .profile
#make display variable visible
export DISPLAY=:0

But still when I login I don't have a display variable...

Anyone with a suggestion?

Thanx,

Jurrian

Mr. C. 09-30-2008 02:30 AM

The bash startup file ~/.profile is sourced after first attempting either ~/.bash_profile or ~/.bash_login. See INVOCATION in man bash. This is useful to read, because as i92guboj mentioned, which file is source depends on whether the shell is a) interactive, b) a login shell.

You can create a .profile or .bash_profile, but your don't need both.

Jurrian 09-30-2008 02:38 AM

I have read the man page, but i guess i am understanding something wrong.

This is how I think it works:

I boot my target, which starts linux after the bootloader did it's work. When linux is booted I can login, so I login on root (no other account available yet), and bash starts.

So I guess bash is invoked as an interactive login shell. so it would read /etc/profile and ~/.profile, since I don't have ~/bash_login and ~/bash_profile.

So I created a file ~/.profile, but it still is not working..

Code from ~/.profile:

Code:

# cat ~/.profile
#make display variable visible
export DISPLAY=:0

Thans for all replies:)

Jurrian

Jurrian 09-30-2008 05:19 AM

Perhaps the problem is that I run busybox instead of bash??

But when I enter echo $SHELL the reply is /bin/bash. So I guess I have to use bash??

Thanx,

Jurrian

Mr. C. 09-30-2008 10:49 AM

Login as root, and create 4 startup files for root, by running the following commands (caution, they will replace root's startup files):

Code:

echo "echo BASH_PROFILE" >| ~/.bash_profile
echo "echo BASH_LOGIN" >| ~/.bash_login
echo "echo PROFILE" >| ~/.profile,
echo "echo BASHRC" >| ~/.bashrc

Log out and log in again. What is output?

The SHELL environment indicates your preferred choice of shells to use for applications that spawn a shell, if such applications interrogate $SHELL. It does not indicate what SHELL you must use.

i92guboj 10-01-2008 07:42 AM

Note also that order is relevant. Let me explain. If you run on a login interactive mode, bash reads /etc/profile, and after that, it looks for three files:

~/.bash_profile
~/.bash_login
~/.profile

In that very same order, and ONLY one is sourced if found: the first one. That means that, if there's a ~/.bash_profile, bash will stop looking, even if that file is empty, so ~/.bash_login and ~/.profile will be ignored. If there's no ~/.bash_profile but there's a ~/.bash_login, it will be read, and ~/.profile will be ignored if it exists. ç



About busybox, I never used it as my main shell, and I can't seem to find any real documents about the sh functionality. I'd just use a regular shell like bash.

Jurrian 10-02-2008 12:40 AM

Mr. C. This is my output:

Code:


192.168.0.253 login: root
Password:


BusyBox v1.9.1 (2008-04-11 15:22:42 EDT) built-in shell (ash)
Enter 'help' for a list of built-in commands.

#

When I look to the created scripts they do have the commands they should. (echo and then the file name)

Could you tell me what the >| operator exactly does? I understand that with echo it will be placed in the file name after >|, or is that the only thing the >| operator specifies, in which file it has to be placed?

i92guboj, I want to keep my linux as small as possible, so I use busybox instead of a complete bash.

Thanx in advance,

Jurrian

Mr. C. 10-02-2008 12:47 AM

Notice your shell :

Code:

BusyBox v1.9.1 (2008-04-11 15:22:42 EDT) built-in shell (ash)
See man ash, searching for Invocation. Ash uses /etc/profile and .profile if they exist, and the environment variable ENV, but read the man page section for details.

The >| operator is redirect with overwrite.

Mr. C. 10-02-2008 01:12 AM

Since you created the .profile, and upon login you did not see PROFILE, either the .profile was not created in root's home directory, or the ash shell is not a login shell. Look at the output of ps, searching for ash. Does the ash command name in the ps output begin with a dash character (eg. -ash)? This would indicate the ash shell was a login shell, and /etc/profile and $HOME/.profile would be used. Otherwise, they are not. Again, see Invocation.

Jurrian 10-02-2008 01:18 AM

Hi,

At least that makes me feel coming more in the correct direction.

After reading the invocation from man ash,

I found out it uses /etc/profile or /etc/.profile, and with the ENV variable you can set a path to a new file.

So I created a ENV=$HOME/.profile in the /etc/profile, and I exported the DISPLAY variable in ~/.profile.

However, still no DISPLAY variable when I echo $DISPLAY...

My /etc/profile script:

Code:

# cat /etc/profile
# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc



pathmunge () {
        if ! echo $PATH | /bin/egrep -q "(^|:)$1($|:)" ; then
          if [ "$2" = "after" ] ; then
              PATH=$PATH:$1
          else
              PATH=$1:$PATH
          fi
        fi
}

# ksh workaround
if [ -z "$EUID" -a -x /usr/bin/id ]; then
        EUID=`id -u`
        UID=`id -ru`
fi

# Path manipulation
if [ "$EUID" = "0" ]; then
        pathmunge /sbin
        pathmunge /usr/sbin
        pathmunge /usr/local/sbin
fi

# No core files by default
ulimit -S -c 0 > /dev/null 2>&1

if [ -x /usr/bin/id ]; then
        USER="`id -un`"
        LOGNAME=$USER
        MAIL="/var/spool/mail/$USER"
fi

HOSTNAME=`/bin/hostname`
HISTSIZE=1000

if [ -z "$INPUTRC" -a ! -f "$HOME/.inputrc" ]; then
    INPUTRC=/etc/inputrc
fi

export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC

for i in /etc/profile.d/*.sh ; do
    if [ -r "$i" ]; then
        . $i
    fi
done

unset i
unset pathmunge

ENV=$HOME/.profile;
export ENV

And my ~/.profile script:

Code:


# cat ~/.profile
export DISPLAY=:0

What am I doing wrong now? And why does the $SHELL variable indicates I'm using bash. while I'm actually using ash?

Thanx in advance,

Jurrian


All times are GMT -5. The time now is 02:09 AM.