使用crontab 添加一個定時任務
* * * * /bin/bash -l -c ' ~/.script.sh'
這個腳本的作用是統(tǒng)計一些數(shù)據(jù)并生成報表文件.
這個腳本每分鐘執(zhí)行一次,但是實際操作中crontab并沒有像預期的那樣生成報表文件.
然而當我手動執(zhí)行這個命令時 "~/.script.sh" 生成文件并退出.
有兩個疑問:
1: 為什么crontab 沒有執(zhí)行我的腳本. 查看了crontab 日志(/var/log/crond)輸出: May 5 20:36:01 localhost CROND[27260]: (ernest) CMD (/bin/bash -l -c '~/script.sh') 沒有發(fā)現(xiàn)任何異常.
2: 會不會是我執(zhí)行腳本的環(huán)境 不一樣呢? 測試了一下 "* * * * * /bin/bash -l -c 'source ~/.bash_profile && ~/.script.sh'" 也不行.
各位大牛們有沒有遇到和我一樣的問題呢?
Manually running the script takes effect, but the most common reasons why crontab does not take effect are: The commands in your script do not use absolute paths.
Try using absolute path. Because~
對不同的用戶會解釋成不同的$HOME
.
Usually add script.sh
at the beginning of
cd /home/username
. .profile
If you create the crontab command with sudo crontab -e, then the executing user is root, and the ~ here will also be parsed as /root If you're not using sudo, that should be fine.