關於crontab以及輸出重定向

2022-04-03 06:47:19 字數 1138 閱讀 3861

linux下可以通過配置crontab來定時執行任務,執行體可以是一條系統命令或自己寫的乙個指令碼,同時可以指派使用者來執行。配置crontab有兩種方法。

方法1、使用crontab命令,例如新增乙個新的或編輯已有的,使用:

crontab -e
就可以進入配置檔案。此時配置crontab的執行者是當前登入使用者,如果當前使用者是root,需要為其他使用者配置,可以使用

crontab -e -u 使用者名稱

或su 使用者名稱

crontab -e

這種方法有乙個缺點,就是當前系統中配置的crontab不在乙個配置檔案中,讓管理員不方便查詢系統到底有多少個crontab。

方法2、直接在/etc/crontab檔案中新增,不過需要是root身份。開啟檔案,應該會看到類似下面的資訊

shell=/bin/bash

path=/sbin:/bin:/usr/sbin:/usr/bin

mailto=root

home=/

# for details see man 4 crontabs

# example of job definition:

# .---------------- minute (0 - 59)

# | .------------- hour (0 - 23)

# | | .---------- day of month (1 - 31)

# | | | .------- month (1 - 12) or jan,feb,mar,apr ...

# | | | | .---- day of week (0 - 6) (sunday=0 or 7) or sun,mon,tue,wed,thu,fri,sat

# | | | | |

# * * * * * user-name command to be executed

要新增新的crontab,只需要在檔案最後增加即可。注意這裡面需要指定使用者名稱;而方法1中則不需要,如果指定了,它會認為是命令的一部分,從而可能導致crontab執行失敗。

如果伺服器都是有root來管理,建議新增crontab使用方法2,這樣系統中的所有計畫任務都在一起,一目了然。

另乙個

Linux Linux printf 輸出重定向

方法一 include include include include include intmain 方法二 include include include intmain 到這裡我們就可以輕鬆解決 prinft 不輸出到螢幕的問題了.不外乎兩種情況.第1 標準輸出被重定向了.第2 輸出緩衝區的問...

Linux Linux printf 輸出重定向

方法一 include include include include include intmain 方法二 include include include intmain 到這裡我們就可以輕鬆解決 prinft 不輸出到螢幕的問題了.不外乎兩種情況.第1 標準輸出被重定向了.第2 輸出緩衝區的問...

Shell教程十一 Shell 輸入 輸出重定向

大多數 unix 系統命令從你的終端接受輸入並將所產生的輸出傳送回 到您的終端。乙個命令通常從乙個叫標準輸入的地方讀取輸入,預設情況下,這恰好是你的終端。同樣,乙個命令通常將其輸出寫入到標準輸出,預設情況下,這也是你的終端。重定向命令列表如下 命令說明 command file 將輸出重定向到 fi...