Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use cron scheduling tasks under Linux

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to use cron scheduling tasks under Linux. It is very detailed and has a certain reference value. Friends who are interested must finish it!

Cron is a tool for configuring task scheduling in Unix systems. This tool can run commands or scripts periodically or at regular intervals.

Fortunately for installing the cron daemon, Fedora Linux is preconfigured to run important system tasks to keep the system updated, and there are several utilities that can run tasks such as cron, anacron, at, and batch. This article focuses only on the installation of the cron utility. Cron is installed with the cronie package, and the cronie package also provides cron services.

To determine if the package already exists, use the rpm command:

$rpm-Q cronie Cronie-1.5.2-4.el8.x86_64 returns the full name of the cronie package if cronie is installed. If it is not installed on your system, it will show that it is not installed.

Use the following command to install:

Dnf install cronie runs the cron daemon cron job is executed by the crond service, which reads the information in the configuration file. Before adding a job to the configuration file, you must start the crond service or install it. What is crond? Crond is short for cron daemon. To determine if the crond service is running, enter the following command:

$systemctl status crond.service ● crond.service-Command Scheduler Loaded: loaded (/ usr/lib/systemd/system/crond.service; enabled; vendor pre > Active: active (running) since Sat 2021-03-20 14:12:35 PDT; 1 day 21h ago Main PID: 1110 (crond) if you don't see something similar Active: active (running) since... You need to start the crond daemon. To run the crond service in the current session, enter the following command:

Systemctl run crond.service configures it to boot automatically, enter the following command:

Systemctl enable crond.service if for some reason you want to stop the crond service, use the stop command as follows:

To restart $systemctl stop crond.service, simply use the restart command:

$systemctl restart crond.service defines a cron job cron configuration the following is an example of the configuration details of a cron job. It defines a simple cron job that pulls the latest changes from the git master branch to the clone's repository:

* / 59 * username cd / home/username/project/design & & git pull origin master has two main parts:

The first part is * / 59 *. This indicates that the timer is set to execute once in the 59th minute. (LCTT translation note: there is an error here in the original text. )

The rest of the line is a command because it will be run from the command line. In this example, the command itself consists of three parts:

The job will run as user username

It will change to the directory / home/username/project/design

Run the git command to pull the latest changes in the master branch

Time syntax as mentioned above, the time information is the first part of the cron job string, as shown above. It determines how often and when cron jobs run. It consists of five parts in the following order:

Minute

Hour

One day of the month

Month

One day of the week

Here is a more graphical way to explain the syntax:

.-minutes (0-59) |.-hours (0-23) | |.-one day in January (1-31) |.-month (1-12) or jan, feb, mar, apr... |.-one day of the week (0-6) (Sunday = 0 or 7) | or sun, mon, tue, wed, thr, fri, sat | * the asterisk (*) of the user-name command-to-be-executed asterisk can be used instead of a number to indicate all possible values at that location. For example, the asterisk in the minute position causes it to run every minute. The following example may help to better understand the syntax.

This cron job will run every minute:

* [command] the slash indicates the number of minutes between minutes. The following example will run 12 times per hour, that is, every 5 minutes:

* / 5 * [command] the next example will be midnight on the second day of each month (for example, January 2 at 12: 00 a.m., February 2 at 12:00, etc.):

0 02 * [command] (LCTT translation note: for cron time format, there are more format symbols, which are not expanded here)

Creating a cron job using crontab the cron job runs in the background, constantly checking the / etc/crontab file and / etc/cron.*/ and / var/spool/cron/ directories. Each user has a unique crontab file in / var/spool/cron/.

You should not edit these cron files directly. The crontab command is a method for creating, editing, installing, uninstalling, and listing cron jobs.

What's even cooler is that you don't have to restart cron after creating a new file or editing an existing file.

$crontab-e this will open your existing crontab file, or create one. When you call crontab-e, the vi editor is used by default. Note: to edit the crontab file using Nano, you can set the EDITOR=nano environment variable.

Use the-l option to list all cron jobs. If necessary, use the-u option to specify a user.

$crontab-l $crontab-u username-l use the following command to delete all cron jobs:

$crontab-r to delete a job for a specific user, you must run the following command as root:

The above $crontab-r-u username is all the contents of this article entitled "how to use cron scheduling tasks under Linux". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report