0

I try to run a script that I named script1.sh:

out="/home/me/scripts/out.txt"
sudo ntpdate 99.99.99.99 >> $out

on a timer with the crontab in a Centos 7 operation system. The aim of this is to automatically synchronize the Centos 7 time with a time server.

Using the commands

crontab –e             # (1)
sudo crontab –e        # (2)
sudo vi /etc/crontab   # (3),

I pasted a string of a schedule definition in every configuration file (1), (2), and (3). The string is

* */1 * * *   me   /home/me/scripts/script1.sh

Then I apply the sudo systemctl reload crond.service command to implement the new configurations.

According to the above schedule, the script1.sh must execute every one hour. However, the problem is the ntpdate requires sudo permission and hence the script1.sh does the same thing. Googling this problem, I found that the sudo password requirement could be taken off with editing the sudoer configuration file using the sudo visudo command to start editing. Then I added to the bottom of its some permissions for no password accessing:

me   ALL=(ALL)   NOPASSWD:   /home/me/scripts/*
me   ALL=(ALL)   NOPASSWD:   /usr/sbin/ntpdate

At first it was working when it was defined in all three (1), (2), and (3). Then since I commented the schedule definition in (2) and (3), and reloaded the crontab – it would stop working and start asking the sudo password. Then I returned it back as was, but no effect was watched.

Question

Could you prompt what to do? What thing can be done to get it working?

1 Answer 1

3

Instead of trying to trigger a root-requiring operation through your user's crontab, you should use the system crontab, and then you simply don't need sudo, because things are already running as root.

But in this concrete case, it really seems like you're doing the wrong thing, technically. ntpdate shouldn't be used like that; that's just a guarantee for sudden time jumps and continuously drifting clocks.

So, scrap that script and install chrony, and appropriately configure /etc/chrony.conf, and enable it: sudo systemctl enable --now chronyd. Chrony is exactly the kind of daemon you want to keep your clock synchronous with NTP servers with minimal errors.

You must log in to answer this question.

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