start a program when I login, not boot
running slackware 10
I would like to have a program run when I log in from the non GIU interface, the basic one with slack. I just dont know where to add the script. Its a program that all users have access to. I see alot of post regarding starting a program at boot, but few regarding login, that have helped. One post did say it does not matter to start a program at login, that you can just start it at boot, and every user will be able to access it PS it needs an internet connection to work, so I would need it to run after my internet connection has been established |
Add your script to ~/.bash_profile. As I understand it .bash_profile is only executed when you first login, not like ~/.bashrc which is executed for every instance of bash
|
where do i find ./bash_profile???
I used the find -name command with no luck. |
Quote:
Quote:
LocoMojo |
It doesn't exists by default, just create it.
|
Thanks, I found it along with some other interseting things.
Is there anything I have to include in there other then the path to the program I want to run, along with the program name. cause it did not work when I logged on. example... /directory/directory/program_to_run thats the only line in my .bash_profile |
Quote:
Quote:
If it is not in your path then you have two choices: 1. Use absolute paths to call it, i.e., /usr/local/bin/program & (use & to send it to the background so you still have access to your console/xterm). 2. Put the program in your path. For example, if the program in question is in /usr/local/bin , but /usr/local/bin is not in your path then you need to issue the follwoing: Quote:
LocoMojo |
Quote:
I'll post my script to save some time. export PATH=$PATH:/usr/BOINC echo $PATH run_client & or /usr/BOINC/run_client & ive tried both with no luck |
Quote:
Is it called "run_client"? If so, is run_client a binary? Don't type "echo $PATH run_client &". Only type "echo $PATH" to see what directories are currently in your path. It is used for information only. If "run_client" is a binary and if it is in your path then you will only need to type "run_client" without the quotes. If "run_client" is in a directory that is not currently in your path then you need to put the directory (not the program) in your path. For example, if "run_client" is in the /usr/bin directory then you would do "export PATH=$PATH:/usr/bin". After that you will be able to type "run_client" and the program will run. Another way to determine whether "run_client" is in your path is to type "which run_client" without the quotes. If it is in your path it will show you the directory it is in. If it's not in your path it will say something like "no run_client found in this directory, that directory or that other directory". If you know exactly where "run_client" is, can you type "file /path/to/run_client" and then post back here what it says? LocoMojo |
the program is called run_client
dont know how to tell if its a binary, but all i have to do to run it is cd to its directory, and type "run_client &" and it will un for me in the background. I did use the command export PATH=$PATH:/usr/BOINC which is the path I need. When I do that it does allow me to run run_client from my home directory without having to cd to the programs directory. however once I logoff that directory is gone from my home path. So would this be the script to run??? #!/bin/bash export PATH=$PATH:/usr/BOINC run_client & with regards to file /usr/BOINC/run_client I get this result... /usr/BOINC/run_client: ASCII text |
Quote:
|
Quote:
Must be a script, I guess. Still, it should say something like "Bourne again script, executable". Anyway, if you want it to stay in your path after you log out and in then you should do this: 1. Go to your home directory and type "touch .bashrc" without the quotes. 2. Open .bashrc with your favorite editor and type the following: # .bashrc # myExports export PATH=$PATH:/usr/BOINC Then save the file. From then on every time you log in and open your xterm "run_client" will be in your path. If you ever decide to take it out of your path, for whatever reason, just comment it out by typing a "#" at the beginning of the "export" line. One thing though...be sure that your .bash_profile says: Quote:
Good luck! LocoMojo |
didnt work. input exactly as you have it, in both files.
is it possible that when I log in from the CLI (runlevel 3) that its not even looking for .bash_profile ??? I thought I read somewhere that .bash_profile was for logging in with a GUI. |
From O'Reilly's 'Learning the Bash Shell', 3rd edition (pg 57):
.bash_profile is read and executed only by the login shell. If you start up a new shell (a subshell) by typing bash on the command line, it will attempt to read commands from the file .bashrc. In other words, .bash_profile is run when you login at the CLI in run level 3. |
how do I know if its reading it or not??
should I not get error messages for wrong paths ro anything I am doing wrong. I have since set it up to run when I boot, by adding some script to the rc.local file. It took a few boot attempts before I got the scipt right. However everytime I got it wrong I was able to see boot errors telling me either the path didnt exist, or couldnt find the command etc... Should i not see errors via the login methdod like i do with the boot method??? |
jturnbul,
I think I misunderstood you. I thought you were just trying to put a program in your path so that you could call it from the CLI. I don't know how I missed it, but you were actually asking how to have the program start up automatically when you logged in...correct? I've just re-read the thread and I noticed that you wanted the program to start up when you logged in, but not when the machine booted. I'm assuming that you want the program to only start up for you and not any other user...correct? If you wanted it to start up automatically upon boot, you would put it in the /etc/rc.d/rc.local script. I've never had the occasion to start up a program only for a particular user because on my machine I am the only user so everything I have automatically starting up, starts up at boot, not the login. You say you're booting into run level 3 so obviously you're not booting into KDE or any other GUI environment. If you were then you could simply run the program in question then log out without shutting that program down, saving the session and then the program would run every time you logged into that saved session. As for logging into the console rather than X, I would imagine that anything in your .bashrc would run upon login. Rather than just export the path you would also want to put the actual program in the .bashrc like this: /usr/BOINC/run_client & Of course, you would need to leave the export line in that you put in earlier so that the program would be in your path. Make sure the export line is before the program call. In other words your .bashrc should look like this: Quote:
One question though. Does that "run_client" program require X? Is it a GUI program? If it is, then it won't work when you log in at run level 3. Sorry for my misunderstanding. Hope this helps this time. LocoMojo |
I wouldn't put it in ~/.bashrc because if you were to login, startx, then launch a few virtual terminals, that program will be executed for each instance of the terminal.
You can try this little test to make sure .bash_profile is being read when you login: Add 'echo "Hello World!"' to the end of ~/.bash_profile, without the single quotes. Logout and and then log back in again and somewhere in all the details before the prompt is given to you you should see the words "Hello World!" in there somewhere |
Quote:
You know, I hadn't thought of that. You're so right. However, this is also true for the .bash_profile. I just tried it with /opt/kde/bin/kcalc and I opened 3 instances of xterms and got 3 instances of kcalc. There's probably a very simple way to pull this off. Perhaps a simple bash script which is called upon login and checks to see if "program" is running, if not, execute it? LocoMojo |
Here's a little script I just wrote:
Quote:
Quote:
I'm sure there's a more elegant way of approaching this, but I'm a newbie and this is the idea I came up with. Maybe someone else will come along and say "why don't you just do this stupid...." LocoMojo |
According to the man page .bash_profile is only executed for login shells.
Ok, my linux machine is headless and I use putty to ssh into it, but I can use screen to test this. I added `echo "Hello World!` to ~/.bash_profile. I now launch a new bash session within screen (`screen bash`) and the prompt is given to me, I then launch another bash session within screen but give bash the --login argument (`screen bash --login`), the words "Hello World!" are printed then the prompt is returned. As I understand it this is correct behaviour - ~/.bash_profile is only run for login shells. Perhaps something in your configuration is making all your terminals login shells or something is making .bash_profile be included for all terminals |
Doh!
I'm such an idiot. I was using my Konsole icon on my kicker in KDE which is configured with the command "konsole -ls", konsole login shell. You're right, my bad. LocoMojo |
You are digging in the wrong place, I think.
You want to log in with KDE or GNOME (or another desktop manager) and have your program run automatically right? The thing you have been trying to do will only work when you log into a console or open a xtem/konsole on the GUI. There are two ways two approach it: most desktops will have a entry in the menu for programs to run after startx or [xdm/gdm/kdm]. Try that out first as it is more userfriendly (somehow I can't find one now on this system...?). The other option is to edit the .Xsession file(s) in your home directory, but I don't know if that would interfere with the normal startup procedure. To make sure that an init-script (that is something different indeed) is called after the network is up, one would need to make sure that the links in the rc directories pointing to it begin with a higher number in their name. If your setup will wait for a dhcp reply in the background, this may not work. In that case you'll need to write some shellcode around the calll to your daemon that checks if an interface is up. But I don't think you want that. |
Quote:
He said in his first post... Quote:
LocoMojo |
Quote:
If you want the program to run when anybody logs in then you could place the command in /etc/profile. However, rather than muck with the original scripts, consider creating a separate script and place that script in /etc/profile.d. All scripts stored in that directory are run from within /etc/profile. Name the script anything you want, but ensure the script uses an sh extension. For comparison sake, I have named my script local.sh. The bash startup scripts are sourced automatically only with the bash shell and not the Korn or C shells. The ~/.bashrc script gets sourced by bash whenever you launch a non-login terminal session. Thus, from the initial runlevel 3 non-X login screen, bash does not source ~/.bashrc. Therefore, to run any additional commands you want to place in ~/.bashrc you must source that latter script from within ~/.bash_profile. However, from any subsequent non-login shell, such as in an X-based xterm or Konsole, bash will source ~/.bashrc. So this thread probably is as good as any time to go live with my web site. :) Human Readable Of particular interest to Slackers: A Slackware Desktop Enhancement Guide Of particular interest to this thread: Harmonizing the Bash Startup Scripts Automagically Running Programs During Startup and Shutdown One last note about something mentioned in this thread, KDE provides two types of terminal windows to launch: a non-login terminal window and a login terminal window. If using bash and the bash startup scripts, then the behavior of each type of terminal window will be based upon the bash startup scripts. If using Konsole, then investigate the Konsole configuration (Settings->Configure Konsole->Sessions tab) to see the differences in the command line options each type of terminal session Konsole supports. |
to confirm I want this program to run when I login to the CLI, not the GUI.
I have found my first problem. I tried the command Quote:
I triend the same script in my .bash_profile of my root account, and it did work. So there is an obvious permission issue. I used the command Quote:
that did not help it as the "hello world" script still wont run. So my non-root account is not looking at .bash_profile |
I have 2 questions if that's OK... Can you confirm that the .bash_profile you are altering is the one in your non-root user's home directory? Can you confirm that /bin/bash is set as the shell in /etc/passwd (if not, .bash_profile is not read)? You can check the first with the command below (while logged in as the non-root user):
Code:
cat ~/.bash_profile && ls -l ~/.bash_profile Code:
grep `whoami` /etc/passwd | cut -d: -f7 |
Also verify that the ~/.bash_profile script located in your non-root account home directory is owned by that account and not root. Occasionally I copy files as root and then forget to modify the file's group and account ownership. Use the list command (ls -l) in your home directory to view the file's ownership. If you see "root root" then the file is owned by root and not your non-root account.
|
cat ~/.bash_profile && ls -l ~/.bash_profile
I get this result, which to me, looks like I dont have executale rights... correct?? #!/bin/bash echo "Hello World" -rw-rw-r-- 1 jnt users 38 2006-01-25 17:35 /home/jnt/.bash_profile grep `whoami` /etc/passwd | cut -d: -f7 or grep $(whoami) /etc/passwd | cut -d: -f7 I get a blank line print with either of those commands |
this is my passwd file. I dont think /bin/bash is in my non root directory of 'jnt'
root:x:0:0::/root:/bin/bash bin:x:1:1:bin:/bin: daemon:x:2:2:daemon:/sbin: adm:x:3:4:adm:/var/log: lp:x:4:7:lp:/var/spool/lpd: sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/: news:x:9:13:news:/usr/lib/news: uucp:x:10:14:uucp:/var/spool/uucppublic: operator:x:11:0:operator:/root:/bin/bash games:x:12:100:games:/usr/games: ftp:x:14:50::/home/ftp: smmsp:x:25:25:smmsp:/var/spool/clientmqueue: mysql:x:27:27:MySQL:/var/lib/mysql:/bin/bash rpc:x:32:32:RPC portmap user:/:/bin/false sshd:x:33:33:sshd:/: gdm:x:42:42:GDM:/var/state/gdm:/bin/bash pop:x:90:90:POP:/: nobody:x:99:99:nobody:/: jnt:x:1000:100::/home/jnt: |
The problem is that you don't have bash set as your shell. The line for jnt should end in /bin/bash the same as gdm's line does.
|
Quote:
|
Quote:
|
try "man bash" then "/INVOCATION" (yes, use caps) - then hit "n" a couple of times to get to the right section, which describes the order of reading startup files, BASH_ENV, and lots of other stuff.
:cool: |
finally done. Thanks to all those who replied.
Once i got /bin/bash into my directory, i just had to use what was suggested in the earlier posts of editing .bash_profile, and having it cd to the directory and running the command. Now for some modifications. I have the program start up in the background, however there is verbose information displayed across the screen, as the program starts up. How do i redirect that info to the bit bucket, so it is completely behind the scenes? Also, I am running into the problem mentioned earlier, that once I am in KDE (GUI) and open a terminal, it tries to run the program again. Not a huge issue, as it just says another instance of the program is running, and ignores the command, but I would like to learn how to prevent that. Thanks again to all of you, you have helped me learn alot. |
Quote:
|
I don't think that is the right way to go, there might be other stuff in your profile that you do want set. use something like: [HTML]
if [ ! -e /tmp/yourprogram/yourprogram.pid ] ; then mkdir -p /tmp/yourprogramname echo $$> /tmp/yourprogram/yourprogram.pid #now start up the program itself /path/to/your/program #if the program exited normally, this will clean up the pid file rm /tmp/yourprogram/yourprogram.pid fi [/HTML] Put this file somewhere in your path (~/bin usually works) and call that from your profile. Pleaseo not put this snippet directly into production, I just wrote it on the fly so you can be sure some flies and bug will be hiding in it. |
Quote:
Quote:
1. Edit ~/.bash_profile in a manner similar as suggested by Jelle so that only one instance always runs. You also might consider looking into the /sbin/pidof command, which is handy to help determine whether a program is already running. That command is kind of like a combination of ps ax | grep "whatever" and returns the pid of the program you seek. 2. Configure Konsole (or xterm, etc) with the correct options such that you normally are not running another login shell and instead run only a terminal shell. If you are using KDE, then you can place a handy icon in your Quick Launch area of the Kicker task bar. On the Kicker task bar, use your secondary mouse button to open the pop-up context menu. Then select Add to Panel, then Special Button, then Terminal Sessions. That icon will present to you several ways to start Konsole and the first option will be simply a (non-login) terminal shell, although other options will include Linux Console, root shell, etc. |
All times are GMT -5. The time now is 01:49 AM. |