Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have an init.d script to start crond which specifies the following for start():

start-stop-daemon -S --quiet --make-pidfile --pidfile /var/run/crond.pid --background --exec /usr/sbin/crond

However, the PID is always one number higher than what's recorded in /var/run/crond.pid. Does anyone have any idea what's going on here? I have approximately ten other init.d scripts that also make the same calls, and only cron.d has this issue.

EDIT: This is interesting:

# /usr/sbin/crond &
#
[1]+  Done                       /usr/sbin/crond
# echo $!
737
# ps -eaf | grep crond
738 root     /usr/sbin/crond
740 root     grep crond
#
share|improve this question
    
What distro and what version are we dealing with here? –  slm Jun 3 '13 at 16:32
    
what if you try this : "start-stop-daemon -S --quiet --pidfile /var/run/crond.pid --exec /usr/sbin/crond" –  Raza Jun 3 '13 at 17:10
    
@slm ARM uCLinux (built by Buildroot) –  trycatch Jun 3 '13 at 18:49
    
@Salton - That "works" in that it spawns a crond instance, but creates no pid file. –  trycatch Jun 3 '13 at 18:49
    
Added an interesting test result in an edit... –  trycatch Jun 3 '13 at 18:55

1 Answer 1

up vote 5 down vote accepted

The crond program is designed to be daemon. When it starts, one of the first things it does it fork a child and exit the parent. This is designed for environments where the caller is waiting for the program to exit before proceeding, whereas the daemon needs to continue executing in the background.

caller ---- fork--> wait -------------------------+-> ...
             |                                    |
             '----> exec crond ---- fork--> exit -'
                                     |
                                     '----> read crontab, wait for time of next job, ...

The PID recorded by start-stop-daemon is the PID of the parent. If no other process forks during the short interval between the two forks, the child's PID ends up being the PID of the parent plus one.

Since start-stop-daemon is designed to handle daemons and let them run in the background, tell crond to stay in the foreground, i.e. not to fork at the beginning.

caller ---- fork--> store pid; ...
             |                                    |
             '----> exec crond -f ----> read crontab, wait for time of next job, ...

With BusyBox's crond, pass it the -f option.

start-stop-daemon -S --quiet --make-pidfile --pidfile /var/run/crond.pid --background --exec /usr/sbin/crond -- -f
share|improve this answer
    
Of COURSE. I was just getting on this path when I found the return code was off by one even from the command line. Awesome. –  trycatch Jun 3 '13 at 19:08
    
Thank you Sir Gilles yet again. –  James Andino Oct 7 '13 at 8:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.