AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1299997
Accepted
Yuri Sucupira
Yuri Sucupira
Asked: 2020-12-15 02:12:27 +0800 CST2020-12-15 02:12:27 +0800 CST 2020-12-15 02:12:27 +0800 CST

如何从任何官方(本地或远程)存储库中获取 Ubuntu 默认 /etc/crontab 文件的精确副本?[复制]

  • 772
这个问题在这里已经有了答案:
如何恢复配置文件? (7 个回答)
1 年前关闭。

长话短说:我不小心删除了 64 位 Ubuntu 20.04 安装程序在我的计算机上安装 Ubuntu 期间放入的默认内容/etc/crontab,所以现在我试图找出是否有可能从Ubuntu ISO 安装文件的内部(例如“本地存储库”,例如ubuntu-20.04-amd64.iso)或远程存储库(例如托管在 ubuntu.com 或其他地方),因此我可以将此默认crontab文件放回/etc.

我安装了 Ubuntu 20.04 ISO 并在其中运行sudo ls -lR | grep -i crontab,但没有crontab找到文件,因此我想它可能被压缩在此类 ISO 文件中的一个文件中,或者系统 crontab可以在 ubuntu 的某个地方下载.com 网络或 Launchpad 的 PPA:我只是不知道。


长话短说:在使用流水线时|,为了将命令的 1 行输出附加到/etc/crontab,我不小心输入了:

| sudo tee /etc/crontab

...代替:

| sudo tee -a /etc/crontab

...因此最终/etc/crontab仅用 1 行替换了 的全部内容,而不是将这样的 1 行添加到此类文件的末尾。绝望!

不过,幸运的是,这是我第一次尝试修改此类文件,因此其内容是 Ubuntu 在安装过程中创建的默认内容。但它的内容是什么?

我决定运行info crontab并发现似乎是相同的默认内容/etc/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
#| | | | |  |     .--<-- 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 )
#

/etc/crontab然后我决定用 owner:group = root:root ( sudo chown root:root /etc/crontab)将上面的代码保存在一个全新的文件中,然后用sudo chmod 755 /etc/crontab. 在我这样做之后,我还创建了它的备份副本,sudo cp /etc/crontab /etc/crontab.bak并创建了它的骨架(即模板)sudo cp /etc/crontab /etc/skel/crontab......

...但我不禁想知道上面的代码是否确实与 Ubuntu 20.04 安装程序/etc/crontab在 Ubuntu 安装期间在其中创建的代码完全相同,我也不知道如何获取此类原始/默认文件的精确副本(如果有的话)。

default cron restore etc
  • 2 2 个回答
  • 587 Views

2 个回答

  • Voted
  1. Koen
    2020-12-15T02:17:41+08:002020-12-15T02:17:41+08:00

    在我的 ubuntu 20.04 桌面上 /etc/crontab 内容如下

    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 )
    #
    

    除了更改 /etc/crontab 文件,您还可以考虑在某些目录中添加文件

    • /etc/cron.d/ 中带有 cron 片段的文件
    • 相关 /etc/cron.[hourly |daily |weekly |monthly] 目录中的可执行文件(脚本)。
    dpkg -S can help to find the package linked to some source
     > dpkg -S "/etc/crontab"
       cron: /etc/crontab
    

    似乎代码可以在这里找到:https ://git.launchpad.net/ubuntu/+source/cron/tree/debian/crontab.main

    • 1
  2. Best Answer
    user986805
    2020-12-15T03:08:29+08:002020-12-15T03:08:29+08:00

    如果您已安装 cron 或至少有一个包含该文件的软件包,您可以通过以下方式找到该软件包:

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

    接下来,下载软件包并将其解压缩到自定义文件夹(DEBIAN 文件夹也位于 /var/lib/dpkg/info 中):

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

    $_是上一个命令中最后一个参数的缩写。

    • 1

相关问题

  • 如何每 5 秒运行一次脚本?

  • 您如何在 Num Lock 开启的情况下在登录屏幕中启动?[复制]

  • 如何使 cron 电子邮件成为我的 @gmail 帐户

  • 您如何在终端上默认使用 bash 以外的 shell?

  • 如何选择应用程序作为默认值?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve