You are not logged in.

#1 2016-09-20 13:37:43

1Try
Member
Registered: 2016-07-21
Posts: 16

[SOLVED] Bash script for bg execution of cmus not working properly

Hi everybody,
I'm new to bash and i'm trying to write a simple script that takes one argument (a process name) checks if the process is running and, if not, opens it in background.
I thought it was a rather simple task but I can't get my script to work properly with cmus. It opens cmus in background correctly but cpu usage rises to 100% in a second. I've tried to write my script from scratch again in different ways but nothing seems to work.

I even tried to write a single line bash script to open cmus in bakcground like this:

#!/bin/bash
cmus &

But it doesn't work either

However, oddly enough, if i manullay run

cmus & 

from command line, everything works like a charm. Cmus opens up in background with no cpu usage issues.

Any idea?

Last edited by 1Try (2016-09-22 07:45:21)

Offline

#2 2016-09-20 13:53:07

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,739

Re: [SOLVED] Bash script for bg execution of cmus not working properly

What is the name of the script?  Tell me it is not cmus


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#3 2016-09-20 15:15:22

1Try
Member
Registered: 2016-07-21
Posts: 16

Re: [SOLVED] Bash script for bg execution of cmus not working properly

No of course not lol. I called it execbg

Offline

#4 2016-09-20 15:40:18

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,739

Re: [SOLVED] Bash script for bg execution of cmus not working properly

I am not familiar with cmus, but I note that it is an ncurses based music player.  I have seen some bad behavior from ncurses based things (htop) if they are run without a console or terminal window. 

Assuming you urxvt installed, try

#!/bin/bash
urxvt -e cmus &

This runs a terminal window and runs cmus in that window.


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#5 2016-09-20 15:56:29

1Try
Member
Registered: 2016-07-21
Posts: 16

Re: [SOLVED] Bash script for bg execution of cmus not working properly

Thank you very much for your prompt reply
Yes I do use URxvt as default terminal. I tried your solution and what it does is to open a new terminal window and run cmus in that window but not in background. It seems like "&" doesn't work with urxvt. In fact I've also tried:

urxvt &

from command line and it opens a new visible urxvt instance while it should be in the background.

Edit:
After trying https://bbs.archlinux.org/viewtopic.php … 3#p1350213 without success
I've stumbled upon https://felixmilea.com/2014/12/running- … -properly/ which, among all the other solutions to run a background task, suggests the use of an external application called screen which seems to work with cmus.
However I don't really like the idea of relying on an external application to run this apparently simple task

Any other idea?
In the post I linked above (the one from archlinux forum) some user suggested the use of -geometry (I still don't know what it is but I'm going to search it) to move the window of screen which is not exactly what we call an elegant solution but it might work. What do you think about it?

Last edited by 1Try (2016-09-20 16:30:41)

Offline

#6 2016-09-21 09:31:36

ayekat
Member
Registered: 2011-01-17
Posts: 1,589

Re: [SOLVED] Bash script for bg execution of cmus not working properly

I'm wondering: why do you want to run cmus in the background? Isn't the point of ncurses applications to be... interactive?
(of course, I don't know cmus either, so maybe it's a perfectly valid requirement...)

To me this sounds a bit like an XY-problem.

EDIT

1Try wrote:

I've stumbled upon https://felixmilea.com/2014/12/running- … -properly/ which, among all the other solutions to run a background task, suggests the use of an external application called screen which seems to work with cmus.
However I don't really like the idea of relying on an external application to run this apparently simple task

Using screen or tmux to run applications with shortcomings (e.g. hard-coupling an interactive user interface with the main process) in the background is a rather common, though hacky, workaround.
Also, terminal multiplexers are nice - you learn to appreciate them once you start managing remote machines smile

Last edited by ayekat (2016-09-21 09:44:30)


pkgshackscfgblag

Offline

#7 2016-09-21 12:03:33

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,442
Website

Re: [SOLVED] Bash script for bg execution of cmus not working properly

The problem is cmus is reading from stdin without much checking.  Many programs that read from stdin will fail in this way when stdin is closed.  So give it a different stdin.

Also, urxvt is "backgrounded" just fine.  The ampersand at the end doesn't mean a program runs in the "background" of your desktop, it just means that the shell interpreter can move on to the next command while that one is still running.

You don't need to give cmus a graphical terminal - you do need to give it standard stream connections.  The simplest approach that might work:

cmus </dev/null>/dev/null 2>&1 &

But also note that backgrounding from within a script that does nothing else may not have the effect you want.  Again, the amphersand just allows other commands in that script to run: if there are no other commands, it servers no purpose.  If you want the script to finish and the cmus process to stay around, your success may vary widly by using '&', use setsid for that purpose.

EDIT: input from dev/null may actually just make cmus exit immediately - I don't have cmus to test, but this is likely.  You basically need a paused input stream.  One hackish way of doing this in bash:

cmus <(echo -n) & 

Or maybe not ... It seems even "echo" will work there in one test case I have here, though it really shouldn't - so I'm not quite sure what's happening there.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#8 2016-09-21 22:53:31

1Try
Member
Registered: 2016-07-21
Posts: 16

Re: [SOLVED] Bash script for bg execution of cmus not working properly

Thank you all very much for your answers, which by the way gave me a lot of material to work on smile. I managed to achieve the result I wanted!

ayekat wrote:

I'm wondering: why do you want to run cmus in the background? Isn't the point of ncurses applications to be... interactive?

I wanted to run a "window-less" instance of cmus and then control it via cmus-remote commands binded to multimedia keys

ayekat wrote:

IUsing screen or tmux to run applications with shortcomings (e.g. hard-coupling an interactive user interface with the main process) in the background is a rather common, though hacky, workaround.
Also, terminal multiplexers are nice - you learn to appreciate them once you start managing remote machines smile

Nice to know. I've just learnt what a terminal multiplexer is.

@Trilby thanks for your solution. Works perfectly both from command line and script even though I don't really understand how it does what it does. I'll study it and figure it out! Also thank you for explaining the use of the the ampersand. Much clearer now.

By the way @ewaller http://www.catb.org/esr/faqs/smart-questions.html was an interesting and useful reading

Marking the thread as solved.
See you next time!

Offline

#9 2016-09-21 23:04:15

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,739

Re: [SOLVED] Bash script for bg execution of cmus not working properly

1Try wrote:

By the way @ewaller http://www.catb.org/esr/faqs/smart-questions.html was an interesting and useful reading

I'm glad you enjoyed it -- I reread it myself from time to time.
I do note that, unlike some members, I did not feel compelled to point you at it.  Your posts were fine right from the start. big_smile


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#10 2016-09-22 03:31:10

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,442
Website

Re: [SOLVED] Bash script for bg execution of cmus not working properly

1Try wrote:

@Trilby thanks for your solution. Works perfectly both from command line and script even though I don't really understand how it does what it does.

Somewhere in cmus there is a loop that does the following (or at least is a functional equivalent):

char line[MAX_LINE];
while (fgets(line,MAX_LINE,stdin)) {
   /* do stuff with line */
}

This tries to read a line from the standard input and does something with that line, then repeats until fgets returns NULL due to reaching an End Of File or due to an error.  But fgets will read either up to MAX_LINE characters, or until a newline *or* a null byte is reached.

If something is connected to the stdin and not feeding data to the program, fgets just waits.  This is the "normal" condition for a terminal program: they could nearly die of boredome waiting for us to hit enter at the command line to feed them a new line to chew on.

But if there is a steady supply of null bytes, fgets goes haywire.  It repeatedly reads zero-length but otherwise valid strings from the dead input source at the fastest possible rate your CPU can let it get away with (google "broken pipe").  Feeding it an EOF would end the loop without reving up your CPU, but most software also interprets this as being done, and they exit (they read in everything there was to read, they got the last command, whatever).  So you need to feed cmus a steady stream of nothing - not null bytes, but actually nothing.  You want it to sit there quitely waiting for input not gorging itself on nulls.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

Board footer

Powered by FluxBB