0

Long story short: I accidentally deleted the default content that the 64-bit Ubuntu 20.04 installer had put inside of /etc/crontab during Ubuntu's installation on my computer, so now I'm trying to find out if it's possible to obtain an exact copy of such file from either the interior of the Ubuntu ISO installation file (e.g. a "local repository" such as ubuntu-20.04-amd64.iso) or from a remote repository (e.g. hosted at ubuntu.com or something) so I can put this default crontab file back in /etc.

I mounted the Ubuntu 20.04 ISO and ran sudo ls -lR | grep -i crontab inside of it, but no crontab file was found, thus I imagine that it may be compressed inside one of the files inside of such ISO file, or maybe a system crontab is available for download somewhere at the ubuntu.com network or Launchpad's PPA: I just don't know.


Short story long: While using pipelining with |, in order to append the 1-line output of a command to /etc/crontab, I accidentally typed:

| sudo tee /etc/crontab

...instead of:

| sudo tee -a /etc/crontab

...and therefore ended up replacing the entire content of /etc/crontab with just 1 line, instead of adding such 1 line to the end of such file. Despair!

To my luck, though, this was the first time that I was attempting to modify such file, therefore its content was the default one created by Ubuntu during its installation. But what content is it?

I decided to run info crontab and found what seems to be the same default content of /etc/crontab (but I'm not really sure):

# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d.  These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

Example of job definition:

#.-------------<-------- minute (0 - 59) #| .------------<------- hour (0 - 23) #| | .-----------<------ day of month (1 - 31) #| | | .----------<----- month (1 - 12) OR jan,feb,mar,apr ... #| | | | .---------<---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat #| | | | | .-------<--- user #| | | | | | .--<-- command #| | | | | | | #v v v v v v v 17 * * * * root cd / && run-parts --report /etc/cron.hourly 25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily ) 47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly ) 52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )

I then decided to save the above code in a brand-new /etc/crontab file with owner:group = root:root (sudo chown root:root /etc/crontab) and then restore its permissions with sudo chmod 755 /etc/crontab. After I did this, I also created a backup copy of it with sudo cp /etc/crontab /etc/crontab.bak and also created a skeleton (i.e. template) of it with sudo cp /etc/crontab /etc/skel/crontab...

...but I can't help but wonder if the code above is indeed the exact same one that the Ubuntu 20.04 installer had created inside of /etc/crontab during Ubuntu's installation, nor do I know how to obtain an exact copy of such original/default file (if any is available somewhere).

muru
  • 197,895
  • 55
  • 485
  • 740

2 Answers2

1

on my ubuntu 20.04 desktop the /etc/crontab content is as follows

koen@hpubuntu:/etc$ cat crontab
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

Example of job definition:

.---------------- minute (0 - 59)

| .------------- hour (0 - 23)

| | .---------- day of month (1 - 31)

| | | .------- month (1 - 12) OR jan,feb,mar,apr ...

| | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat

| | | | |

* * * * * user-name command to be executed

17 * * * * root cd / && run-parts --report /etc/cron.hourly 25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily ) 47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly ) 52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )

instead of changing the /etc/crontab file you coudl also consider adding files in some directories

  • a file in /etc/cron.d/ with a cron snippet
  • an executable (script) in the relevant /etc/cron.[hourly |daily |weekly |monthly] directory.
dpkg -S can help to find the package linked to some source
 > dpkg -S "/etc/crontab"
   cron: /etc/crontab

it seems the code can be found here : https://git.launchpad.net/ubuntu/+source/cron/tree/debian/crontab.main

Koen
  • 1,167
  • Thanks for this. Now I have a confirmation that the default /etc/crontab created by Ubuntu does indeed have the same content of the example code shown by info crontab. I wonder, though, if this file is available somewhere for copy/download, or if this info crontab workaround is the only way. – Yuri Sucupira Dec 14 '20 at 10:26
  • From now on, I indeed intend to add executable scripts in the appropriate /etc/cron.hourly|daily|weekly|monthly directory instead of modifying /etc/crontab. It's definitely safer. – Yuri Sucupira Dec 14 '20 at 10:41
1

Provided you have cron installed or at least one package that contains the file, you can find the package with:

$ dpkg -S /etc/crontab
cron: /etc/crontab

next, download and extract the package to a custom folder (The DEBIAN folder is also found in /var/lib/dpkg/info):

mkdir -p cron && \
cd $_ && apt-get download $_ && \
dpkg-deb --raw-extract ${_}*.deb $_

$_ is short for the last argument from the previous command.

  • 2
    Thanks. This answers 100% what I asked, and I also learned something new (I didn't know about the use of the $_ variable). – Yuri Sucupira Dec 14 '20 at 11:26