Tiny Core Linux

General TC => General TC Talk => Topic started by: mb0 on February 07, 2012, 06:06:34 PM

Title: scripting q-- wait for response?
Post by: mb0 on February 07, 2012, 06:06:34 PM
How would i delay/sleep a bash script to wait until a command has given a return? For example, the 'mpc idle' command only returns when something changes, driven by external events. So i don't think it works the same way as waiting for a program to complete.
Title: Re: scripting q-- wait for response?
Post by: gerald_clark on February 07, 2012, 06:33:30 PM
That is the default behavior.
If you want a script to start a program and NOT wait for it to terminate, you must use '&' to run it in background.
Title: Re: scripting q-- wait for response?
Post by: mb0 on February 07, 2012, 07:16:58 PM
ok, well this is my script;

Code: [Select]
while true
do
        read line < /usr/local/bin/mpc idle
        if [ $line == "player" ]; then
                sudo echo 'not'
        else
                sudo echo 'idle'
        fi
        sleep 0.2
done

and it echo's 'idle' every .2 seconds. (NB without else or sleep it doesn't do anything, even with external events firing)
in case you are not familiar; 'mpc idle' will return 'player' when an external event changes something with mpd. Until that, it doesn't return anything.
Title: Re: scripting q-- wait for response?
Post by: gerald_clark on February 07, 2012, 07:52:21 PM
This really is not a bash forum, but

read line < /usr/local/bin/mpc idle
tries to read from the file /usr/local/bin/mpc

Try
line=`/usr/local/bin/mpc idle`
or
line=$( /usr/local/bin/mpc idle )
Title: Re: scripting q-- wait for response?
Post by: mb0 on February 11, 2012, 02:28:18 PM
Well thanks for replying anyway. I would urge a general linux sub-forum to filter out these questions from those who don't want to see them.

I have a slightly more tinycore related follow up.. if i try and run this script in eg. bootlocal.sh, it doesn't work. However if i run it after, it does. How can i get it to work automatically on boot?
Title: Re: scripting q-- wait for response?
Post by: gerald_clark on February 11, 2012, 02:46:21 PM
You should not run anything in bootlocal.sh that waits for a response unless a response is assured.
Title: Re: scripting q-- wait for response?
Post by: mb0 on February 11, 2012, 03:53:40 PM
so what is the proper method to auto run this on boot?
Title: Re: scripting q-- wait for response?
Post by: gerald_clark on February 11, 2012, 03:59:46 PM
Why would you want to?
Title: Re: scripting q-- wait for response?
Post by: mb0 on February 11, 2012, 04:35:17 PM
because it is meant to be headless. And I still don't understand why this doesn't work by saving it as a script and then calling '/opt/path/script.sh &' ?
Title: Re: scripting q-- wait for response?
Post by: gerald_clark on February 11, 2012, 04:39:51 PM
What does it accomplish?
Title: Re: scripting q-- wait for response?
Post by: mb0 on February 11, 2012, 04:59:00 PM
I'm  not quite sure why it matters... i'm sure i need it.. but anyway; To provide an update when there is an event from mpc. For example, to update an LCD.
Title: Re: scripting q-- wait for response?
Post by: gerald_clark on February 11, 2012, 05:24:57 PM
The script you showed above does nothing but echo to console.
Title: Re: scripting q-- wait for response?
Post by: mb0 on February 11, 2012, 05:34:55 PM
Yes i'm aware of that, it was a non complete example. Obviously i replace the echo with other code. Like i said, it works when i launch it myself. Now, like i asked, how do i get it to run automatically on boot?
Title: Re: scripting q-- wait for response?
Post by: coreplayer2 on February 11, 2012, 05:51:26 PM
Why don't you make an extension of the script, then add a file to X.d which starts the script on each boot?

I think instructions are in the wiki or Q&A section..
Title: Re: scripting q-- wait for response?
Post by: mb0 on February 11, 2012, 06:02:56 PM
Do i need to create an extension?? The script is persistant in /opt. I guess i would be using init.d instead of x.d since i am not using the gui ??

I would still also like to know *why* it doesn't work just running 'script.sh &' style from another script like bootlocal.sh ?
Title: Re: scripting q-- wait for response?
Post by: gerald_clark on February 11, 2012, 06:34:49 PM
It is hard to say why an unrevealed script does not run.
Title: Re: scripting q-- wait for response?
Post by: mb0 on February 11, 2012, 07:04:43 PM
so you are saying there is no reason  to expect a script not to be launched fine from bootlocal.sh?#

e: here is an example that doesn't work; (remember, which works when ran manually)

#!/bin/sh
while true
do
        /usr/local/bin/mpc idle > /dev/null
        LINE=`/usr/local/bin/curl -s 127.0.0.1/test`
        curl -F "var=$LINE" 127.0.0.1:5678
done

saved as /opt/script.sh, with '/opt/script.sh &' in bootlocal.sh
Title: Re: scripting q-- wait for response?
Post by: gerald_clark on February 11, 2012, 07:14:26 PM
If it is executable, and you call it from bootlocal.sh, it will run.
It is your responsibility to insure that it runs to completion an does not hang on a line such as
/usr/local/bin/mpc idle > /dev/null.

bootlocal.sh runs as root without a stdin, so any program requiring keyboard input will fail.
It also runs files synchronously, so any program that does not return will hang bootlocal.sh.
Title: Re: scripting q-- wait for response?
Post by: mb0 on February 11, 2012, 07:17:29 PM
again, how would i run this script on startup??

(and FWIW, i thought the & addition would stop it from blocking?.. as well as this i had it at the end of the scipt.
FYI there is no keyboard input required)
Title: Re: scripting q-- wait for response?
Post by: gerald_clark on February 11, 2012, 07:30:14 PM
It is your script.
You will have to figure out why it is not running as expected.
I cannot troubleshoot secret scripts, and have lost interest.
Title: Re: scripting q-- wait for response?
Post by: mb0 on February 11, 2012, 07:35:28 PM
Excuse me, i posted my script just two posts ago. It runs as expected when i run it manually. Therefore, there is not a fundamental problem with the script.
Title: Re: scripting q-- wait for response?
Post by: Rich on February 11, 2012, 08:15:59 PM
Hi mb0
I don't know what the problem is, but I will make a couple of comments.
1. Are you sure  it's not  running? If you start  top  or execute  ps, does it show up?
2. I don't know what those commands do,  but if their execution time is short, you will suck up a lot of CPU cycles.
    If that's the case, it might be slowing down another process that is trying to start up, maybe one you are relying on.
Title: Re: scripting q-- wait for response?
Post by: mb0 on February 11, 2012, 08:21:02 PM
Thank you for your reply rich. The execution time is not short. often minutes. the rest of the startup seems fine. i will look into your first point tomorrow.
Title: Re: scripting q-- wait for response?
Post by: Rich on February 11, 2012, 08:32:39 PM
Hi mb0
When troubleshooting a problem it can be helpful to have feedback. Since you are backgrounding the script, I
don't know if an echo command will work, but this will:
Code: [Select]
#!/bin/sh
while true
do
        /usr/local/bin/mpc idle > /dev/null
        touch one
        LINE=`/usr/local/bin/curl -s 127.0.0.1/test`
        touch two
        curl -F "var=$LINE" 127.0.0.1:5678
        touch three
done
If the script ran, ls will return the files one,two, and three. If you delete them and the script is still running, they will
be recreated.
Title: Re: scripting q-- wait for response?
Post by: gerald_clark on February 11, 2012, 09:21:48 PM
Better make that /tmp/one, /tmp/two, /tmp/three.
Title: Re: scripting q-- wait for response?
Post by: Rich on February 11, 2012, 09:40:55 PM
Yeah, from a point of keeping the file system uncluttered, that is a cleaner approach.
Title: Re: scripting q-- wait for response?
Post by: maro on February 11, 2012, 10:09:19 PM
Yeah, from a point of keeping the file system uncluttered, that is a cleaner approach.
Well, that as well as the fact that it could be quite a bit of guesswork to find out in which local directory a script was executed.

My general rule of thumb for trouble shooting a script that "the system" starts (e.g. one called by 'crobtab'): I'd tend to use pretty much only absolute file names. After I've identified what typically was a wrong assumption on my part I remove any surplus use of  these absolute file names again, as they are IMHO not a "good look" in general terms.
Title: Re: scripting q-- wait for response?
Post by: H.Lunke on February 12, 2012, 10:15:17 AM
The execution time is not short. often minutes. the rest of the startup seems fine.

Hi mb0,

'mpc' does not return until an event is generated, such as changing the playlist, pausing the player and so on. So the following works fine even when run from bootlocal.sh via: /path/to/script.sh &

Code: [Select]
while true;do echo `date;mpc idle`;done >/tmp/logfile
After reboot you should be able to watch the growing logfile via: "tail -f /tmp/logfile" - sure there should be some player activities ongoing..

You could also write the loop as follows:
Code: [Select]
while echo `date;mpc idle`;do mpc >/tmp/logplay;done >/tmp/logevent
This way "/tmp/logplay" contains the song currently played (if any) and "/tmp/logevent" contains status change messages.