在Debian系統中,Crontab是一個用于設置周期性被執行的任務的工具
打開終端。
輸入crontab -e
命令來編輯當前用戶的Crontab文件。如果你想編輯其他用戶的Crontab文件,請使用sudo crontab -u username -e
,其中username
是目標用戶的用戶名。
在打開的Crontab文件中,你可以在文件的頂部定義你的自定義函數。例如,創建一個名為my_function
的函數:
#!/bin/bash
my_function() {
echo "Hello, this is my custom function!"
}
0 6 * * * my_function >> /tmp/my_function.log 2>&1
這行代碼的意思是:每天早上6點(0分6秒),執行my_function
函數,并將輸出重定向到/tmp/my_function.log
文件。
注意:確保你的腳本具有可執行權限。你可以使用chmod +x script_name.sh
命令來為腳本添加可執行權限。