LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Run shell script automatically after Login. (https://www.linuxquestions.org/questions/linux-newbie-8/run-shell-script-automatically-after-login-659485/)

hnshashi 07-31-2008 07:11 AM

Run shell script automatically after Login.
 
Hi all,
I want run shelll script myScript.sh automaticaly after login.
plz help me how can do in Redhat 7.2

d2490n 07-31-2008 07:53 AM

Hi,
Im not sure about RedHat but in most distors there will be a file called rc.local (/etc/rc.local) and you can add anything you want to it, such as a path to a script or a list of commands.

Hope that helps

owenjh 07-31-2008 09:05 AM

Try looking for a .bashrc file in your home directory

That should work, it will run every time a terminal is opened.


- Owen.

arizonagroovejet 07-31-2008 11:53 AM

Quote:

Originally Posted by d2490n (Post 3232043)
Hi,
Im not sure about RedHat but in most distors there will be a file called rc.local (/etc/rc.local) and you can add anything you want to it, such as a path to a script or a list of commands.

No. That would run the command at boot not when the user logs in.

If you want to run a script after you log in then you can try the .bashrc method as someone else mentioned, or if you're using a graphical desktop environment such as KDE or Gnome you can run it when they start up. I don't know how to do it in Gnome off the top of my head but for KDE look at my post in this thread. That way it will run just once, not every time you open a terminal. Which method you want to use will depend on what the script actually does.

I assume you're aware that RedHat 7.2 is really, really old and way past the end of it's support life cycle.

sunils1973 08-01-2008 12:30 AM

The directory /etc/init.d contains scripts which are to be loaded at boot time (/etc/rc.d/init.d in redhat)
But we have to put a link in the directories rc0.d,rc1.d,rc2.d and so on to run in the corresponding run levels

These directories include filenames such as S07filename like that
S indicates start the daemon and K indicates kill

you can put the script in /etc/init.d/ directory and make a link in the corresponding run level directoreies.

onebuck 08-01-2008 12:58 AM

Hi,

You should setup a .bashrc & .bash_profile for your user account;

Code:

sample .bashrc;
:~$ cat .bashrc

#.bashrc
#08-30-06 12:20 gws copied loki:/root
#
#06-27-07 13:06 gws added from odin for willi
#
#06-27-07 13:14 gws added from oden:root to /home/gws/.bashrc

# Add bin to path

export PATH="$PATH:/sbin:/usr/sbin:$HOME/bin"

#export PATH="$PATH:$HOME/bin"

# Dynamic resizing
shopt -s checkwinsize

# Custom prompt
#PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

#08-29-06 11:40 gws

if [ `id -un` = root ]; then
  PS1='\[\033[1;31m\]\h:\w\$\[\033[0m\] '
 else
  PS1='\[\033[1;32m\]\h:\w\$\[\033[0m\] '
fi

#
# Add color
eval `dircolors -b`

# User defined aliases
alias cls='clear'
alias clls='clear; ls'
alias ll='ls -l'
alias lsa='ls -A'
alias lsg='ls | grep'
alias lsp='ls -1 /var/log/packages/ > package-list'
alias na='nano'
alias web='links -g -download-dir ~/ www.google.com'

#08-29-06 11:50 gws

#To clean up and cover your tracks once you log off
#Depending on your version of BASH, you might have to use
# the other form of this command
  trap "rm -f ~$LOGNAME/.bash_history" 0

#The older KSH-style form
#  trap 0 rm -f ~$LOGNAME/.bash_history

Code:

sample .bash_profile;

~$ cat .bash_profile
# .bash_profile
#08-30-06 12:21 gws copied loki:/root
#06-27-07 13:10 gws copied from odin:root for willi
#06-27-07 13:17 gws copied from odin:root for /home/gws
#
# Source .bashrc
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

The .bashrc is very useful!

unSpawn 08-01-2008 02:30 AM

Actually it depends, and the OP isn't clear about that nor has he returned to elaborate. It should be clear that the /etc/init.d and /etc/rc.local parts of the SysV initscript structure have nothing to do with running scripts on user login. The .bashrc option, first mentioned by owenjh does, but misses one option for shells that adhere to that, using the globally accessable /etc/profile.d for running scripts. The benefit of running scripts from there is that a user will not be able to tamper with it and the administrator can choose (for example using `id -g` statements) to which users the script should apply.

arizonagroovejet 08-01-2008 02:54 AM

Quote:

Originally Posted by unSpawn (Post 3232945)
Actually it depends, and the OP isn't clear about that nor has he returned to elaborate.

Yep. As is unfortunately so often the case they don't provide enough detail for people to give anything other than generalised answers which may or may not help them achieve what they want to do. So OP you need to tell us:

- What this script you want to run actually does.
- Exactly under what circumstances you want it to run. (E.g. after you log in on a text session, a graphical session, every time you start a new shell (if so which shell?))
- If you're using a graphical desktop environment then which one.

Quote:

Originally Posted by unSpawn (Post 3232945)
It should be clear that the /etc/init.d and /etc/rc.local parts of the SysV initscript structure have nothing to do with running scripts on user login.

You'd think. It could be that the people who have suggested these didn't read the question properly. If not then hopefully they've learnt something as a result of this thread.

onebuck 08-01-2008 06:39 AM

Hi,
Quote:

Originally Posted by hnshashi (Post 3232011)
Hi all,
I want run shelll script myScript.sh automaticaly after login.
plz help me how can do in Redhat 7.2

What part is not understood? The OP states wishes to run 'myScript.sh' after login.

arizonagroovejet 08-01-2008 07:26 AM

Quote:

Originally Posted by onebuck
What part is not understood? The OP states wishes to run 'myScript.sh' after login.

As has been established by the various (relevant) answers in this thread, there is more than one way in which you can run a script after login depending on how you are actually logging in.

If you don't use any kind of graphical environment then you can call myScript.sh from ~/.bashrc or the equivalent file for whatever shell you're using if it's not bash. You log in, ~/.bashrc is parsed and myScript.sh gets run as a result. Calling myScript.sh from ~/.bashrc will also cause it to run if you log in via ssh.

If you put the call in your ~/.bashrc and you're using a graphical desktop environment, I'll use KDE as an example, then every time the user opens Konsole (KDE's terminal app) then ~/.bashrc will be parsed and then myScript.sh will get run. In this scenario myScript.sh is not being run after login, it's being run every time a new shell is started in Konsole. Depending on what myScript.sh actually does then this may not be desirable behaviour.

If you are using KDE and only want myScript.sh to run once after you log in, not whenever you open a new shell in Konsole, then you can have KDE call myScript.sh when it starts up, which is just after you log in. That way myScript.sh is only one once, right after you log in, and it is not run again at any point whilst you are logged in.

So when saying you want to run a script after log in then it is advisable to provide information such as I suggested in my previous post because depending on what the script does and what, if any, desktop environment you are using makes a difference to running a script after log in is best achieved. Saying that you are using RedHat 7.2 is not useful information for the purposes of the question being asked because there is nothing special about RedHat 7.2 (unless you count that it's support life cycle ended at least six years ago) when it comes to running a script after you log in. Saying 'I use KDE and want to run a script just once after I log in' would be useful because then people would know to tell you that you can have KDE run scripts when it starts up and tell you how to do that.

onebuck 08-01-2008 12:48 PM

Hi,

The chicken or the egg scenario. Not much information from OP so we could all come up with different ways to handle a particular situation. His/her criteria was when the user login to the shell and wished to run myscript.sh.

A little AI or even simple boolean would be a means to setup. The case wuld be nice if the OP would have been a bit more specific.

hnshashi 08-04-2008 12:33 AM

Hi all,
Thanks for ur replies,

Just i have added path in .bashrc file,it is working.

Hellsteeth 09-25-2008 11:17 AM

run script once on login without gui
 
Quote:

Originally Posted by arizonagroovejet (Post 3233153)

If you are using KDE and only want myScript.sh to run once after you log in, not whenever you open a new shell in Konsole, then you can have KDE call myScript.sh when it starts up, which is just after you log in. That way myScript.sh is only one once, right after you log in, and it is not run again at any point whilst you are logged in.

I read this post with interest. I want to run a script (startup.sh for example) only on the first time I login to a terminal session immediately after bootup and not each and every time I open a session. I do not want to use a GUI (KDE/GNOME/Whatever).

I do not want it at boot time before login so the rc.d type locations are out. I only want it run once after initial machine boot up and initial login. The problem with .bashrc and .bash_profile means it gets run each time a terminal session is opened.

I have been using Linux for a few years now but scripting is not my strong point so would appreciate some pointers on this.

Many thanks

dasy2k1 09-25-2008 04:04 PM

erm you are aware that redhat 7.2 is several years out of date and should no longer be considered secure

you should upgrade to somthing a little newer

Hellsteeth 09-26-2008 04:43 AM

cli question
 
Quote:

Originally Posted by dasy2k1 (Post 3291791)
erm you are aware that redhat 7.2 is several years out of date and should no longer be considered secure

you should upgrade to somthing a little newer

Could not agree more, and apologies if I did not mention the fact that this was just a general question. I am actually running the latest stable version of Arch Linux. The previous poster (arizonagroovejet) gave such a good clear roundup of what to do in almost every situation except for a cli only install, I thought it an appropriate place to post. The answer would be applicable to almost any version of Linux (I think :) )


All times are GMT -5. The time now is 01:26 PM.