如何在 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+ 瀏覽

開啟你的事業

透過完成課程獲得認證

開始學習
廣告