View Single Post
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