linux後台執行命令crontab__linux

來源:互聯網
上載者:User

有如下幾種方式:
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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.