cron is a background process under Linux that is used to perform some tasks regularly. Because I am using Ubuntu, all the commands in this article are only guaranteed to be valid under Ubuntu, but other systems should be similar.
If you want cron to perform the tasks you specify, you must first edit the crontab file. crontab is a text file used to store the commands you want to run. You can use the following command
crontab -e
to open the crontab file belonging to your user. The first time you use this command, you will be asked to choose a text editor. I chose vim. The selected editor can also be changed using the
select-editor
command. There are enough hints in the command line for this point, so I won’t go into details.
The crontab file after opening looks like this:
# m h dom mon dow command
*/2 * * * * date >> ~/time.log
Second The line is a periodic task I wrote for testing. It means to execute the date >> ~/time.log command every two minutes (record the current time to the time.log file). You can add it to your crontab, then save and exit.
After saving the crontab, we need to restart cron to apply this scheduled task. Use the following command:
sudo service cron restart
The following briefly explains the meaning of each line in crontab. Each line in the crontab represents a regularly executed task and is divided into 6 parts. The first 5 parts indicate when the command is executed, and the last part indicates the command executed. Each section is separated by spaces, and no other section can use spaces except the last section (command) which can use spaces internally. The first 5 parts represent: minutes, hours, days, months, and weeks. The value range of each part is as follows:
minutes 0-59
hours 0-23
days 1-31
Month 1 - 12
Week 0 - 6 0 means Sunday
In addition to these fixed values, you can also use asterisks (*), commas (,), and slashes (/) to Indicates some other meanings:
Asterisks represent any value, such as filling in the hour part * represents any hour (every hour)
Comma can allow multiple values to be filled in one part, such as the minutes part Fill in 1,3 to represent one minute or three minutes
Slashes are generally used in conjunction with * to represent how long it takes. For example, fill in */2 in the hour part to represent every two minutes. So there is no difference between */1 and *
*/2 can be regarded as any value divisible by 2.
The following are some examples (the command part is omitted):
* * * * * # Execute the task every one minute
0 * * * * # Execute the task at 0 o'clock every hour , such as 6:00, 10:00
6,10 * 2 * * # Execute a task at 6 minutes and 10 minutes every hour on the 2nd of every month
*/3,*/5 * * * * # Execute the task every 3 minutes or 5 minutes, such as 10:03, 10:05, 10:06
The above is the basic knowledge of adding scheduled tasks in cron. Because the task in cron is basically to execute the command line, of course there will be permission issues. The tasks in the above examples are executed with the permissions of your current logged-in user. If you need to execute a task as the root user, you can add sudo before crontab.
sudo crontab -e
By the way, the crontab file is different for each user, so the task of regularly checking the time just set cannot be seen here. Because we have not added such scheduled tasks for the root user.
By the way, if you don’t like the command line, you can go to the Ubuntu Software Center and go to the next scheduled task program. You can find it by searching for schedule in the Software Center. It is a GUI program, and it is quite fool-proof. However, it seems there is no way to set it up to use the root user to run tasks. Moreover, for those who want to SSL to the server for operation, the command line is still the best (only) tool.
Because I am currently using this knowledge, I don’t know much about cron. If you want to know about cron's children's shoes under Ubuntu, you can read the official Ubuntu article CronHowTo. I personally think it is quite detailed.

Python解析XML中的特殊字符和转义序列XML(eXtensibleMarkupLanguage)是一种常用的数据交换格式,用于在不同系统之间传输和存储数据。在处理XML文件时,经常会遇到包含特殊字符和转义序列的情况,这可能会导致解析错误或者误解数据。因此,在使用Python解析XML文件时,我们需要了解如何处理这些特殊字符和转义序列。一、特殊字符和

Python编程解析百度地图API文档中的坐标转换功能导读:随着互联网的快速发展,地图定位功能已经成为现代人生活中不可或缺的一部分。而百度地图作为国内最受欢迎的地图服务之一,提供了一系列的API供开发者使用。本文将通过Python编程,解析百度地图API文档中的坐标转换功能,并给出相应的代码示例。一、引言在开发中,我们有时会涉及到坐标的转换问题。百度地图AP

使用Python解析SOAP消息SOAP(SimpleObjectAccessProtocol)是一种基于XML的远程过程调用(RPC)协议,用于在网络上不同的应用程序之间进行通信。Python提供了许多库和工具来处理SOAP消息,其中最常用的是suds库。suds是Python的一个SOAP客户端库,可以用于解析和生成SOAP消息。它提供了一种简单而

随着PHP8.0的发布,许多新特性都被引入和更新了,其中包括XML解析库。PHP8.0中的XML解析库提供了更快的解析速度和更好的可读性,这对于PHP开发者来说是一个重要的提升。在本文中,我们将探讨PHP8.0中的XML解析库的新特性以及如何使用它。什么是XML解析库?XML解析库是一种软件库,用于解析和处理XML文档。XML是一种用于将数据存储为结构化文档

使用Python解析带有命名空间的XML文档XML是一种常用的数据交换格式,能够适应各种应用场景。在处理XML文档时,有时会遇到带有命名空间(namespace)的情况。命名空间可以防止不同XML文档中元素名的冲突,提高了XML的灵活性和可扩展性。本文将介绍如何使用Python解析带有命名空间的XML文档,并给出相应的代码示例。首先,我们需要导入xml.et

PHP中的HTTPBasic鉴权方法解析及应用HTTPBasic鉴权是一种简单但常用的身份验证方法,它通过在HTTP请求头中添加用户名和密码的Base64编码字符串进行身份验证。本文将介绍HTTPBasic鉴权的原理和使用方法,并提供PHP代码示例供读者参考。一、HTTPBasic鉴权原理HTTPBasic鉴权的原理非常简单,当客户端发送一个请求时

PHP爬虫是一种自动化获取网页信息的程序,它可以获取网页代码、抓取数据并存储到本地或数据库中。使用爬虫可以快速获取大量的数据,为后续的数据分析和处理提供巨大的帮助。本文将介绍如何使用PHP实现一个简单的爬虫,以获取网页源码和内容解析。一、获取网页源码在开始之前,我们应该先了解一下HTTP协议和HTML的基本结构。HTTP是HyperText

PHP中的单点登录(SSO)鉴权方法解析引言:随着互联网的发展,用户通常要同时访问多个网站进行各种操作。为了提高用户体验,单点登录(SingleSign-On,简称SSO)应运而生。本文将探讨PHP中的SSO鉴权方法,并提供相应的代码示例。一、什么是单点登录(SSO)?单点登录(SSO)是一种集中化认证的方法,在多个应用系统中,用户只需要登录一次,就能访问


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools