Register Guidelines E-Books Search Today's Posts Mark Forums Read

Go Back   MobileRead Forums > E-Book Software > Calibre > Devices

Notices

Reply
 
Thread Tools Search this Thread
Old 07-05-2016, 09:42 AM   #1
Ersatzreifen
Bibliothekar
Ersatzreifen began at the beginning.
 
Ersatzreifen's Avatar
 
Posts: 38
Karma: 10
Join Date: Jun 2011
Location: San Jose City, Philippines
Device: Galaxy Tab S w/Bookari Premium
Need help with Unix shell script for my calibre-server

I'm trying to use a shell script to start my calibre-server, and to respawn it if it crashes (which it does frequently.)

Here's the script so far:

Code:
#! /bin/bash
# $Author$
# $Date$
# $Header$
# $Id$
# $Locker$
# $Name$
# $RCSfile$
# $Revision$
# $Source$
# $State$
# $Log$
# 
# Program:  /usr/local/keep-calibre-server-running
# Program Homepage: None
# Author: Russell W. Behne (rwbehne1@gmail.com)
# Author's Homepage: Homepage: http://behne.ddns.net
# 
# For use with: calibre-server
# Purpose: To start then respawn calibre-server when it crashes.
# Usage: Call from an entry in /etc/init.d/after.local: /usr/local/keep-calibre-server-running &

instances=`ps ax | grep "calibre-server"| grep -v grep | wc -l`

if [ $instances == 0 ]; then
  while true; \
    do /usr/bin/calibre-server --daemonize --port 8787 --with-library /home/Calibre/MASTER ; \
    done
  else
    exit 1
fi
It doesn't seem to be working, so I wrote the following script to test to quickly see if the server is running, and how many instances:

Code:
# !/bin/bash
# $Author$
# $Date$
# $Header$
# $Id$
# $Locker$
# $Name$
# $RCSfile$
# $Revision$
# $Source$
# $State$
# $Log$
# 
# Program:  /usr/local/is-calibre-running
# Program Homepage: None
# Author: Russell W. Behne (rwbehne1@gmail.com)
# Author's Homepage: Homepage: http://behne.ddns.net
# 
# For use with: calibre-server respawning script named "keep-calibre-running"
# Purpose: To test to see if calibre-server is running, and how many instances there are.
# Usage: Call from the command line with is-calibre-running

echo Instances of calibre-server running: `ps ax | grep "calibre-server"| grep -v grep | wc -l`
After starting the keep-calibre-server-running script calibre-server should be running just 1 instance.

Next, I run the is-calibre-running script to see what it says, and it tells me that there are multiple instances of the server running (up to about 60!) and the number changes each time I run that script.

This is telling me that for some reason more than one instance is starting, and they are crashing - respawning too fast. I don't understand what's going wrong, or why it's spawning multiple instances, too fast. It should spawn only one instance, then do nothing until the server stops for whatever reason, then it should loop back and respawn only one new instance, in an endless loop, ad nauseaum. Unfortunately the script doesn't seem to be stopping and waiting when it should be.

Can anyone help get this working?
Ersatzreifen is offline   Reply With Quote
Old 07-05-2016, 06:38 PM   #2
signum
Zealot
signum calls his or her ebook reader Vera.signum calls his or her ebook reader Vera.signum calls his or her ebook reader Vera.signum calls his or her ebook reader Vera.signum calls his or her ebook reader Vera.signum calls his or her ebook reader Vera.signum calls his or her ebook reader Vera.signum calls his or her ebook reader Vera.signum calls his or her ebook reader Vera.signum calls his or her ebook reader Vera.signum calls his or her ebook reader Vera.
 
Posts: 119
Karma: 64428
Join Date: Aug 2011
Device: none
Quote:
Originally Posted by Ersatzreifen View Post
I'm trying to use a shell script to start my calibre-server, and to respawn it if it crashes (which it does frequently.)

Here's the script so far:

Code:
#! /bin/bash

instances=`ps ax | grep "calibre-server"| grep -v grep | wc -l`

if [ $instances == 0 ]; then
  while true; \
    do /usr/bin/calibre-server --daemonize --port 8787 --with-library /home/Calibre/MASTER ; \
    done
  else
    exit 1
fi
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 is offline   Reply With Quote
Old 07-06-2016, 08:25 AM   #3
Ersatzreifen
Bibliothekar
Ersatzreifen began at the beginning.
 
Ersatzreifen's Avatar
 
Posts: 38
Karma: 10
Join Date: Jun 2011
Location: San Jose City, Philippines
Device: Galaxy Tab S w/Bookari Premium
Unhappy

Quote:
Originally Posted by signum View Post
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.
Ersatzreifen is offline   Reply With Quote
Old 07-06-2016, 09:33 AM   #4
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 11,703
Karma: 6658935
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Why not use something like monit, which is made for this sort of thing?
chaley is offline   Reply With Quote
Old 07-06-2016, 04:35 PM   #5
signum
Zealot
signum calls his or her ebook reader Vera.signum calls his or her ebook reader Vera.signum calls his or her ebook reader Vera.signum calls his or her ebook reader Vera.signum calls his or her ebook reader Vera.signum calls his or her ebook reader Vera.signum calls his or her ebook reader Vera.signum calls his or her ebook reader Vera.signum calls his or her ebook reader Vera.signum calls his or her ebook reader Vera.signum calls his or her ebook reader Vera.
 
Posts: 119
Karma: 64428
Join Date: Aug 2011
Device: none
OK, my bad. You'll have to put the "grep -v grep" back into the calculation of "instances".

Sometime during the sleep period, type in "ps axu" and note what user ids and process ids are in use for "calibre-server". Might be useful later.
signum is offline   Reply With Quote
Old 07-07-2016, 01:30 AM   #6
eschwartz
Ex-Helpdesk Junkie
eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.
 
eschwartz's Avatar
 
Posts: 19,422
Karma: 85397180
Join Date: Nov 2012
Location: The Beaten Path, USA, Roundworld, This Side of Infinity
Device: Kindle Touch fw5.3.7 (Wifi only)
You should take a live look at the processes using (h)top.

...

Code:
ps aux|grep "something"
will often discover the grep process itself, depending on the timing.
If you are looking for a list of only the matching processes, then traditionally you use
Code:
ps aux|grep "[s]omething"
since the regex doesn't match itself.

But I'd just check the return code of
Code:
pgrep -f calibre > /dev/null
rather than testing the integer value of three different commands in a pipe, ending with "wc".

Yes, the "if" builtin doesn't perform comparisons, it checks the return value of the "[" binary (alias for "test"). Make use of that fact to cut down on the rube goldberg code.

...

And as chaley said, you really should stick with software which is actually designed to restart unexpectedly-killed daemons. Monit is a great choice for that, but any decent init system should be able to handle that to some degree.

Last edited by eschwartz; 07-07-2016 at 01:38 AM.
eschwartz is offline   Reply With Quote
Reply

Tags
calibre-server, respawning, shell script

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script Kindle 4nt nixlos Kindle Developer's Corner 3 08-12-2014 12:46 PM
Does anyone have a launchd script for calibre-server? offby1 Calibre 8 06-06-2014 08:04 AM
invoking sql in a shell script Mingyar Kobo Developer's Corner 17 05-31-2013 06:50 PM
Epub creation in unix shell SBT ePub 11 12-13-2011 01:02 PM


All times are GMT -4. The time now is 06:40 AM.


MobileRead.com is a privately owned, operated and funded community.