Friday, 17 September 2010

Getting Calibre Server to run at Boot in Ubuntu 10.04

Calibre is a multi-platform eBook management software which supports multiple eBook reader and formats.

It also contains a simple eBook distribution server which can be accessed both via a web browser and an OPDS compatible client.

In this article I will set-up the Calibre server to start at boot time and serve books.

Even though Ubuntu 10.4 has Calibre in its repositories, I prefer to download and install the latest version as Calibre updates quite often with both compatibility fixes and new functionality.

The following procedure can be completely done over command line, on a headless system.

To do this, enter the following in a terminal:
 sudo apt-get install xdg-utils  
 sudo python -c "import urllib2; exec urllib2.urlopen('http://status.calibre-ebook.com/linux_installer').read(); main()"  

And follow the on-screen instructions. In case something goes wrong, go here and see if the instructions have changed.

There has been a report (by Emil in the comments) that the following are also needed in Ubuntu 10.10:
 sudo apt-get install xdg-utils imagemagick python-imaging python-mechanize python-lxml python-dateutil python-cssutils python-beautifulsoup python-dnspython python-poppler libpodofo-utils libwmf-bin python-chm   

After installing Calibre, it is time to set it up for boot start up.

Enter the following:
 cd /etc/init.d  
 sudo pico calibre  

In the editor which opens up, copy and paste the following script, making changes as needed (check the top 20-30 lines). UPDATE: if you are sing Calibre 0.7.27+ you need the script from here instead.


 #! /bin/sh  
 ### BEGIN INIT INFO  
 # Provides: calibre  
 # Required-Start: network  
 # Required-Stop:  
 # Default-Start: 2 3 4 5  
 # Default-Stop: 0 1 6  
 # Short-Description: Controls the Calibre content server  
 # Description: Controls the Calibre content web server  
 #  
 ### END INIT INFO  
 # Author: Alexandrdos Schillings  
 #  
 # Please remove the "Author" lines above and replace them  
 # with your own name if you copy and modify this script.  
 # Do NOT "set -e"  
 # PATH should only include /usr/* if it runs after the mountnfs.sh script  
 #  
 #  
 PATH=/sbin:/usr/sbin:/bin:/usr/bin  
 DESC="Calibre Content Server"  
 NAME=calibre-server  
 DAEMON=/usr/bin/$NAME  
 PIDFILE=/var/run/$NAME.pid  
 # Edit the CONTENT variable below to point it to your library directory.  
 # Edit the PORT variable below to change the port the server will run on.  
 # Edit the MAX_COVER variable below to change the maximum size of a book cover image.  
 CONTENT=/home/alex/Documents/Calibre  
 PORT=8080  
 MAX_COVER=600x800  
 # The --develop flag refreshes the server on a database change.  
 DAEMON_ARGS="--develop --with-library=$CONTENT --pidfile=$PIDFILE --port=$PORT --max-cover=$MAX_COVER --daemonize"  
 SCRIPTNAME=/etc/init.d/$NAME  
 # Exit if the package is not installed  
 [ -x "$DAEMON" ] || exit 0  
 # Read configuration variable file if it is present  
 [ -r /etc/default/$NAME ] && . /etc/default/$NAME  
 # Load the VERBOSE setting and other rcS variables  
 . /lib/init/vars.sh  
 # Define LSB log_* functions.  
 # Depend on lsb-base (>= 3.0-6) to ensure that this file is present.  
 . /lib/lsb/init-functions  
 #  
 # Function that starts the daemon/service  
 #  
 do_start()  
 {  
 # Return  
 # 0 if daemon has been started  
 # 1 if daemon was already running  
 # 2 if daemon could not be started  
 start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null || return 1  
 start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_ARGS || return 2  
 }  
 #  
 # Function that stops the daemon/service  
 #  
 do_stop()  
 {  
 # Return  
 # 0 if daemon has been stopped  
 # 1 if daemon was already stopped  
 # 2 if daemon could not be stopped  
 # other if a failure occurred  
 start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE  
 RETVAL="$?"  
 [ "$RETVAL" = 2 ] && return 2  
 # Wait for children to finish too if this is a daemon that forks  
 # and if the daemon is only ever run from this initscript.  
 # If the above conditions are not satisfied then add some other code  
 # that waits for the process to drop all resources that could be  
 # needed by services started subsequently. A last resort is to  
 # sleep for some time.  
 start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON  
 [ "$?" = 2 ] && return 2  
 # Many daemons don't delete their pidfiles when they exit.  
 rm -f $PIDFILE  
 return "$RETVAL"  
 }  
 #  
 # Function that sends a SIGHUP to the daemon/service  
 #  
 case "$1" in  
 start)  
 [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"  
 do_start  
 case "$?" in  
 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;  
 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;  
 esac  
 ;;  
 stop)  
 [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"  
 do_stop  
 case "$?" in  
 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;  
 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;  
 esac  
 ;;  
 status)  
 status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?  
 ;;  
 restart)  
 #  
 # If the "reload" option is implemented then remove the  
 # 'force-reload' alias  
 #  
 log_daemon_msg "Restarting $DESC" "$NAME"  
 do_stop  
 case "$?" in  
 0|1)  
 do_start  
 case "$?" in  
 0) log_end_msg 0 ;;  
 1) log_end_msg 1 ;; # Old process is still running  
 *) log_end_msg 1 ;; # Failed to start  
 esac  
 ;;  
 *)  
 # Failed to stop  
 log_end_msg 1  
 ;;  
 esac  
 ;;  
 *)  
 #echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2  
 echo "Usage: $SCRIPTNAME {start|stop|status|restart}" >&2  
 exit 3  
 ;;  
 esac  
 :  

Save the file, and enter the following:

 sudo chown root calibre  
 sudo chgrp root calibre  
 sudo chmod 755 calibre  
 sudo insserv calibre  
 update-rc.d calibre defaults   

This will set Calibre to start at boot time, and serve books from the directory which was given in the script.

To manually start/stop the server. use the following:
 sudo /etc/init.d/calibre start  
 sudo /etc/init.d/calibre stop  

The server can now be accessed by entering the following in a web-browser:
 http://<device_ip>:<port_set_in_script>  

For OPDS access, you will need to modify the URL as follows:
 http://<device_ip>:<port_set_in_script>/opds/  

8 comments:

  1. Thanks!

    worked in Ubuntu server 10.10 base install but there were a couple of warnings when it upgraded the start script to upstart.

    To get Calibre working I had to install these packages as well:

    sudo apt-get install xdg-utils imagemagick python-imaging python-mechanize python-lxml python-dateutil python-cssutils python-beautifulsoup python-dnspython python-poppler libpodofo-utils libwmf-bin python-chm

    Also, Calibre throws a segmentation fault some times but it seems to work.

    ReplyDelete
  2. Hi Emil,

    Thanks for the feedback, I'll update the article.

    Have a look to see if this update fixes the segmentation faults: http://alt236.blogspot.com/2010/11/autostart-script-changes-for-calibre.html#more

    ReplyDelete
  3. nice information,
    thanks

    It's work in my ubuntu server 10.10 (I used calibre-0.8.26)
    :)

    ReplyDelete
  4. Thank You! This worked perfect for me.

    ReplyDelete
  5. Hi, I followed the instructions and calibre-server is now opening fine in my browser. However, I'm getting any library content to show. I've checked the library path and verified that it does contain my test ebook + metadata.db files (copied from another Calibre installed on another machine.)

    Error: No books found

    Object.createException (http://192.168.1.20:8081/static/stacktrace.js:81:18)

    Object.run (http://192.168.1.20:8081/static/stacktrace.js:66:25)

    printStackTrace (http://192.168.1.20:8081/static/stacktrace.js:57:62)

    render_error (http://192.168.1.20:8081/static/browse/browse.js:134:18)

    booklist (http://192.168.1.20:8081/static/browse/browse.js:271:29)

    HTMLDocument. (http://192.168.1.20:8081/browse/category/allbooks:33:17)

    Function.ready (http://192.168.1.20:8081/static/jquery.js:392:9)

    HTMLDocument. (http://192.168.1.20:8081/static/jquery.js:745:10)

    ReplyDelete
  6. hi,
    I find the same as the issuer of the remark of 4 March 2012.
    Using calibre 1.1.0.

    The default folder to hold the books is /root/Calibre Library/
    The CONTENT variable has been edited to: CONTENT=/root/Calibre\ Library
    Still the above error appears in the browser when tryin to view the books.

    Any suggestions?

    ReplyDelete
  7. Hello,

    On kubuntu 14.04, the line

    sudo insserv calibre

    returned an error message (insserv not found)

    So, I just skipped it and, well, it works.

    just to help.

    Regards.

    ReplyDelete