1

I have a virtual server (debian) and the clock fails to sync from time to time (probably because i save/restore state with vboxheadlesstray).

To fix this issue I run dpkg-reconfigure ntp && ntpq -p, it works when I run it as root, but doesn't work with cron. I have added it in crontab -e (as root user) and is using this line:

1 * * * * dpkg-reconfigure ntp && ntpq -p > /dev/null 2>&1

My ordinary user user get mail about it saying /bin/sh: 1: dpkg-reconfigure: not found, why is my ordinary user getting the mail and not root and what do I need to change to make it work?

3 Answers 3

3

In Debian, dpkg-reconfigure is located under /usr/sbin, and root obviously has it in his $PATH, but cron limits $PATH to /usr/bin:/bin, even for root.
See man 5 crontab :

Several  environment  variables  are  set  up automatically by the cron(8) daemon.
SHELL is set to /bin/sh, and LOGNAME and HOME are set from the /etc/passwd line of the crontab's owner.
PATH is set to "/usr/bin:/bin". HOME, SHELL, and PATH may be overridden by settings in the crontab;
LOGNAME is the user that the job is running from, and may not be changed.

So you would have to modify your crontab :

  • giving full path :
    1 * * * * /usr/sbin/dpkg-reconfigure ntp && ntpq -p > /dev/null 2>&1
  • or with modified $PATH :
    PATH=/usr/bin:/bin:/usr/sbin
    1 * * * * dpkg-reconfigure ntp && ntpq -p > /dev/null 2>&1

It would work, but it would not be clean :p
You'd better follow above recommandation, assuming you have a working ntp daemon, or just put that job instead :
10 * * * * /usr/sbin/ntpdate &>/dev/null

3

dpkg-reconfigure is located in /usr/sbin/ on my system:

martin@martin ~ % which dpkg-reconfigure
/usr/sbin/dpkg-reconfigure

Check the PATH variable in your crontab, maybe it's missing the /usr/sbin/ directory?

Also, using dpkg-reconfigure just to force NTP to re-set the time doesn't seem like a particularly good idea, and ntpq -p > /dev/null 2>&1 doesn't seem to do anything useful. According to the manpage, -p just lists the peers, so running that and piping the output to /dev/null should do essentially nothing.

Why don't you just run /etc/init.d/ntp restart > /dev/null from your crontab?

3

Better yet add the following to ntp.conf

tinker panic 0

That way you dont have to restart ntp (which is not good).

2
  • Didn't work. Server clock still unsynced with this.
    – Marwelln
    Jan 27, 2014 at 10:03
  • What does ntpq -p say?
    – dfc
    Jan 27, 2014 at 18:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .