如何在 Linux 系统中为特定用户添加 Cron 作业

linuxoperating systemsoftware & coding

本文将教您如何安排 Cron 作业在一天中的特定时间执行。

Cron 作业的常规语法

MIN    HOUR    Day of month    Month    Day of Week    Command
0-59   0-23       1-31          1-12       0-6       linux command or script

要查看计算机上现有的 cron 任务列表,请运行以下命令

# crontab -u test1 -l
no crontab for test1

要将新的 cron 任务添加到 Test1 用户,请运行以下命令 

#crontab -u test1 -e
no crontab for test1 - using an empty one
Select an editor. To change later, run 'select-editor'.
/bin/ed
/bin/nano <---- easiest
/usr/bin/vim.basic
/usr/bin/vim.tiny
Choose 1-4 [2]:2
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command

为其他用户安排作业

cron 的基本用法是在特定时间执行作业,如下所示。这将在每天上午 9 点和下午 6 点执行完整备份 Shell 脚本 (backup_files.sh)

00 09-18 * * * /home/test1/backup_files.sh

解释

00 – 00th Minute
09-18 – 09 AM & 06:00PM
* --Every day of the Month
* -- Every Month.
* --Every day of the week

结论

配置完成后,您应该能够每天在特定时间以特定用户身份运行脚本或命令。这样,我们可以为任何用户指定Cron任务。这类Cron通常用于备份用户数据,例如桌面、下载等。


相关文章


有用资源