有如下幾種方式:
crontab
at
&
nohup
1. crontab
定時執行任務
# crontab -e //編輯crontab設定檔Select an editor. To change later, run 'select-editor'. 1. /bin/ed 2. /bin/nano <---- easiest 3. /usr/bin/emacs24 4. /usr/bin/vim.basic 5. /usr/bin/vim.tinyChoose 1-5 [2]:
# crontab -l //查看crontab設定檔# 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 command5 * * * * ls
# crontab -r //刪除所有調度任務
文法格式
| 分 |
小時 |
日 |
月 |
星期 |
命令 |
| 0-59 |
0-23 |
1-31 |
1-12 |
0-6 |
cmd |
特殊符號
*:所有數字
/:每
-:一個區間
,:多個數字
2. at
特定任務運行一次
# service atd status //查看atd服務atd start/running, process 1350
# at now + 5 minutes //添加warning: commands will be executed using /bin/shat> /home/a.outat> ctrl+Djob 4 at Wed Apr 19 14:48:00 2017
| 時間 |
例子 |
說明 |
| Minute |
at now + 5 minutes |
任務在5分鐘後運行 |
| Hour |
at now + 1 hour |
任務在1小時後運行 |
| Days |
at now + 3 days |
任務在3天后運行 |
| Weeks |
at now + 2 weeks |
任務在兩周后運行 |
| Fixed |
at midnight |
任務在午夜運行 |
| Fixed |
at 10:30pm |
任務在晚上10點30分 |
| Fixed |
at 23:59 12/31/2018 |
任務在2018年12月31號23點59分 |
# at -l //列出所有作業1 Thu Apr 20 02:05:00 2017 a thomas
# at -r job //刪除作業Warning: deleting running job
3. &
在後台運行一個佔用時間不長的任務
# jobs //查看後台運行任務# fg //將後台運行任務調至前台
4. ctrl + z
將一個正在前台執行的命令放到後台,並且處於暫停狀態
5. nohup
在後台運行一個命令,即使在使用者退出時也不受影響
# nohup --helpUsage: nohup COMMAND [ARG]... or: nohup OPTION
6. 查看當前終端的進程id
# echo $$ 2602