LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Slackware (https://www.linuxquestions.org/questions/slackware-14/)
-   -   calibre startup script (https://www.linuxquestions.org/questions/slackware-14/calibre-startup-script-4175521990/)

micheal.hd 10-13-2014 01:11 PM

calibre startup script
 
I have installed calibre from sbopkg with all dependencies. The program works as a GUI application from a desktop. Program aslo works when I type the service as on cli with same arguments as in my script.
When I execute the script I put together it does not work. It tells me service started but does not start.

Can someone have a look and see why this is not working. I have gotten this far but lack the finishing touch.

Thx

----------------------------------
#!/bin/sh
#
# Create a user with no login
# sudo groupadd calibre
# sudo useradd -g calibre -d /var/lib/calibre -s /bin/false calibre
# sudo usermod -G calibre calibre
# sudo mkdir /var/lib/calibre
# sudo touch /var/lib/calibre/calibre-server-log
# sudo touch /var/lib/calibre/calibre-server-errors
# sudo chown -R calibre:calibre /var/lib/calibre
#
# Install calibre and dependencies using sbopkg from slackbuilds.org
#

CONTENT=/usr/local/pub/calibre # library location
CALIBRE_LOGFILE=/var/lib/calibre/calibre-server-log # log files are good
CALIBRE_ERRORFILE=/var/lib/calibre/calibre-server-errors
PORT=8080 # port to listen on
CALIBRE_USER=calibre # lets run service as a user
DAEMON=/usr/bin/calibre-server # binary file
PIDFILE=/var/run/calibre-server.pid # setup a pid file
ARGS="--auto-reload --with-library=$CONTENT --pidfile=$PIDFILE --port=$PORT --daemonize"

# Need to get the status of the service prior any pending operations
calibre_status() {
if ( ps aux | grep "$DAEMON" |grep -v grep); then
return 0
else
return 3
fi
}

# start the service pending the outcome from the status request
calibre_start() {
calibre_status;
if [ $? = 0 ]; then
echo "Calibre Server is already running..."
exit 1;
fi
echo "Starting Calibre Server..."
su - $CALIBRE_USER -c "$DAEMON $ARGS" > $CALIBRE_LOGFILE 2>$CALIBRE_ERRORFILE &
}

calibre_stop () {
echo -n "Stopping Calibre Server..."
killall $DAEMON
rm $PIDFILE
echo "done"
}

calibre_restart() {
echo "Restarting Calibre Server..."

calibre_status;
if [ $? = 0 ]; then
calibre_stop
sleep 3
calibre_start
else
calibre_start
fi
}

case "$1" in
start)
calibre_start
exit 0
;;
stop)
calibre_stop
exit 0
;;
restart)
calibre_restart
exit 0
;;
status)
calibre_status
if [ $? = 0 ]; then
echo "Calibre Server is already running..."
else
echo "Calibre is not running"
fi
;;
*)
echo "usage: $0 start|stop|restart|status"
exit 1
;;
esac

Gerard Lally 10-13-2014 02:35 PM

Does the calibre user have write permissions on the log and error files, and execute permission on the var/lib/calibre parent directory?

Code:

ls -la /var/lib | grep calibre
ls -la /var/lib/calibre


Alien Bob 10-13-2014 03:43 PM

How do you check that the calibre-server is running?
What do you see when you run the script as "sh -x /path/to/yourscript start" ? Perhaps the script fails halfway.

I use a similar script that starts several calibre content servers, and it has been working for years.

Eric

micheal.hd 10-13-2014 07:01 PM

calibre startup script
 
I had the /var/lib/calibre directory as 0755 and the two log files as 0664.

When I run the script sh -x I get some interesting results:
=====================================================================
Run #1
[michealj@orion run]$ sh -x /etc/rc.d/rc.calibre-server start
+ CONTENT=/usr/local/pub/calibre
+ CALIBRE_LOGFILE=/var/lib/calibre/calibre-server-log
+ CALIBRE_ERRORFILE=/var/lib/calibre/calibre-server-errors
+ PORT=8080
+ CALIBRE_USER=calibre
+ DAEMON=/usr/bin/calibre-server
+ PIDFILE=/var/run/calibre-server.pid
+ ARGS='--auto-reload --with-library=/usr/local/pub/calibre --pidfile=/var/run/calibre-server.pid --port=8080 --daemonize'
+ case "$1" in
+ calibre_start
+ calibre_status
+ ps aux
+ grep /usr/bin/calibre-server
+ grep -v grep
+ return 3
+ '[' 3 = 0 ']'
+ echo 'Starting Calibre Server...'
Starting Calibre Server...
+ exit 0
+ su - calibre -c '/usr/bin/calibre-server --auto-reload --with-library=/usr/local/pub/calibre --pidfile=/var/run/calibre-server.pid --port=8080 --daemonize'
[michealj@orion run]$ /etc/rc.d/rc.calibre-server: line 41: /var/lib/calibre/calibre-server-log: Permission denied

But the permission from the files looked good, RWX on dir and RW on the files. I dont know.

====================================================
Run #2 after I removed the logging from the startup script, I now get another error.
[michealj@orion run]$ sudo sh -x /etc/rc.d/rc.calibre-server start
+ CONTENT=/usr/local/pub/calibre
+ CALIBRE_LOGFILE=/var/lib/calibre/calibre-server-log
+ CALIBRE_ERRORFILE=/var/lib/calibre/calibre-server-errors
+ PORT=8080
+ CALIBRE_USER=calibre
+ DAEMON=/usr/bin/calibre-server
+ PIDFILE=/var/run/calibre-server.pid
+ ARGS='--auto-reload --with-library=/usr/local/pub/calibre --pidfile=/var/run/calibre-server.pid --port=8080 --daemonize'
+ case "$1" in
+ calibre_start
+ calibre_status
+ ps aux
+ grep /usr/bin/calibre-server
+ grep -v grep
+ return 3
+ '[' 3 = 0 ']'
+ echo 'Starting Calibre Server...'
Starting Calibre Server...
+ exit 0
+ su - calibre -c '/usr/bin/calibre-server --auto-reload --with-library=/usr/local/pub/calibre --pidfile=/var/run/calibre-server.pid --port=8080 --daemonize'
[michealj@orion run]$
=========================================================
When I check to see if the server is running:
[michealj@orion run]$ sudo sh -x /etc/rc.d/rc.calibre-server status
+ CONTENT=/usr/local/pub/calibre
+ CALIBRE_LOGFILE=/var/lib/calibre/calibre-server-log
+ CALIBRE_ERRORFILE=/var/lib/calibre/calibre-server-errors
+ PORT=8080
+ CALIBRE_USER=calibre
+ DAEMON=/usr/bin/calibre-server
+ PIDFILE=/var/run/calibre-server.pid
+ ARGS='--auto-reload --with-library=/usr/local/pub/calibre --pidfile=/var/run/calibre-server.pid --port=8080 --daemonize'
+ case "$1" in
+ calibre_status
+ ps aux
+ grep /usr/bin/calibre-server
+ grep -v grep
+ return 3
+ '[' 3 = 0 ']'
+ echo 'Calibre is not running'
Calibre is not running
[michealj@orion run]$
===================================================
I am stumped. Must have something to be with running it as a user. Do you normally run this type of service with root permissions ?

micheal.hd 10-14-2014 05:41 PM

The issue has to be around how I created the "calibre" user and when trying to run the script as that user and not root.

Anyone ?

saulgoode 10-14-2014 08:34 PM

Code:

su - $CALIBRE_USER -c "$DAEMON $ARGS" > $CALIBRE_LOGFILE 2>$CALIBRE_ERRORFILE &
I believe that the problem is the stdout and stderr redirection occurs before the user is switched -- thus the log and errors files are owned by root, not calibre.

Try moving the second quote to the end of the line.

micheal.hd 10-17-2014 07:54 PM

Quote:

Originally Posted by saulgoode (Post 5253874)
Code:

su - $CALIBRE_USER -c "$DAEMON $ARGS" > $CALIBRE_LOGFILE 2>$CALIBRE_ERRORFILE &
I believe that the problem is the stdout and stderr redirection occurs before the user is switched -- thus the log and errors files are owned by root, not calibre.

Try moving the second quote to the end of the line.

I removed the log files completely and tried su - $CALIBRE_USER -c "$DAEMON $ARGS" & but still no joy.

I think maybe I need to delete this user and start again. There something wrong with the user interaction because if I run the script as root #/usr/bin/calibre-server --with-library=/calibre --daemonize it works.
I tried running the script as my normal user and it failed as well. IS there a group the user must be in for this to work ?


All times are GMT -5. The time now is 02:50 AM.