LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to automatic run a shell script at boot? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-automatic-run-a-shell-script-at-boot-675041/)

ceantuco 10-08-2008 08:41 AM

how to automatic run a shell script at boot?
 
hi guys!

I wrote my first useful shell script! :) now I would like to make it run as soon as I log in. where should I put the shell scrip to auto run?
thanks!

linuxer8786 10-08-2008 08:49 AM

Quote:

Originally Posted by ceantuco (Post 3304093)
hi guys!

I wrote my first useful shell script! :) now I would like to make it run as soon as I log in. where should I put the shell scrip to auto run?
thanks!

you should put your script in:/usr/local/bin

ceantuco 10-08-2008 09:00 AM

i just added the script to that location but it doesn't run. what else can I do?

dasy2k1 10-08-2008 09:06 AM

putting the script in /usr/local/bin just means you can type the name without having to type the full path

if you now out the command you use to exicute the script in your
~/.bashrc it will runn whenever you log in (if you log in graphically it may not run untill you open a terminal)

David the H. 10-08-2008 09:19 AM

If you're using KDE you can place it in ~/.kde/Autostart and it will run whenever you start your kde session. I know gnome has a similar folder somewhere as well.

I don't think placing it in ~/.bashrc is a good idea, because then it will run every time you open a new bash shell, not just on startup. Unless you want that behavior, of course.

Oh, and placing the script in /usr/local/bin will only allow you to run it by name only if that directory is set in your PATH variable. Most distros add this by default, but it's not guaranteed. /usr/local/bin is one of the recommended traditional locations in which to keep personal scripts and programs, however.

ceantuco 10-08-2008 09:32 AM

I put the file in ~/.kde/Autostart but the script doesn't run. what can I do?

David1357 10-08-2008 09:45 AM

Quote:

Originally Posted by ceantuco (Post 3304093)
I wrote my first useful shell script! :) now I would like to make it run as soon as I log in. where should I put the shell scrip to auto run?

From the man page for bash:
Code:

When bash is invoked as an interactive login shell, or as a
non-interactive shell with the --login option, it first reads
and  executes commands from the file /etc/profile, if that
file exists.  After reading that file, it looks for
~/.bash_profile, ~/.bash_login, and ~/.profile, in that order,
and reads and executes commands from the first one that exists
and is readable.  The --noprofile option may be used  when the
shell is started to inhibit this behavior.

You can call it from one of those files. If none of those files exist, you can create them.

ceantuco 10-08-2008 09:54 AM

I created ~/.bash_profile and put my script there but it didn't start up when I logged in.
what else can I try?

WorldIsNotFair 10-08-2008 10:18 AM

are you using this script on the office ? what role are you in the office (user or admin ?)

.bash_profile should be created auto when admin create an user.

u should consult your admin then.

if it's your laptop / personal desktop , just su in n' put it in the /etc/rc.local ... hehehe

permalac 10-08-2008 10:35 AM

Quote:

Originally Posted by ceantuco (Post 3304093)
hi guys!

I wrote my first useful shell script! :) now I would like to make it run as soon as I log in. where should I put the shell scrip to auto run?
thanks!

may be you are interested on running the script once at boot, on that case you should put you your script in /etc/rc.local, this script is launched after all /etc/rc.d stuff. and with root privileges.

ceantuco 10-08-2008 11:23 AM

ok... This is a personal computer, I'm running as user. I placed my script on /etc/rc.local (I had to md rc.local) but my script didn't run when I logged in.

I also put it on /etc/rc.d but it didn't work!

last thing I did was to put the scrip on in /etc/init.d but nothing!

any ideas?

Thanks guys for your time! :)

David1357 10-08-2008 11:54 AM

Quote:

Originally Posted by ceantuco (Post 3304153)
I created ~/.bash_profile and put my script there but it didn't start up when I logged in.
what else can I try?

What is the output of
Code:

$ ls -al ~
Also, you might want to put a bit of logging in your "~/.bash_profile" script to make sure it
1) runs
2) executes your script without any errors

ceantuco 10-08-2008 12:31 PM

there u go:

drwxr-xr-x 37 cesar users 4096 2008-10-08 12:18 .
drwxr-xr-x 5 root root 4096 2008-09-09 10:52 ..
drwx------ 3 cesar users 4096 2008-09-09 11:42 .adobe
-rw------- 1 cesar users 4928 2008-10-08 12:17 .bash_history drwxr-xr-x 2 cesar users 4096 2008-10-08 10:50 .bash_profile
-rw-r--r-- 1 cesar users 1177 2008-09-09 10:44 .bashrc
drwx------ 7 cesar users 4096 2008-09-09 11:00 .beagle drwxr-xr-x 2 cesar users 4096 2008-09-09 10:44 bin
drwx------ 3 cesar users 4096 2008-09-29 16:09 .cache drwxr-xr-x 5 cesar users 4096 2008-09-29 16:08 .config
drwx------ 3 cesar users 4096 2008-09-22 16:01 .dbus
-rw-r--r-- 1 cesar users 51 2008-10-08 12:18 .DCOPserver_work__0
lrwxrwxrwx 1 cesar users 31 2008-10-08
12:18 .DCOPserver_work_:0 -> /home/cesar/.DCOPserver_work__0
drwx------ 2 cesar users 4096 2008-10-08 10:41 Desktop
-rw------- 1 cesar users 26 2008-09-09 10:44 .dmrc
drwxr-xr-x 4 cesar users 4096 2008-09-23 12:29 Documents
-rw-r--r-- 1 cesar users 315 2008-09-09 10:44 .dvipsrc
-rw-r--r-- 1 cesar users 1637 2008-09-09 10:44 .emacs drwxr-xr-x 3 cesar users 4096 2008-10-07 13:18 .emacs.d
-rw------- 1 cesar users 16 2008-09-09 11:18 .esd_auth
drwxr-xr-x 2 cesar users 4096 2008-09-09 10:44 .fontconfig drwxr-xr-x 2 cesar users 4096 2008-09-09 10:44 .fonts
drwx------ 2 cesar users 4096 2008-10-08 10:43 .gconf
drwx------ 2 cesar users 4096 2008-10-08 10:44 .gconfd
drwx------ 3 cesar users 4096 2008-10-08 10:43 .gnome2
drwx------ 2 cesar users 4096 2008-09-09 11:02 .gnome2_private
-rw-r--r-- 1 cesar users 16257 2008-09-09 10:44 .gnu-emacs
drwx------ 3 cesar users 4096 2008-10-06 10:31 .gnupg drwxr-xr-x 10 cesar users 4096 2008-09-09 15:40 .housecall6.6
-rw-r--r-- 1 cesar users 25058 2008-09-09 15:39 hs_err_pid3895.log
-rw------- 1 cesar users 185 2008-10-08 12:18 .ICEauthority
-rw-r--r-- 1 cesar users 861 2008-09-09 10:44 .inputrc
drwxr-xr-x 3 cesar users 4096 2008-09-09 15:38 .java
drwx------ 4 cesar users 4096 2008-09-15 09:50 .kde
drwx------ 3 cesar users 4096 2008-09-09 10:44 .local
drwx------ 3 cesar users 4096 2008-09-09 15:38 .macromedia
-rw-r--r-- 1 cesar users 3098 2008-09-29 12:30 .mailcap drwxr-xr-x 3 cesar users 4096 2008-09-09 10:48 .mcop drwxr-xr-x 4 cesar users 4096 2008-09-09 11:02 .mozilla
-rw-r--r-- 1 cesar users 299 2008-10-08 09:52 music.sh
drwx------ 3 cesar users 4096 2008-10-08 09:55 .ooo-2.0
-rw-r--r-- 1 cesar users 1028 2008-09-09 10:44 .profile drwxr-xr-x 2 cesar users 4096 2008-09-09 10:44 public_html drwxr-xr-x 2 cesar users 4096 2008-10-08 12:18 .qt
-rw------- 1 cesar users 34548 2008-09-29 16:01 .realplayerrc
-rw------- 1 cesar users 2078 2008-10-06 13:48 .recently-used
-rw-r--r-- 1 cesar users 218 2008-09-09 13:12 .recently-used.xbel
drwxr-xr-x 2 cesar users 4096 2008-09-09 10:44 .skel
drwx------ 2 cesar users 4096 2008-10-02 12:08 .ssh
drwx------ 4 cesar users 4096 2008-09-24 12:31 .thumbnails
drwx------ 2 cesar users 4096 2008-09-09 12:11 .tsclient drwxr-xr-x 2 cesar users 4096 2008-09-09 11:16 .wapi drwxr-xr-x 4 cesar users 4096 2008-09-25 16:12 .wine drwxr-xr-x 2 cesar users 4096 2008-09-09 11:19 .wireshark
-rw------- 1 cesar users 259 2008-10-08 12:18 .Xauthority
-rw-r--r-- 1 cesar users 1940 2008-09-09 10:44 .xim.template drwxr-xr-x 2 cesar users 4096 2008-09-29 16:02 .xine -rwxr-xr-x 1 cesar users 1446 2008-09-09 10:44 .xinitrc.template
-rw------- 1 cesar users 8816 2008-10-08 13:19 .xsession-errors
-rw-r--r-- 1 cesar users 40690 2008-09-29 16:08 .y2log
-rw-r--r-- 1 cesar users 281 2008-09-29 15:30 .y2usersettings

David1357 10-08-2008 01:37 PM

Quote:

Originally Posted by ceantuco (Post 3304289)
drwxr-xr-x 2 cesar users 4096 2008-10-08 10:50 .bash_profile

.bash_profile is supposed to be a file, not a directory. You put commands that you want to run at login in the file.

trickykid 10-08-2008 01:39 PM

The proper place would be .bash_login and not .bash_profile.

If you want things to run when you exit, put them in .bash_logout

Good outline of bash/shell initialization files:

http://tldp.org/LDP/Bash-Beginners-G...ect_03_01.html

trickykid 10-08-2008 01:41 PM

Quote:

Originally Posted by ceantuco (Post 3304093)
hi guys!

I wrote my first useful shell script! :) now I would like to make it run as soon as I log in. where should I put the shell scrip to auto run?
thanks!

Also your title contradicts your question. Do you want to have this run when you login or before during boot?

If during login, use the .bash_login in you're users home directory. If at bootup before anyone logs in, put it in rc.local initialization file usually found in /etc/rc.d/

David1357 10-08-2008 02:33 PM

Quote:

Originally Posted by trickykid (Post 3304344)
The proper place would be .bash_login and not .bash_profile.

According to the man page, and my own experience, "~/.bash_profile" and "~/.bash_login" are essentially the thing with different names. You can double-check this by running
Code:

$ ls -l /etc/profile
and
Code:

$ ls -l /etc/login
and telling us which one exists on your system.

ceantuco 10-08-2008 02:37 PM

hey guys! I added my script to ~/.bash_login but still it doesn't run. I want the script to run when I log in (sorry about the confusion with the title)

I have /etc/profile and I DO NOT have /etc/login

thanks guys!

trickykid 10-08-2008 03:27 PM

Quote:

Originally Posted by ceantuco (Post 3304410)
hey guys! I added my script to ~/.bash_login but still it doesn't run. I want the script to run when I log in (sorry about the confusion with the title)

I have /etc/profile and I DO NOT have /etc/login

thanks guys!

What is this script and how do you know it's not being run?

ceantuco 10-08-2008 03:32 PM

the script is supposed to ask what type of music I want to listen to then I will answer and depending my answer it would open my sound files. I know it is not running because it doesn't ask me anything. Is it possible that I'm doing something wrong?

trickykid 10-08-2008 03:39 PM

Quote:

Originally Posted by David1357 (Post 3304404)
According to the man page, and my own experience, "~/.bash_profile" and "~/.bash_login" are essentially the thing with different names. You can double-check this by running
Code:

$ ls -l /etc/profile
and
Code:

$ ls -l /etc/login
and telling us which one exists on your system.

True but so is .profile, it's just more of a preference thing.

Here's why I separate them though from one another, .profile, .bash_login and .bash_profile are more for the initial things you want done upon login but on each terminal or shell you open. When I want something specific to run when I login as in a specific command, etc, I'll put it in .bash_login. This just keeps things tidy really to me.

Really you can have .bash_login, .profile and or .bash_profile to do everything you want initially when you login but .bashrc is really where it slightly differs from those and should be used for environment settings and aliases, etc. Say for instance if you're in X or GUI, each time you open a new terminal, it will search for .bashrc and not the others. I would imagine you might want a script to run upon initial login but not each time you want to run a new terminal or shell.

trickykid 10-08-2008 03:41 PM

Quote:

Originally Posted by ceantuco (Post 3304466)
the script is supposed to ask what type of music I want to listen to then I will answer and depending my answer it would open my sound files. I know it is not running because it doesn't ask me anything. Is it possible that I'm doing something wrong?

Well, how are you logging in? I'd imagine logging in through a GUI instead of at the console it's not going to work like you anticipate.

ceantuco 10-08-2008 03:46 PM

yes, I'm using the GUI! ok, so it is not going to work. what I wanted to do was something like a .bat file (for windows) where you could place it on the startup folder or the run key on the registry to make it run.
is that possible?

David1357 10-08-2008 03:48 PM

Quote:

Originally Posted by ceantuco (Post 3304410)
hey guys! I added my script to ~/.bash_login but still it doesn't run. I want the script to run when I log in (sorry about the confusion with the title)

Did you make "~/.bash_login" a directory and put your script in the directory? That is wrong! "~/.bash_login" and "~/.bash_profile" are files.

Also, did you do what I suggested and put some logging in "~/.bash_profile" (or "~/.bash_login)" to make sure everything is working the way you expect it to? If you are not adding any logging or debugging statements to your script, you are being lazy. Laziness will not solve your problems.

ceantuco 10-08-2008 03:55 PM

wooowww you are being lazy. Laziness will not solve your problems are hard statements buddy. I'm not being lazy, at first I thought I had to create a dir and cp my shell to it but then I read that "~/.bash_login" and "~/.bash_profile" were files so I did that but it didn't work. Now I'm not a programmer so I DO NOT know how to add debugging statements.

David1357 10-08-2008 04:00 PM

Quote:

Originally Posted by ceantuco (Post 3304478)
yes, I'm using the GUI! ok, so it is not going to work. what I wanted to do was something like a .bat file (for windows) where you could place it on the startup folder or the run key on the registry to make it run.

When you login through the GUI, the contents of the "~/.bash_profile" (or "~/.bash_login") file will get executed, just as if you logged in via the console.

In your case, the root of the problem is that you have a "~/.profile" file, which takes precedence over the other two files. You need to call your script from the end of that file.

David1357 10-08-2008 04:02 PM

Quote:

Originally Posted by ceantuco (Post 3304490)
Now I'm not a programmer so I DO NOT know how to add debugging statements.

You should be able to add lines like
Code:

echo "This line got executed" >> ~/debug.log
to your "~/.profile" file and your script to see where things go wrong. If nothing shows up in "~/debug.log" then you know you have modified the wrong file or made some other mistake.

ceantuco 10-08-2008 04:06 PM

Thanks David I'll give it a shot!

trickykid 10-08-2008 04:06 PM

Quote:

Originally Posted by ceantuco (Post 3304490)
wooowww you are being lazy. Laziness will not solve your problems are hard statements buddy. I'm not being lazy, at first I thought I had to create a dir and cp my shell to it but then I read that "~/.bash_login" and "~/.bash_profile" were files so I did that but it didn't work. Now I'm not a programmer so I DO NOT know how to add debugging statements.

Probably in order for it to work is you need to kick off a terminal to then run the script within the shell prompt upon login into the GUI, since I'm guessing it's just a shell script and not a GUI program you created to start at login.

This may depend on what Desktop environment you use (might have to put it somewhere in an AutoStart ocnfiguration for Gnome, KDE or whatever you use) but something like this might work:

xterm -e "/path/to/script"

You can replace xterm with whatever terminal software you choose.

And we should really stop the nonsense about members being lazy. Don't assume, just help or move on without all the negativity.

trickykid 10-08-2008 04:08 PM

Quote:

Originally Posted by David1357 (Post 3304497)
You should be able to add lines like
Code:

echo "This line got executed" >> ~/debug.log
to your "~/.profile" file and your script to see where things go wrong. If nothing shows up in "~/debug.log" then you know you have modified the wrong file or made some other mistake.

It still won't work unless this script is either a GUI program or it's launched within a terminal like I've pointed out. It probably is getting run but then dies cause it doesn't know where to run the output to when logging into the GUI.

trickykid 10-08-2008 04:13 PM

I don't know about KDE but under Gnome, you should have a Sessions in System > Preferences > Personal > Sessions

This is where you can add startup programs. I'm sure KDE has the same options, just couldn't tell you exactly the path to configure programs to startup within KDE at login.

But you can add all you want to your bash initialization scripts, it's not going to work since you're not logging into a console and you're using a plain old bash script. It needs to be run in a terminal to work. ;)

ceantuco 10-08-2008 04:27 PM

thanks Trickykid! I'll try it later! I appreciate your help!

David the H. 10-08-2008 10:25 PM

Have you made sure you have proper execute permissions on the script as well? Perhaps you should tell us in more detail exactly what you've tried, i.e. what lines you've tried addding to the files and whatnot.

And pay attention to trickykid's comment about the script itself needing a gui. If the thing only runs in a terminal, then you're not going to see anything because it doesn't know how to open up a terminal window on startup so that it can ask you for input. You either have to use something like xdialog, kdialog, or gtkdialog in your script in order to get a window, or perhaps start it from a shortcut with the "run in terminal" option selected.

~/.kde/Autostart (or the equivalent in gnome) really should work, provided you have everything else working right. You could also keep the script somewhere else and create a shortcut to it in that directory.

David1357 10-09-2008 07:35 AM

Quote:

Originally Posted by trickykid (Post 3304506)
It still won't work unless this script is either a GUI program or it's launched within a terminal like I've pointed out.

That is not true. I tried it on an Ubuntu machine, and the commands in my "~/.profile" got executed when I logged in via the GUI.

Now if the script is doing something like "read", then you are right about needing to launch a terminal. However, I have been operating under the assumption that the script asked the question using a dialog.

ceantuco 10-09-2008 11:37 AM

Hi David,

I ran chmod 755 [file] to give the proper permissions to my script and I'm able to run it when I type my file name and then hit enter. I just can't get the damm thing to run when I log in! This is very frustrating.

David the H. 10-09-2008 12:01 PM

Quote:

Originally Posted by David1357 (Post 3305145)
However, I have been operating under the assumption that the script asked the question using a dialog.

I wouldn't assume that. ceantuco's description said it prompted the user for input, but didn't make it clear exactly how it does that. Since he called it his "first script", the reasonable assumption is that he's using a simple read prompt. If he hasn't arranged for either a terminal window or a gui dialog to pop up, then the script is certainly being executed, but hanging or failing because there's no way to give it the input it needs.

Of course a good way to test if it's actually getting executed is to add a few debugging lines and then check if anything new has been logged after login.

David the H. 10-09-2008 12:08 PM

ceantuco, how exactly does the script prompt you? Are you using any kind of gui dialog, or just the terminal prompt? Perhaps you could post it here for us?


All times are GMT -5. The time now is 06:47 PM.