LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   init script successfully triggers daemon process but doesn't create pid file -Deb7 (https://www.linuxquestions.org/questions/linux-newbie-8/init-script-successfully-triggers-daemon-process-but-doesnt-create-pid-file-deb7-4175495222/)

linuxecho 02-17-2014 08:16 AM

init script successfully triggers daemon process but doesn't create pid file -Deb7
 
I am trying to use an initscript to run some rsyncing via inotifywait. In true newbie style I have borrowed an initscript of the web and have edited the inputs to suit (though I have read over it to understand how it works).
All is well to the extent that when I start the machine under run levels 2 or 3 the daemon that I have written is indeed triggered as expected – I can verify that if I edit a file in one test folder, it gets rsynced instantly to another folder which is what the daemon is set up to acheive at the minute.
The problem is that this script does not appear to be creating a pid file and hence I cannot stop the service by calling
Code:

sudo /etc/init.d/websites_send_backups stop
(whether I’m root or not. Running that command gives
Quote:

[ok] websites_send_backups process is not running
which is set to be echoed when the pid can’t found and stop is called – and I can verify that the relevant pid file is not in /var/run. I shouldn’t need to start/stop the service that much but I would like to be able to use the start stop syntax rather than searching for the process id using the name and killing from there. I get the same behaviour when I invoke the initscript from a shell in the foreground.

Disclaimer – I’m fresh to shell scripting so all sorts of scripting errors could be under my nose without my necessarily being aware of them.

This is on Debian Wheezy Release 7.4.

What I have tried:
• Reading the daemon log file (couldn’t even find a reference to website_send_backups), did a cat /var/log/daemon.log |grep website on it too to make sure.
• Perusing various other log files just in case again nothing caught my eye.
• Verified the daemon is truly responsible for triggering the inotifywait watch and therefore the initscript must be being invoked as expected by removing the website_send_backups using rc.update and rebooting – upon which as expected the rsyncing on close_write no longer occurs.
• Run ps –ef|grep websites_send_backups and ps –ef|grep websites_send_backupsd to find out what processes have been spawned under that name (see below).
• Checked the options provided by my initscript do indeed appear to be those specified in the manual.
• :Alternating the ‘provides’ line between the daemon name and the initscript name - though I think that’s just what gets printed to the terminal and is therefore more or less irrelevant (just wanted to say I’d edited something since the problem really) .
• Errmm...thats it. Thrown up my hands and started typing this post.

Here’s my init-script and output of ps commands – please let me know if I can usefully post anything else.

Code:

# Provides:          websites_send_backupsd
# Required-Start:    $remote_fs $syslog  rsync
# Required-Stop:   
# Default-Start:    2 3
# Default-Stop:      0 1 4 5 6
# Short-Description: This daemon sends website backup files to where they're needed
# Description:      This daemon uses inotifywait to monitor directories in the background
#                    and rsync them to a target when they are opened for writing and then closed again.
### END INIT INFO
#
# Using the lsb functions to perform the operations.
. /lib/lsb/init-functions
# Process name ( For display )
NAME=websites_send_backups
# Daemon name, where is the actual executable
DAEMON=/home/deploy/websites_send_backupsd
# pid file for the daemon
PIDFILE=/var/run/websites_send_backupsd.pid
#
# If the daemon is not there, then exit.
test -x $DAEMON || exit 5

case $1 in
 start)
  # Checked the PID file exists and check the actual status of process
  if [ -e $PIDFILE ]; then
  status_of_proc -p $PIDFILE $DAEMON "$NAME process" && status="0" || status="$?"
  # If the status is SUCCESS then don't need to start again.
  if [ $status = "0" ]; then
    exit # Exit
  fi
  fi
  # Start the daemon.
  log_daemon_msg "Starting the process" "$NAME"
  # Start the daemon with the help of start-stop-daemon
  # Log the message appropriately
  if start-stop-daemon --start --quiet --oknodo --pidfile $PIDFILE --exec $DAEMON ; then
  log_end_msg 0
  else
  log_end_msg 1
  fi
  ;;
 stop)
  # Stop the daemon.
  if [ -e $PIDFILE ]; then
  status_of_proc -p $PIDFILE $DAEMON "Stoppping the $NAME process" && status="0" || status="$?"
  if [ "$status" = 0 ]; then
    start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE
    /bin/rm -rf $PIDFILE
  fi
  else
  log_daemon_msg "$NAME process is not running"
  log_end_msg 0
  fi
  ;;
 restart)
  # Restart the daemon.
  $0 stop && sleep 2 && $0 start
  ;;
 status)
  # Check the status of the process.
  if [ -e $PIDFILE ]; then
  status_of_proc -p $PIDFILE $DAEMON "$NAME process" && exit 0 || exit $?
  else
  log_daemon_msg "$NAME Process is not running"
  log_end_msg 0
  fi
  ;;
 reload)
  # Reload the process. Basically sending some signal to a daemon to reload
  # it configurations.
  if [ -e $PIDFILE ]; then
  start-stop-daemon --stop --signal USR1 --quiet --pidfile $PIDFILE --name $NAME
  log_success_msg "$NAME process reloaded successfully"
  else
  log_failure_msg "$PIDFILE does not exists"
  fi
  ;;
 *)
  # For invalid arguments, print the usage message.
  echo "Usage: $0 {start|stop|restart|reload|status}"
  exit 2
  ;;
esac



Output of command ps –ef |grep websites_send_backups psd:
Quote:

root 3185 3179 0 13:00 ? 00:00:00 /bin/sh /home/deploy/websites_send_backupsd
root 3187 3185 0 13:00 ? 00:00:00 /bin/sh /home/deploy/websites_send_backupsd
deploy 3513 3464 0 13:02 pts/1 00:00:00 grep websites_send_backupsd
Output of command ps –ef |grep websites_send_backups :
Quote:

root 3179 2046 0 13:00 ? 00:00:00 /bin/sh /etc/init.d/websites_send_backups start
root 3185 3179 0 13:00 ? 00:00:00 /bin/sh /home/deploy/websites_send_backupsd
root 3187 3185 0 13:00 ? 00:00:00 /bin/sh /home/deploy/websites_send_backupsd
deploy 3515 3464 0 13:03 pts/1 00:00:00 grep websites_send_backups

linuxecho 02-17-2014 10:45 AM

Errrm...ahem. I've now read the manual for the start stop daemon tool and it would seem that it doesn't actually create the pidfile unless you specify the -make-pidfile option. Ironic cheers all round. And even if you do use that option, it doesn't actually delete the pidfile when you use the stop command. Sorry, I kind of assumed that this kind of thing was being handled by the start-stop-daemon as default, though on reflection that is a silly assumption, particularly as my init script explicitly handles its removal rather than relying on the start stop daemon for that - I now realise that most processes handle their own pidfile creation and deletion.
However although my init script currently handles the deletion of the pidfile on successful stop, I'm guessing that getting the daemon itself to handle the creation and removal of its pid is the preferred option rather than relying on the start stop daemon and the initscript respectively as its more portable and more reliable.
I don't know how exactly to grab the environment variable from the start-stop-daemon within the daemon itself, but I'll have a look into that.... I suppose I could grep for it or something but I imagine that is a horrible hack and there is a more elegant way...
So I've solved my own non issue again. I'm having a great dialogue with myself here :) Its a real pity I can't mark my own comments as helpful and gain reputation I'd be a guru or something in no time (I'm joking of course, for those of you that may be serious minded - you never know).


All times are GMT -5. The time now is 05:23 AM.