Tiny Core Linux
General TC => General TC Talk => Topic started 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.
-
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.
-
ok, well this is my script;
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.
-
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 )
-
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?
-
You should not run anything in bootlocal.sh that waits for a response unless a response is assured.
-
so what is the proper method to auto run this on boot?
-
Why would you want to?
-
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 &' ?
-
What does it accomplish?
-
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.
-
The script you showed above does nothing but echo to console.
-
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?
-
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..
-
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 ?
-
It is hard to say why an unrevealed script does not run.
-
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
-
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.
-
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)
-
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.
-
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.
-
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.
-
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.
-
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:
#!/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.
-
Better make that /tmp/one, /tmp/two, /tmp/three.
-
Yeah, from a point of keeping the file system uncluttered, that is a cleaner approach.
-
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.
-
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 &
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:
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.