LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Slackware (https://www.linuxquestions.org/questions/slackware-14/)
-   -   start a program when I login, not boot (https://www.linuxquestions.org/questions/slackware-14/start-a-program-when-i-login-not-boot-407484/)

jturnbul 01-24-2006 01:39 PM

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

phil.d.g 01-24-2006 02:41 PM

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

jturnbul 01-24-2006 06:35 PM

where do i find ./bash_profile???
I used the find -name command with no luck.

LocoMojo 01-24-2006 07:10 PM

Quote:

Originally Posted by jturnbul
where do i find ./bash_profile???
I used the find -name command with no luck.

It's in your home directory. To see it you need to issue the following command:

Quote:

ls -a (if you're in your home directory)

ls -a ~/ (if you're not in your home directory)
By the way, it is .bash_profile not ./bash_profile. There's also a systemwide file called profile in /etc.

LocoMojo

gbonvehi 01-24-2006 07:21 PM

It doesn't exists by default, just create it.

jturnbul 01-24-2006 07:27 PM

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

LocoMojo 01-24-2006 07:41 PM

Quote:

Originally Posted by jturnbul
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

If the program you want to run is in your path:

Quote:

echo $PATH
Then you only have to type the name of the program to call it.

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:

export PATH=$PATH:/usr/local/bin
Another way to see your PATH is to type "env" without the quotes. This will bring up all your environment variables.

LocoMojo

jturnbul 01-24-2006 08:27 PM

Quote:

Originally Posted by LocoMojo
If the program you want to run is in your path:



Then you only have to type the name of the program to call it.

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:



Another way to see your PATH is to type "env" without the quotes. This will bring up all your environment variables.

LocoMojo

Thanks, I tried the commands from CLI and they worked, however I put them into my .bash_profile and nothing happens when I log on.

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

LocoMojo 01-24-2006 08:55 PM

Quote:

Originally Posted by jturnbul
Thanks, I tried the commands from CLI and they worked, however I put them into my .bash_profile and nothing happens when I log on.

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

What program are you trying to run?

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

jturnbul 01-24-2006 09:17 PM

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

jturnbul 01-24-2006 09:28 PM

Quote:

#!/bin/bash
export PATH=$PATH:/usr/BOINC
run_client &
this didnt work

LocoMojo 01-24-2006 09:36 PM

Quote:

Originally Posted by jturnbul
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

It's a text file and it runs when you type run_client & ?

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:

if [ ! "$BASHRC" -a -f ~/.bashrc ]; then
. ~/.bashrc
fi
If it doesn't then put that in the .bash_profile file exactly as quoted.

Good luck!

LocoMojo

jturnbul 01-24-2006 09:45 PM

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.

gilead 01-24-2006 09:53 PM

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.

jturnbul 01-24-2006 10:33 PM

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???

LocoMojo 01-25-2006 12:14 AM

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:

# .bashrc

# myExports
export PATH=$PATH:/usr/BOINC/

# Start up
/usr/BOINC/run_client &
I'm guessing that that would work. I never tried it. The only thing that might be a problem (I'm just guessing here) is if you're using ksh or csh (or any other non-bash shell) for your shell it wouldn't source the .bashrc file. I don't know, I'm a newbie myself, maybe someone else could tell you.

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

phil.d.g 01-25-2006 03:14 AM

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

LocoMojo 01-25-2006 07:35 AM

Quote:

Originally Posted by phil.d.g
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.

Hi phil.d.g,

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

LocoMojo 01-25-2006 08:40 AM

Here's a little script I just wrote:

Quote:

#!/bin/bash

# Check to see if kcalc is running. If not, then exec it.

if ps aux | grep -w "kcalc" | grep -v grep > /dev/null ;
then
echo "Kcalc is already running"
else
/opt/kde/bin/kcalc &
fi
exit 0

I don't know if the syntax is right, but it works. I called the script "runkcalc" and put the following line in my .bashrc :

Quote:

~/bin/runkcalc
Then I opened up several instances of Konsole, but wound up with only one instance of Kcalc.

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

phil.d.g 01-25-2006 10:03 AM

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

LocoMojo 01-25-2006 10:26 AM

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

Jelle 01-25-2006 11:18 AM

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.

LocoMojo 01-25-2006 11:39 AM

Quote:

Originally Posted by Jelle
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.

I think jturnbull is talking about the console.

He said in his first post...

Quote:

I would like to have a program run when I log in from the non GIU interface, the basic one with slack.
Maybe he needs to clarify whether he means he is logging into an X session from the console or if he is logging into the console.

LocoMojo

Woodsman 01-25-2006 02:23 PM

Quote:

I would like to have a program run when I log in from the non GIU interface
If you want the program to run only when you login, then place the program in your ~/.bash_profile script. In typical Slackware fashion, this file does not exist and must be created by the end-user. The script does not need to be executable because the script is sourced by bash when you login. Assign 644 permissions to the file. This script gets sourced by bash only once and only at login.

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.

jturnbul 01-25-2006 04:52 PM

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:

echo "hello world"
in my non root account .bash_profile, and nothing happened.

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:

chmod -Rv 644 .bash_profile
and it advised me permissions were changed (rw-r--r--)
that did not help it as the "hello world" script still wont run. So my non-root account is not looking at .bash_profile

gilead 01-25-2006 04:59 PM

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
You can check the second with the command below (while logged in as the non-root user):

Code:

grep `whoami` /etc/passwd | cut -d: -f7
or
grep $(whoami) /etc/passwd | cut -d: -f7


Woodsman 01-25-2006 05:41 PM

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.

jturnbul 01-25-2006 07:32 PM

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

jturnbul 01-25-2006 07:36 PM

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:

gilead 01-25-2006 07:47 PM

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.

jturnbul 01-25-2006 07:50 PM

Quote:

Originally Posted by gilead
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.

How did that happen?, and better yet how do I fix it???

jturnbul 01-25-2006 08:30 PM

Quote:

Originally Posted by jturnbul
better yet how do I fix it???

well solved that one myself by just editing passws as root and adding /bin/bash to my directory.

Genesee 01-25-2006 08:49 PM

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:

jturnbul 01-25-2006 08:58 PM

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.

jturnbul 01-25-2006 09:02 PM

Quote:

Originally Posted by Genesee
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:

so where would I add the --noprofile option?? I think thats what would stop bash from running .bash_profile again, when I open a terminal??

Jelle 01-26-2006 10:54 AM

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.

Woodsman 01-26-2006 12:00 PM

Quote:

How do i redirect that info to the bit bucket, so it is completely behind the scenes?
You want to redirect the standard output and standard error (output) from the screen to /dev/null. Check the web for the Advanced Bash Scripting How-To.

Quote:

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.
That indicates that although you placed the program in ~./bash_profile, which runs only once during the actual login, unlike .bashrc, that you are starting Konsole (or xterm, etc.) with the login shell option rather than just opening a terminal session. Consider some options to help you with that:

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.