Quote:
Originally Posted by signum
Your "while true" loop will execute forever; there is nothing to stop it or to slow it down. All the script does is fork off processes as fast as it can. Try this, or something like it:
Code:
while true; do
instances=`ps ax | grep "calibre-server" | wc -l`
if [ $instances -eq 0 ]; then
/usr/bin/calibre-server ... (same as above)
fi
sleep 300
done
You want it to run all day, so place the "do while" as the major controlling outside loop.
To do anything interesting, the value of "instances" should change from time to time, so determine it next, inside the loop.
You probably want to use a numeric comparison operator, hence the "-eq" instead of the "==".
Whether or not it starts a server, it should "sleep" for 5 minutes. This can be almost any value you wish, but it should be at least as large as the time it will take for the server to start, otherwise the "ps" command may show zero until the server is running and the script will have already started another server.
|
@signum
No joy.
That suggestion looked great, and made sense. It
seems to be running right, but I still can't connect to the server using it. I added some
echo lines (highlighted below in bold) to flag what's happening so I have some idea of where in the cycle it is, and if it's actually running. Since the server seems to take only a couple seconds to start up, I've reduced the sleep time to 30 seconds.
(The whole command for starting the server is now in another script called from this one, named /usr/bin/startcalibre so I have only one place to make changes, should I need to do so. I made this change
after verifying that the script is still not working right, so it's not the cause.)
Here's the script now:
Code:
echo "Starting script..."
while true; do
instances=`ps ax | grep "calibre-server" | wc -l`
echo "Testing for server state..."
if [ $instances -eq 0 ]; then
/usr/bin/startcalibre ;\
fi
echo Instances of calibre-server running: $instances
echo "Sleeping."
sleep 30
done
Now when I run it here's the output to the terminal:
Code:
behne:/usr/local/bin # /usr/local/bin/keep-calibre-running &
[1] 22715
behne:/usr/local/bin # Starting script...
Testing for server state...
Instances of calibre-server running: 1
Sleeping.
Testing for server state...
Instances of calibre-server running: 1
Sleeping.
Testing for server state...
Instances of calibre-server running: 1
Sleeping.
Each of the several loops is exactly 30 seconds apart, indicating that the sleep cycle is correct, and it's looping properly. Note that although it
seems to be functioning properly, and reports that one instance of the server is running, the web browser reports that there's nothing to connect to: "Firefox can't establish a connection to the server at behne.ddns.net:8787."
If I start the content server manually with the same parameters, no problem. But the server doesn't want to work from this script, and I don't know enough to figure it out. I'm stumped.