如何更改默认的crontab编辑器

Art representing Bash shell on a Linux system
Fatmawati Achmad Zaenuri/Shutterstock.com Fatmawati Achmad Zaenuri / Shutterstock.com

Want to have crontab use the editor of your choice instead of the other way around? This tutorial shows you how. These instructions will work with Linux, macOS and other Unix-like operating systems.

是否希望crontab使用您选择的编辑器而不是其他方式? 本教程向您展示如何。 这些说明适用于Linux,macOS和其他类似Unix的操作系统。

文本编辑器的敏感主题 (The Touchy Subject of Text Editors)

A text editor performs a pretty mundane task. Yet the depth of feeling people associate with their personal preference editor has led to flame wars that have been burning since 1985. We’re not stoking that fire, nor advocating one editor over another. What we’re going to show you is how to change the default editor for crontab to something else, should you choose to.

文本编辑器执行非常普通的任务。 然而,人们与他们的个人喜好编辑器相关联的感觉的深度导致了自1985年以来一直在燃烧的火焰大战 。我们不是在煽动这场大火,也不是在鼓吹一位编辑器。 我们将向您展示的是,如果您愿意,如何将crontab的默认编辑器更改为其他名称。

The crontab -e command will open an editor so that you can edit your cron table. Your cron table holds the list of any scheduled jobs you have set to occur at specified times. We’re not diving into the details of cron jobs in this article. We’re simply looking at the editor associated with the crontab -e command.

crontab -e命令将打开一个编辑器,以便您可以编辑cron表。 cron表包含您已设置为在指定时间发生的所有计划作业的列表。 我们不会在本文中深入探讨cron作业的细节 。 我们只是在看与crontab -e命令关联的编辑器。

The very first time you issue the crontab command with the -e (edit) option in a Bash terminal, you’re asked to pick the editor you’d like to use. Type crontab , a space, -e and press Enter.

第一次在Bash终端中向crontab命令发出-e (编辑)选项时,系统会要求您选择要使用的编辑器。 键入crontab ,一个空格, -e并按Enter。

crontab -e
crontab -e command

The editor you select is then used to open your cron table. In this example, nano was chosen by pressing the 1 key.

然后,您选择的编辑器用于打开cron表。 在此示例中,通过按1键选择了nano。

cron table in nano editor

The editor that you select from the menu is used every time you issue the crontab -e command. If you later change your mind, how do you pick another editor if you only get the menu the first time? That’s easy. The command to use is select-editor.

每次发出crontab -e命令时,都会使用从菜单中选择的编辑器。 如果您以后改变主意,如果仅第一次获得菜单,该如何选择另一个编辑器? 这很容易。 要使用的命令是select-editor

select-editor
select-editor command

So far, so simple. But what if you want to use an editor that isn’t in that menu? Or what if you’re working on an operating system that doesn’t provide the select-editor command? We can handle those scenarios as well.

到目前为止,如此简单。 但是,如果您想使用该菜单中没有的编辑器怎么办? 或者,如果您正在使用不提供select-editor命令的操作系统,该怎么办? 我们也可以处理这些情况。

不提供选择编辑器的发行版呢? (What About Distros That Don’t Provide select-editor?)

We can set the default editor for crontab by adding a line to our .bash_profile file. Type this command:

我们可以通过在.bash_profile文件中添加一行来为crontab设置默认编辑器。 输入以下命令:

gedit ~/.bash_profile

When the editor appears, add this entry to the file:

当编辑器出现时,将此条目添加到文件中:

export VISUAL="gedit"

export VISUAL="gedit"

.bash_profile in gedit

Of course, you’d substitute the command that launches the editor you wish to use for ‘gedit’. Save that file and close the editor. To see these changes take effect, either log out and back in or issue this command:

当然,您可以用替代命令来启动要用于“ gedit”的编辑器。 保存该文件并关闭编辑器。 要查看这些更改是否生效,请注销并重新登录或发出以下命令:

. ~/.bash_profile
. ~/.bash_profile command

Note that the line starts with a dot or period. The source command is an alias for the period command and performs the same action. But not all distributions provide the source command. The period command should always be present. Having given that caveat, the source command was present on all of the distributions this article was tested against Ubuntu, Debian, Manjaro, Arch, Fedora, CentOS, and OpenIndiana.

请注意,该行以点或句点开头。 source命令是period命令的别名,并且执行相同的操作。 但并非所有发行版都提供source命令。 period命令应始终存在。 具有给定的是需要注意,所述source的命令存在于所有这本文针对Ubuntu的,Debian的,Manjaro拱门Fedora的,CentOS的和的OpenIndiana测试的分布。

Whether you type a period or the word source, the command causes the settings from your .bash_profile to be read and transferred to your current session. Now when you type:

无论您输入句点还是单词source ,该命令都会导致您的.bash_profile中的设置被读取并传输到当前会话中。 现在,当您键入:

crontab -e

The editor you have specified will be used to open your cron table.

您指定的编辑器将用于打开cron表。

cron table in gedit

您的.bash_profile可能不为空 (Your .bash_profile Might Not Be Empty)

Your .bash_profile file might not be empty when you edit it. Just scroll to the bottom and add the export VISUAL="gedit" line to the bottom of the file. This is the default .bash_profile in Manjaro Linux, with the new line added:

您的.bash_profile文件在编辑时可能不会为空。 只需滚动到底部,然后将export VISUAL="gedit"行添加到文件底部即可。 这是Manjaro Linux中的默认.bash_profile,并添加了新行:

export line in .bash_profile

最后是OpenIndiana (And Finally, OpenIndiana)

With OpenIndiana, you need to add the export VISUAL="gedit" line to your .bashrc file, not to your .bash_profile. The command to you need to enter is:

使用OpenIndiana,您需要将export VISUAL="gedit"行添加到.bashrc文件中,而不是添加到.bash_profile中。 您需要输入的命令是:

pluma ~/.bashrc
.bashrc in pluma

Add the line and save the file, and close and re-open your terminal window.

添加该行并保存文件,然后关闭并重新打开您的终端窗口。

Issue the crontab -e command to verify your changes have taken affect:

发出crontab -e命令以验证您的更改已生效:

crontab -e
cron table in nano openIndiana

And now your cron table is loaded into nano.

现在,您的cron表已加载到nano中。

Now you can specify the editor of your choice on many types of Linux, whether it is descended from Debian, RedHat, Arch or something closer to a plain vanilla Unix.

现在,您可以在多种类型Linux上指定您选择的编辑器,无论它是Debian,RedHat,Arch的后代,还是更接近普通的Unix的后代。

翻译自: https://www.howtogeek.com/410995/how-to-change-the-default-crontab-editor/

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值