解析Ubuntu下crontab命令的用法
cron是一个Linux下的后台进程,用来定期的执行一些任务。因为我用的是Ubuntu,所以这篇文章中的所有命令也只能保证在Ubuntu下有效,但其他系统应该也差不多。
想要让cron执行你指定的任务,首先就要编辑crontab文件。crontab是一个文本文件,用来存放你要运行的命令。你可以以下命令
crontab -e
来打开你的用户所属的crontab文件。第一次用这个命令,会让你选择文本编辑器,我选的是vim。选定的编辑器也可以使用
select-editor
命令来更改。这点命令行中已经有足够的提示,就不多说了。
打开后的crontab文件类似这种样子:
# m h dom mon dow command
*/2 * * * * date >> ~/time.log
第二行是我为了测试写的一个定期任务,它的意思是,每隔两分钟就执行 date >> ~/time.log 命令(记录当前时间到time.log文件)。你可以把它加入你的crontab中,然后保存退出。
保存了crontab之后,我们还需要重启cron来应用这个计划任务。使用以下命令:
sudo service cron restart
下面稍微解释下crontab中每行的含义。crontab中的每一行代表一个定期执行的任务,分为6个部分。前5个部分表示何时执行命令,最后一个部分表示执行的命令。每个部分以空格分隔,除了最后一个部分(命令)可以在内部使用空格之外,其他部分都不能使用空格。前5个部分分别代表:分钟,小时,天,月,星期,每个部分的取值范围如下:
分钟 0 - 59
小时 0 - 23
天 1 - 31
月 1 - 12
星期 0 - 6 0表示星期天
除了这些固定值外,还可以配合星号(*),逗号(,),和斜线(/)来表示一些其他的含义:
星号 表示任意值,比如在小时部分填写 * 代表任意小时(每小时)
逗号 可以允许在一个部分中填写多个值,比如在分钟部分填写 1,3 表示一分钟或三分钟
斜线 一般配合 * 使用,代表每隔多长时间,比如在小时部分填写 */2 代表每隔两分钟。所以 */1 和 * 没有区别
*/2 可以看成是能被2整除的任意值。
以下是一些例子(省略了命令部分):
* * * * * # 每隔一分钟执行一次任务
0 * * * * # 每小时的0点执行一次任务,比如6:00,10:00
6,10 * 2 * * # 每个月2号,每小时的6分和10分执行一次任务
*/3,*/5 * * * * # 每隔3分钟或5分钟执行一次任务,比如10:03,10:05,10:06
以上就是在cron中加入计划任务的基本知识。因为cron中的任务基本就是执行命令行,所以当然也会有权限问题。以上例子中的任务就是以你当前登录用户的权限执行的,如果你需要以root用户执行某个任务,可以在crontab前加上sudo。
sudo crontab -e
顺带一提,crontab文件对每个用户都是不同的,所以刚才设置的定期看时间的任务,在这里是看不到的。因为我们没有为root用户增加这样的计划任务。
再顺带一提,不喜欢命令行的童鞋可以去 Ubuntu软件中心 下一个 计划任务 程序。在软件中心中搜索 schedule 就可以搜到。它是一个GUI的程序,做的还蛮傻瓜化的。不过好像没办法设置成使用root用户运行任务。而且,对于要ssl到服务器上进行操作的童鞋来说,命令行还是最好(唯一)的工具。
因为我目前就用到这些知识,所以对cron也没有更多了解。想了解Ubuntu下的cron的童鞋,可以去看看Ubuntu官方的这篇 CronHowTo ,个人觉得讲的还是蛮详细的。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

The reasons for the installation of VS Code extensions may be: network instability, insufficient permissions, system compatibility issues, VS Code version is too old, antivirus software or firewall interference. By checking network connections, permissions, log files, updating VS Code, disabling security software, and restarting VS Code or computers, you can gradually troubleshoot and resolve issues.

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

Visual Studio Code (VSCode) is a cross-platform, open source and free code editor developed by Microsoft. It is known for its lightweight, scalability and support for a wide range of programming languages. To install VSCode, please visit the official website to download and run the installer. When using VSCode, you can create new projects, edit code, debug code, navigate projects, expand VSCode, and manage settings. VSCode is available for Windows, macOS, and Linux, supports multiple programming languages and provides various extensions through Marketplace. Its advantages include lightweight, scalability, extensive language support, rich features and version

vscode built-in terminal is a development tool that allows running commands and scripts within the editor to simplify the development process. How to use vscode terminal: Open the terminal with the shortcut key (Ctrl/Cmd). Enter a command or run the script. Use hotkeys (such as Ctrl L to clear the terminal). Change the working directory (such as the cd command). Advanced features include debug mode, automatic code snippet completion, and interactive command history.
