Editing crontab by script doesn't work

Bug #1854891 reported by Jeremy Phelps on 2019-12-03
This bug affects 1 person
Affects Status Importance Assigned to Milestone
cron (Ubuntu)

Bug Description

I wrote a script to automatically edit a crontab across a fleet of machines.
The script works something like this:

   ex $crontab << EOT
    <ex commands to effect the changes I want>

I set EDITOR to point at that script, and then run "crontab -e", only to find that the program says "No modification made". I carefully debugged my script to ensure that it was actually making a change. In spite of the change, crontab(1) claims "No modification made" and rejects the changes.

After other attempts at troubleshooting failed, I delved into the source code. The "No modification made" error is reported after "crontab" checks the file modification time, which it does both before and after calling $EDITOR.

The check is done like this:

            if (statbuf.st_mtime == fsbuf->st_mtime) {
                    return 1; /* No change to file */

According to the stat(2) Linux man page, "st_mtime" is actually a macro defined as:

    #define st_mtime st_mtim.tv_sec

So it seems that the problem is that editing a file by script takes less than a second, and this method of checking a file's modification time only provides 1-second resolution.

It seems that attempting to move away from using the st_mtime field would introduce some portability problems, since Linux and MacOS X provide incompatible ways to get subsecond precision on a file's modification time.

In the meantime, I will call sleep(1) from my script to ensure that enough time passes that crontab can see that there was a change.

To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers