在 Ubuntu 的 LNMP 環境中設置定時任務,通常是指使用 cron 服務來定期執行某些任務
打開終端。
輸入以下命令以編輯 cron 配置文件:
crontab -e
* * * * * /path/to/command arg1 arg2
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday to Saturday, 7 is also Sunday)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)
例如,如果你想要每天凌晨 1 點備份數據庫,你可以添加以下行:
0 1 * * * /path/to/backup-script.sh
保存并關閉編輯器。這將自動安裝新的 cron 配置。
若要查看當前用戶的定時任務列表,請運行以下命令:
crontab -l
crontab -r
命令刪除整個 crontab 配置文件,或者使用 crontab -e
編輯器刪除相應的行。注意:在執行定時任務時,請確保使用絕對路徑,而不是相對路徑。此外,確保你的腳本具有可執行權限。你可以使用 chmod +x /path/to/script.sh
命令為腳本添加可執行權限。