Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: RHEL, Debian, Gentoo when I'm brave.
Posts: 47
Rep:
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.
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.
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.
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
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.
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
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.
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.
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.
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.
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 )
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.