Periodic Tasks

本文详细介绍了Linux下Cron任务调度的工作原理及配置方法。包括crond守护进程如何读取并执行crontab文件中的任务,crontab文件的格式说明,以及如何使用crontab命令管理个人和系统的定时任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[size=large][b]CRON: SCHEDULE COMMANDS[/b]
Under Linux, periodic execution is normally handled by the cron daemon. cron starts when the system boots and remains running as long as the system is up. cron reads one or more configuration files containing lists of command lines and times at which they are to be invoked. The command lines are executed by sh, so almost anything you can do by hand from the shell can also be done with cron.

A cron configuration file is called a “crontab,” short for “cron table.” cron looks for crontab files in three places: /var/spool/cron (/var/spool/cron/tabs on SUSE and /var/spool/cron/crontabs on Debian), /etc/cron.d, and /etc/crontab.

Crontab files for individual users are stored underneath /var/spool/cron. Typically, there is (at most) one crontab file per user: one for root, one for jsmith, and so on. Crontab files are named with the login names of the users they belong to, and cron uses these filenames to figure out which UID to use when running the commands that each file contains. The crontab command transfers crontab files to and from this directory.

Crontab files that schedule system maintenance tasks and other tasks defined by the system administrator are stored in the file /etc/crontab and in other files found in the /etc/cron.d directory. These files have a slightly different format from the per-user crontab files because they allow commands to be run as an arbitrary user. cron treats the /etc/crontab and /etc/cron.d entries in exactly the same way. In general, /etc/crontab is intended as a file for the system administrator to maintain by hand, whereas /etc/cron.d is provided as a place where software packages can install any crontab entries they might need.

When cron starts, it reads all of its config files, stores them in memory, and then goes to sleep. Once each minute, cron wakes up, checks the modification times on the crontab files, reloads any files that have changed, and then executes any tasks scheduled for that minute before returning to sleep.


[b]THE FORMAT OF CRONTAB FILES[/b]
All the crontab files on a system share a similar format. Comments are introduced with a pound sign (#) in the first column of a line. Each noncomment line contains six or seven fields and represents one command:

[b]minute hour day month weekday [username] command[/b] or
[b]minute hour day month weekday [username] run-parts directory[/b]

The first six fields are separated by whitespace, but within the command field whitespace is taken literally. The username is found only in /etc/crontab and in files from the /etc/cron.d directory; it specifies on whose behalf the command should be run. This field is not present or necessary in the user-specific crontab files (those stored in /var/spool/cron) because the UID is implied by the filename.

The minute, hour, day, month and weekday fields tell when to run the command.
Their interpretations are shown below:
[table]
|Field|Description|Range
|Minute|Minute of the hour|0 to 59
|Hour|Hour of the day|0 to 23
|Day|Day of the month| 1 to 31
|Month|Month of the year|1 to 12
|WeekDay|Day of the week|0 to 6 (0=sunday)
[/table]

Each of the time-related fields may contain:
[list]
[*]A star, which matches everything
[*]A single integer, which matches exactly
[*]Two integers separated by a dash, matching a range of values
[*]A comma-separated series of integers or ranges, matching any listed value
[/list]
There is a potential ambiguity to watch out for with the weekday and day fields. Every day is both a day of the week and a day of the month. If both weekday and day are specified, a day need satisfy only one of the two conditions in order to be selected.

The command is the sh command line to be executed. It can be any valid shell command and should not be quoted. command is considered to continue to the end of the line and may contain blanks or tabs.
Eg. 20 1 * * * find /tmp -atime +3 -exec rm -f { } ';'
This command will run at 1:20 each morning. It removes all files in the /tmp directory that have not been accessed in 3 days.

[b]CRONTAB MANAGEMENT[/b]
crontab filename installs filename as your crontab, replacing any previous version. crontab -e checks out a copy of your crontab, invokes your editor on it (as specified by the EDITOR environment variable), and then resubmits it to the crontab directory. crontab -l lists the contents of your crontab to standard output, and crontab -r removes it, leaving you with no crontab file at all.

Root can supply a username argument to edit or view other users' crontabs. For example, crontab -u jsmith -r erases the crontab belonging to the user jsmith.

Two config files, /etc/cron.deny and /etc/cron.allow, specify which users may submit crontab files. If the allow file exists, then it contains a list of all users that may submit crontabs, one per line. No unlisted person can invoke the crontab command. If the allow file doesn’t exist, then the deny file is checked. It, too, is just a list of users, but the meaning is reversed: everyone except the listed users is allowed access. If neither the allow file nor the deny file exists, most systems allow only root to submit crontabs. (Debian and Ubuntu default to allowing submissions by all users.)
[/size]
内容概要:该研究通过在黑龙江省某示范村进行24小时实地测试,比较了燃煤炉具与自动/手动进料生物质炉具的污染物排放特征。结果显示,生物质炉具相比燃煤炉具显著降低了PM2.5、CO和SO2的排放(自动进料分别降低41.2%、54.3%、40.0%;手动进料降低35.3%、22.1%、20.0%),但NOx排放未降低甚至有所增加。研究还发现,经济性和便利性是影响生物质炉具推广的重要因素。该研究不仅提供了实际排放数据支持,还通过Python代码详细复现了排放特征比较、减排效果计算和结果可视化,进一步探讨了燃料性质、动态排放特征、碳平衡计算以及政策建议。 适合人群:从事环境科学研究的学者、政府环保部门工作人员、能源政策制定者、关注农村能源转型的社会人士。 使用场景及目标:①评估生物质炉具在农村地区的推广潜力;②为政策制定者提供科学依据,优化补贴政策;③帮助研究人员深入了解生物质炉具的排放特征和技术改进方向;④为企业研发更高效的生物质炉具提供参考。 其他说明:该研究通过大量数据分析和模拟,揭示了生物质炉具在实际应用中的优点和挑战,特别是NOx排放增加的问题。研究还提出了多项具体的技术改进方向和政策建议,如优化进料方式、提高热效率、建设本地颗粒厂等,为生物质炉具的广泛推广提供了可行路径。此外,研究还开发了一个智能政策建议生成系统,可以根据不同地区的特征定制化生成政策建议,为农村能源转型提供了有力支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值