LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 07-31-2008, 07:11 AM   #1
hnshashi
LQ Newbie
 
Registered: Jul 2008
Location: Bangalore,India
Posts: 16

Rep: Reputation: 0
Smile 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
 
Old 07-31-2008, 07:53 AM   #2
d2490n
Member
 
Registered: Jul 2005
Location: San Diego, CA
Distribution: RHEL, Debian, Gentoo when I'm brave.
Posts: 47

Rep: Reputation: 15
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
 
Old 07-31-2008, 09:05 AM   #3
owenjh
Member
 
Registered: Sep 2007
Distribution: Fedora / Slackware
Posts: 46

Rep: Reputation: 15
Try looking for a .bashrc file in your home directory

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


- Owen.
 
Old 07-31-2008, 11:53 AM   #4
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: SLED, openSUSE
Posts: 1,009

Rep: Reputation: 171Reputation: 171
Quote:
Originally Posted by d2490n View Post
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.
 
Old 08-01-2008, 12:30 AM   #5
sunils1973
Member
 
Registered: Jan 2007
Location: INDIA
Distribution: Ubuntu, Debian
Posts: 320

Rep: Reputation: 30
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.
 
Old 08-01-2008, 12:58 AM   #6
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: SlackwareŽ
Posts: 11,052
Blog Entries: 1

Rep: Reputation: 1373Reputation: 1373Reputation: 1373Reputation: 1373Reputation: 1373Reputation: 1373Reputation: 1373Reputation: 1373Reputation: 1373Reputation: 1373
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!
 
Old 08-01-2008, 02:30 AM   #7
unSpawn
Moderator
 
Registered: May 2001
Posts: 26,999
Blog Entries: 54

Rep: Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745
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.
 
Old 08-01-2008, 02:54 AM   #8
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: SLED, openSUSE
Posts: 1,009

Rep: Reputation: 171Reputation: 171
Quote:
Originally Posted by unSpawn View Post
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 View Post
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.
 
Old 08-01-2008, 06:39 AM   #9
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: SlackwareŽ
Posts: 11,052
Blog Entries: 1

Rep: Reputation: 1373Reputation: 1373Reputation: 1373Reputation: 1373Reputation: 1373Reputation: 1373Reputation: 1373Reputation: 1373Reputation: 1373Reputation: 1373
Hi,
Quote:
Originally Posted by hnshashi View Post
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.
 
Old 08-01-2008, 07:26 AM   #10
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: SLED, openSUSE
Posts: 1,009

Rep: Reputation: 171Reputation: 171
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.
 
Old 08-01-2008, 12:48 PM   #11
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: SlackwareŽ
Posts: 11,052
Blog Entries: 1

Rep: Reputation: 1373Reputation: 1373Reputation: 1373Reputation: 1373Reputation: 1373Reputation: 1373Reputation: 1373Reputation: 1373Reputation: 1373Reputation: 1373
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.
 
Old 08-04-2008, 12:33 AM   #12
hnshashi
LQ Newbie
 
Registered: Jul 2008
Location: Bangalore,India
Posts: 16

Original Poster
Rep: Reputation: 0
Hi all,
Thanks for ur replies,

Just i have added path in .bashrc file,it is working.
 
Old 09-25-2008, 11:17 AM   #13
Hellsteeth
LQ Newbie
 
Registered: Sep 2008
Posts: 2

Rep: Reputation: 0
run script once on login without gui

Quote:
Originally Posted by arizonagroovejet View Post

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
 
Old 09-25-2008, 04:04 PM   #14
dasy2k1
Member
 
Registered: Oct 2005
Location: 127.0.0.1
Distribution: Ubuntu 12.04 X86_64
Posts: 956

Rep: Reputation: 34
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
 
Old 09-26-2008, 04:43 AM   #15
Hellsteeth
LQ Newbie
 
Registered: Sep 2008
Posts: 2

Rep: Reputation: 0
cli question

Quote:
Originally Posted by dasy2k1 View Post
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 )
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Run shell script as root automatically elempoimen Linux - Software 8 06-20-2010 01:24 AM
How to run a shell script evey day automatically at a particular time krevathi1912 Programming 3 09-22-2007 01:15 PM
RH8 - how to automatically run a shell script at boot time nu-B Linux - Newbie 2 11-03-2003 02:47 PM
RH8 - how to automatically run iptables rules shell script at boot time nu-B Linux - General 1 10-29-2003 08:38 PM


All times are GMT -5. The time now is 05:46 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration