07-05-2016, 09:42 AM | #1 |
Bibliothekar
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 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` 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? |
07-05-2016, 06:38 PM | #2 | |
Zealot
Posts: 119
Karma: 64428
Join Date: Aug 2011
Device: none
|
Quote:
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 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. |
|
07-06-2016, 08:25 AM | #3 | |
Bibliothekar
Posts: 38
Karma: 10
Join Date: Jun 2011
Location: San Jose City, Philippines
Device: Galaxy Tab S w/Bookari Premium
|
Quote:
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 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. 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. |
|
07-06-2016, 09:33 AM | #4 |
Grand Sorcerer
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?
|
07-06-2016, 04:35 PM | #5 |
Zealot
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. |
07-07-2016, 01:30 AM | #6 |
Ex-Helpdesk Junkie
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" If you are looking for a list of only the matching processes, then traditionally you use Code:
ps aux|grep "[s]omething" But I'd just check the return code of Code:
pgrep -f calibre > /dev/null 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. |
Tags |
calibre-server, respawning, shell script |
Thread Tools | Search this Thread |
|
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 |