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

本文将指导您安排计划在一天中的特定时间执行的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 AM和6 PM执行完整备份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类型通常用于备份用户数据,例如桌面,下载等。