The cron process in Linux

Source: Internet
Author: User
Article Title: cron process, a daemon in Linux. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

At the beginning, I was not clear about the relationship between background processes, daemon processes, and cron processes. Now I understand. As I wrote in the text above:

Background process: a program that does not require user input during running. You can run multiple background processes on a multitasking operating system such as UNIX/Linux, and the user interacts with the foreground process (for example, data input ). Some background processes (such as Daemon) never require user input. Other processes are temporarily in the background when the user is busy with the programs currently running on the foreground.

A daemon is a background process. The Cron process is one of the many daemon processes. Let's take a look at the explanation of Cron in the dictionary:

  Cron

= Chronograph, [unix] (clock) daemon, (precise) Timing Program

It is a time-related program. Simply put, the cron daemon executes the predefined command, he will execute the reservation command or script at the specified time.

Okay. Now that you know what he is doing, let's get to know how he works.

"Running jobs at a certain interval requires cron for management. It consists of a crond daemon and a group of tables that describe what operations are performed and how often they are used. This daemon wakes up every minute and checks crontab to determine what to do. You can use the crontab command to manage crontab. The crond daemon is often started by the init process at system startup ."

Note: In fact, Vixie-Cron is installed on my machine. Therefore, my computer is not crond, but Vixie-Cron.

I have already mentioned the contents of the crontab table and how to write it. There is a new content to be added. The field after the time, that is, the sixth field: the 6th field contains all the content after the first five fields. It is a string to be passed to shell. Percent sign (%) will be converted to a blank line, so if you want to use % or any other special character, you need to add a backslash () in front (). The row before the first % is passed to shell, and all rows after this % are passed as standard input.

Generally, only the root user has the permission to run the cron process. If the Administrator permits or disallows other users to use the cron process, edit the c r o n under/etc. d e n y and c r o n. a l o w files to prohibit or allow users to own their own c r o n t a B files.

Every user can have their own c r o n t a B files. However, in a large system, the system administrator usually disallows these files, instead, only one such file is retained throughout the system. The system administrator uses c r o n. d e n y and c r o n. a l o w files to prohibit or allow users to own their own c r o n t a B files.

If you do not have your own crontab file, you may need to manually create a file for the first time, and then use $ crontab <你的crontab文件名> To submit your crontab, and then you can use the crontab command to modify, delete, and list the contents of your crontab. For more information about how to use this command, see the previous blog about cron.

Note: crontab refers to a table, which records commands or scripts to be executed at the scheduled time. Second, it is also a command, you can use the crontab command to manage the crontab table.

When running cron, You need to view the crontab for job scheduling. The preceding several statements are very important and explain the problems I have encountered: the daemon wakes up every minute and checks crontab to determine what to do. Check crontab once every minute, and then arrange to run the command or script once.

Where is the crontab created by the user? Here:/var/spool/cron/crontabs. In fact, crontab for cron check includes not only crontab created by the user, but also crontab of the system, including/etc/crontab and/etc/cron. files in the d directory (/etc/cron. d ). It is necessary to explain/etc/crontab. A typical example is as follows:

/Etc/crontab

SHELL =/bin/bash PATH =/sbin:/bin:/usr/sbin:/usr/bin MAILTO = root

HOME =/# run-parts 01 ***** root run-parts/etc/cron. hourly

02 4 * ** root run-parts/etc/cron. daily 22 4*0 root

Run-parts/etc/cron. weekly 42 4 1 ** root run-parts/etc/cron. monthly

In these system crontab, a field is added between the fifth time field (week) and the command. This field specifies which user should run this command. Generally, it is the root user. Then explain the following command: there will be cron under/etc. hourly, cron. daily, cron. weekly, cron. the four monthly folders are separated with files to specify the operations to be performed every hour, every day, every week, and every month, the run-parts command is used to execute programs and scripts in the following directory, such as run-parts/etc/cron. hourly Means to execute/etc/cron. programs and scripts in the hourly folder.

In this example, the real work is executed by the run-parts command, which runs/etc/cron. hourly,/etc/cron. scripts in directories such as daily;/etc/crontab only controls the job execution time. Note that all commands here are run as root users. It should also be noted that crontab can contain shell variable assignments, which will be executed before running the command.

  Output

You may want to know how to handle the output from the command. Most commands designed to use cron use syslog To record output in logs (see the discussion in "LPI 102 exam preparation: management tasks ). However, the output directed to stdout is sent to the user by email.

I am not quite clear about the above section. However, we can see in the system log/var/log/messages whether your cron scheduling command or script has been executed. Then, in the current test, if it is a GUI application, it seems that cron cannot run well, so it cannot run well. For example, you cannot use mp3blster to play music, because he needs to use ncurses to draw his interface. For example, when using mplayer to play a video, I can only play music in the afternoon test and cannot display a video.

In gentoo, I used eix-s cron to check whether there are many cron programs with different characteristics developed by different people. For example, dcron and fcron. You can view the description in detail.

For cron's learning, it may have come to an end. To sum up, we will celebrate it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.