ntpdate command not found [only when using with cron]

12,400

Use full path!

/usr/sbin/ntpdate $NTPSERVER

cron doesn't include any ~/.bashrc, ~/.zshrc, etc., so the $PATH maybe unset.

Share:
12,400

Related videos on Youtube

Arun
Author by

Arun

Updated on September 18, 2022

Comments

  • Arun
    Arun over 1 year

    I have a linux machine with ntpdate installed and it is working when i run it from my ssh terminal or from a shell script manually.

    However , when i add the shell script to crontab i get the error

    ntpdate: command not found

    !/bin/bash

    NTPSERVER=192.168.1.192
    LOGPATH="/home/test/" ntpdate -q $NTPSERVER>$LOGPATH/tmp.txt
    RETVAL=$?

    if [ $RETVAL -ne 0 ] ; then
    echo "Failure Unable to query NTP Server :">>$LOGPATH/ntpdebug.txt
    date >>$LOGPATH/ntpdebug.txt
    exit $RETVAL
    fi

    echo "Local Time: ">>$LOGPATH/ntpdebug.txt date >>$LOGPATH/ntpdebug.txt cat $LOGPATH/tmp.txt>>$LOGPATH/ntpdebug.txt

    ntpdate $NTPSERVER
    RETVAL1=$?

    if [ $RETVAL1 -ne 0 ] ; then
    echo "Failure Unable to connect NTP Server :">>$LOGPATH/ntpdebug.txt
    date >>$LOGPATH/ntpdebug.txt
    exit $RETVAL1
    fi

    echo "Synchronized" >>$LOGPATH/ntpdebug.txt

  • mandza
    mandza about 4 years
    There is still folks comming here from google. So ntpdate is deprecated and changed with timedatectl. You can use sudo timedatectl set-ntp on