Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I compiled a short bash one-liner to focus a running application or launch it if it isn't running:

#!/bin/bash
#intellilaunch.sh

wmctrl -a $1 || $1 & disown
exit 1

The command exits perfectly fine when run directly from the command line:

:~$ wmctrl -a firefox || firefox & disown
[1] 32505

A quick check with the system monitor shows that only firefox is running.

However, when I launch firefox via the script (./intellilaunch.sh firefox) it spawns a persisting new process called intellilaunch.sh firefox, which only exits after closing firefox.

What am I doing wrong?

Edit:

I modified my script according to michas' suggestion:

#!/bin/bash

program=$(basename $1)

if ! wmctrl -a "$program"; then
  "$1"&
fi

Not a one-liner anymore but it works perfectly fine now!

share|improve this question
    
does it work with another program? Eg.: xclock? –  jippie Dec 25 '12 at 22:15
    
@jippie Same exact behaviour: script launches xclock but then stays open instead of exiting; only exits after I close xclock –  Glutanimate Dec 25 '12 at 22:38
    
Strange, I copied your script and works fine here. Only difference is that I didn't use an exit 1 –  jippie Dec 25 '12 at 22:42
    
I just tried it out without exit 1. Same result. How did you launch the script? Did you use ./intellilaunch.sh xclock or something else? –  Glutanimate Dec 25 '12 at 22:53
    
yup. What OS / distribution are you on? –  jippie Dec 25 '12 at 22:57
show 2 more comments

1 Answer

up vote 2 down vote accepted

I cannot reproduce this behavior on my system. From your description is sounds like there is a process not properly set to background.

Try to run as bash -x intellilaunch.sh xclock, this should show, what is going on.

Also || binds stronger than &, therefore you send the whole pipe in background. Maybe an explicit if would be a good idea.

Your

wmctrl -a firefox || firefox & disown ; exit 1

is interpreted as

( wmctrl -a firefox || firefox ) & disown ; exit 1

whereas you probably meant

wmctrl -a firefox || ( firefox & disown ) ; exit 1 

Beause of that, bash will start two jobs one with wmctl and firefox - and another one with disown and exit. As the background job needs a short time to launch it will probably start the commands slighly later, that is why the output of bash -x seems to be in the wrong order.

share|improve this answer
    
Do you really need the disown? Does your script show the same blocking behavior without the disown? –  michas Dec 25 '12 at 23:50
    
bash -x intellilaunch.sh xclock displayed the same behaviour, i.e. the process just wouldn't exit (the disown was superfluous, I think). I followed your suggestion and dropped the || for an if statement (see above). Everything works perfectly fine now. Thank you! –  Glutanimate Dec 26 '12 at 0:09
    
That still leaves the question why it did not work before. - Running bash with -x lets bash print out all the executed commands. What was the output in your case? - Did just leaving out the disown change the situation? –  michas Dec 26 '12 at 0:32
    
I pasted the outputs both with and without disown here. Look as if everything is out of order. Is it due to the ||? –  Glutanimate Dec 26 '12 at 0:45
1  
No, it's because of the &. (See extended answer.) –  michas Dec 26 '12 at 21:35
show 1 more comment

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.