Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 02-17-2014, 08:16 AM   #1
LQ Newbie
Registered: Feb 2014
Posts: 8

Rep: Reputation: Disabled
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
sudo /etc/init.d/websites_send_backups stop
(whether I’m root or not. Running that command gives
[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.

# 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.
# Using the lsb functions to perform the operations.
. /lib/lsb/init-functions
# Process name ( For display )
# Daemon name, where is the actual executable
# pid file for the daemon
# If the daemon is not there, then exit.
test -x $DAEMON || exit 5

case $1 in
  # 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
  # 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
   log_end_msg 1
  # 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
   log_daemon_msg "$NAME process is not running"
   log_end_msg 0
  # Restart the daemon.
  $0 stop && sleep 2 && $0 start
  # Check the status of the process.
  if [ -e $PIDFILE ]; then
   status_of_proc -p $PIDFILE $DAEMON "$NAME process" && exit 0 || exit $?
   log_daemon_msg "$NAME Process is not running"
   log_end_msg 0
  # 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"
   log_failure_msg "$PIDFILE does not exists"
  # For invalid arguments, print the usage message.
  echo "Usage: $0 {start|stop|restart|reload|status}"
  exit 2

Output of command ps –ef |grep websites_send_backups psd:
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 :
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
Old 02-17-2014, 10:45 AM   #2
LQ Newbie
Registered: Feb 2014
Posts: 8

Original Poster
Rep: Reputation: Disabled
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).

Last edited by linuxecho; 02-17-2014 at 11:12 AM.
1 members found this post helpful.


daemon, init script, pid, start, stop

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Script to check PID from file and check whether process is running or not rajkiran183 Linux - Newbie 5 10-19-2012 11:28 AM
init script (stop) doesn't remove process IDs tdnnash25 Linux - Server 3 12-23-2009 09:01 PM
CentOS 4 (Fedora Core) init script - create PID file cejennings_cr Linux - Newbie 1 07-10-2006 08:22 AM
starting script, init by copy/create file in (samba) folder? muab Linux - General 7 06-22-2005 06:02 PM
How can I create a pid file from a backgournd process that I start? Hackiller Linux - General 3 05-12-2004 02:37 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:47 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration