Use of Linux command crontab - loop execution of set commands
Compared with at , which executes a command only once, crontab, which we are going to talk about in this article, executes the set commands in a loop.
Similarly, the use of crontab requires the support of the crond service. The service is started as follows
# service crond start
//or
# /etc/init.d/crond start
Crontab user permission configuration
In the system, for the sake of security, of course, we cannot say that all users can use the crontab command. You should know that this is a very dangerous thing. Therefore, we need to set the user's permissions. Generally, we need to use two files to set the user's permissions. They are /etc/cron.allow and /etc/cron.deny.
/etc/cron.allow will allow users who use crontab to write to this file. Users in this file have permission to use crontab. Of course, users not in this file do not have permission to use crontab.
/etc/cron.deny will not allow users who use crontab to write to this file. Users in this file do not have permission to use crontab. Of course, users not in this file have permission to use crontab.
In terms of priority, /etc/cron.allow takes precedence over /etc/cron.deny. The way the crond process works with permissions is this: when a user uses the crontab command, the process will search the system for the /etc/cron.allow file. After finding the file, it will detect whether the user is in the file. If so, the user is allowed to use crontab. If not, the user is not allowed to use crontab. If the /etc/cron.allow file does not exist, the process will search for the /etc/cron.deny file and then detect whether the user is in this file. If neither file exists, the system defaults to only the root user having permission to use crontab. In general systems, because all users are trusted by default, the /etc/cron.deny file exists in the system by default and is empty.
How to run crontab
When a user creates a scheduled command using crontab, the task will be recorded in the /var/spool/cron/ directory in the form of a document. And the document is named after the user. For example, if the user onmpw uses crontab to create a scheduled command, the task will be recorded in the /var/spool/cron/onmpw document. However, we should try not to use vim to edit this file, because it is likely that cron will not run due to syntax errors.
crontab Command Syntax
Next, let's look at the syntax of crontab
# crontab [ -u username ] [options]
Option parameters:
-u : This option is only available to root users, and is used to help other users set or remove crontab routines.
-e : Edit crontab work content
-l : View crontab work content
-r : Remove all crontab work content. Note that this is all content. If you only need to remove one or two items, you need to use the –e parameter to edit.
The format of each job in crontab
After talking about the syntax of crontab, we immediately use the following command to edit it
# crontab –e
Then a vim interface will open. At this time, we need to understand the content format of the file before we can create our work routine.
Each task needs to occupy a line, so what should the content of this line be? Each line contains the following six items:
Minute (0-59) Hour (0-23) Day (1-31) Month (1-12) Week (0-7) Command
Among these six items, the others are easy to understand. Only the week item requires our attention, that is, if the number of the week is set to 0 or 7, it means Sunday.
Of course, in addition to these normal numbers, crontab also provides us with several special characters to help us make more flexible settings.
* (asterisk): Acceptable at any time.
59 23 * * * rm –rf /tmp/*
In the above example, the day, month, and week are all *, which means 'execute the following command at 23:59 in the evening regardless of the month, day, or day of the week'.
, (comma): separate time periods. For example, if we need to delete files starting with sess_ in the /tmp/ directory at 2 a.m. and 11 p.m. every day, we should set it like this
0 2,23 * * * rm –rf /tmp/sess_*
- (minus sign): represents a time period. If we need to perform a task from 8 am to 4 pm every day, we should set it like this
0 8-16 * * * Command
/n: n is a number, which means that the command is executed every n units.
*/3 * * * * command
Here we use * and /5 together, of course we can also use the equivalent form such as 0-59/3.
There is one thing to note here: week and day and month cannot exist at the same time. That is to say, if the week is set to a specific number, then our day and month need to be represented by *. Similarly, if the day and month are set to specific numbers, the week is represented by *.
Crontab job routine management
OK, now we know how to use crontab -e to set a job routine. Next, let's see how to manage the job routine we set.
In the crontab command syntax we mentioned that there are two options –l and –r. To find the job routine we set, use -l.
# crontab –l
*/5 * * * * rm -rf /tmp/sess_*
10 11 * * * rm -rf /tmp/wsdl_*
This is to view all the work routines. If we want to delete all the work routines, we can use –r
# crontab –r //Delete all work routines
# crontab –l //Check again
//It is empty
Of course, as we said before, if you want to delete only one item, you need to edit it again with -e and delete the information of the task you want to delete.
Crontab is a commonly used command, and we must be familiar with the format of its content.
I hope this article is helpful to you.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Restart PostgreSQL in Ubuntu 18.04
Publish Date:2025/04/09 Views:72 Category:PostgreSQL
-
This short article shows how to restart PostgreSQL in Ubuntu. Restart PostgreSQL Server in Ubuntu You can restart Postgres server in Ubuntu using the following command. Order: sudo service postgres restart Sometimes the above command does n
Issues to note when installing Apache on Linux
Publish Date:2025/04/08 Views:78 Category:OPERATING SYSTEM
-
As the most commonly used web server, Apache can be used in most computer operating systems. As a free and open source Unix-like operating system, Linux and Apache are a golden pair. This article will introduce the installation and use of A
How to decompress x.tar.xz format files under Linux
Publish Date:2025/04/08 Views:186 Category:OPERATING SYSTEM
-
A lot of software found today is in the tar.xz format, which is a lossless data compression file format that uses the LZMA compression algorithm. Like gzip and bzip2, it supports multiple file compression, but the convention is not to compr
Summary of vim common commands
Publish Date:2025/04/08 Views:115 Category:OPERATING SYSTEM
-
In Linux, the best editor should be vim. However, the complex commands behind vim's powerful functions also make us daunted. Of course, these commands do not need to be memorized by rote. As long as you practice using vim more, you can reme
Detailed explanation of command return value $? in Linux
Publish Date:2025/04/08 Views:58 Category:OPERATING SYSTEM
-
? is a special variable. This variable represents the return value of the previous command. That is to say, when we run certain commands, these commands will return a code after running. Generally, if the command is successfully run, the re
Common judgment formulas for Linux script shell
Publish Date:2025/04/08 Views:159 Category:OPERATING SYSTEM
-
In shell script programming, predicates are often used. There are two ways to use predicates, one is to use test, and the other is to use []. Let's take a look at how to use these two methods through two simple examples. Example 1 # test –
Shell script programming practice - specify a directory to delete files
Publish Date:2025/04/08 Views:98 Category:OPERATING SYSTEM
-
Usually, in Linux system we need to frequently delete some temporary files or junk files. If we delete them one by one manually, it will be quite troublesome. I have also been learning shell script programming recently, so I tried to write
Use of Linux command at - set time to execute command only once
Publish Date:2025/04/08 Views:158 Category:OPERATING SYSTEM
-
This article mainly involves a knowledge point, which is the atd service. Similar to this service is the crond service. The functions of these two services can be similar to the two functional functions of javascript. Those who have learned
Linux practice - regularly delete files under the directory
Publish Date:2025/04/08 Views:198 Category:OPERATING SYSTEM
-
Since we want to delete the files under the directory regularly, we need to use the Linux crontab command. And the content format of each work routine is also introduced in the format of each crontab work. Similarly, we need to use shell sc