如何在 Linux 系统中为特定用户添加计划任务


在本文中,我们将教会你如何安排计划任务在一天中的特定时间执行。

计划任务的一般语法

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

要查看机器上存在的计划任务列表,请运行以下命令

# crontab -u test1 -l
no crontab for test1

要将新的计划任务添加到 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

结束语

在配置结束时,你应该能够在每天的特定时间从特定用户处运行脚本或命令。这样,我们可以为任何用户指定计划任务。此类计划任务通常用于备份用户数据,例如桌面、下载等。

更新时间:2020 年 1 月 21 日

3K+ 浏览

开启你的事业

通过完成课程获得认证

开始学习
广告