Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have a script file (start-conky.sh). It executes correctly, but when I do a "ps -u " it shows as still a PID, which I'm assuming means it is still in memory. Unless I do a "kill " it just stays there. It is not causing any problem. I'm just curious as to how to get the script to terminate after it executes. I've listed the script below. I really don't know that much about writing programs, so I'm sure there are mistakes. Can anyone help?

Title: start-conky.sh "the title is just listed here it is not in the script"

#!/bin/bash

conky -c ~/Conky/scripts/config1 & conky -c ~/Conky/scripts/config2

exit
share|improve this question
    
Uh, sorry. How do you accept answers. I've taken most all of the questions that were answered and used the info successfully. If I have miss some protocol, I apologize. –  user18432 Apr 17 '12 at 18:46
    
Thanks, I see it and will make use of it. Thanks again. –  user18432 Apr 17 '12 at 19:00
    
No problem..... –  David Oneill Apr 17 '12 at 19:03

2 Answers 2

up vote 0 down vote accepted

When a script is run, each line is executed one at a time and each must complete before the next one starts. The & character after a command launches the command in the background allowing the next statement to be executed immediately without waiting for the previous one to finish.

So, your script is launching one instance of conky in the background using the config1 file and then launching another instance of conky in the foreground using the config2 file and then waits for that instance to finish before processing the next exit line. If you change your script to have a & after each conky command like this...

#!/bin/bash

conky -c ~/Conky/scripts/config1 &
conky -c ~/Conky/scripts/config2 &

exit

...it will not wait for them to finish before processing the exit command.

As John and Eliah stated, && is used to chain two commands together with the second one executing only if the first one completed successfully. This is generally done from the command line as opposed to a script. This explains why after you made this change the second instance of conky never launched. The first one never finished so that the second could start.

I hope that clears some stuff up.

share|improve this answer
    
Thank You for the detailed explanation. I made the changes and it works perfect. Thanks again. Is there some document, book, or web site that you could recommend for learning basic programming in Linux? –  user18432 Apr 18 '12 at 15:49
    
If you want to learn more about Bash scripting, here is a good beginner-level guide: howtogeek.com/67469/… If you want to learn some more general purpose programming, Python is pretty easy to get started with: en.wikibooks.org/wiki/… –  Ramón Apr 18 '12 at 16:40

What is that & supposed to do? The way you use it it will send conky -c ~/Conky/scripts/config1 in the background.

share|improve this answer
2  
Yes. If your intent was to first run conky -c ~/Conky/scripts/config1 and then if it completes without error run conky -c ~/Conky/scripts/config2 the connector you want to use is && not &. –  irrational John Apr 17 '12 at 16:15
    
Thanks, like I said I really don't know much on writing programs, so I used "&" instead of "&&" out of ignorance. Thanks for the info, I appreciate it. –  user18432 Apr 17 '12 at 18:48
    
OK, when I put "&&" in place of "&" the second part of the script never launches. –  user18432 Apr 17 '12 at 19:06
    
@user18432 If you want to run a 2nd command while the 1st command is still running, then & is what you want. The 1st command does not complete until conky has exited, so if you wanted the 2nd command to run before conky had exited, you could use &&. Then the 2nd command would execute if the first conky instance reported that it had run successfully. But do you really want to run 2 instances of conky at the same time? If you want is to run conky once, with 2 configuration files (config1 and config2), you could try conky -c config1 -c config2 (I'm not sure if that will work). –  Eliah Kagan Apr 17 '12 at 19:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.