2016-01-29 140 views
-4

我已經將cron作業添加到/etc/crontab文件在php腳本中的方法file_put_contents但是這個cron沒有執行。如果我將這個文件保存在nano編輯器中,cron是可以工作的。可以promtp爲什麼被php腳本添加的cron沒有執行? 我嘗試重新加載cron守護進程,但這沒有幫助。 我使用文章http://www.kavoir.com/2011/10/php-crontab-class-to-add-and-remove-cron-jobs.html的cron類。Cron從PHP腳本不工作

1更換方法saveJobs用於保存crons到文件/etc/crontab中

static public function saveJobs($jobs = array()) { 
    $output = file_put_contents('/etc/crontab', self::arrayToString($jobs)); 
    return $output; 
} 

命令添加cron是添加

Crontab::addJob("*/1 * * * * root php -f /var/www/get_all_clients_data_db.php "); 

紀錄cron來的crontab但這cron的不執行。

/etc/crontab中

# /etc/crontab: system-wide crontab 
# Unlike any other crontab you don't have to run the `crontab' 
# command to install the new version when you edit this file 
# and files in /etc/cron.d. These files also have username fields, 
# that none of the other crontabs do. 

SHELL=/bin/sh 
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin 

# m h dom mon dow user command 
17 * * * * root cd/&& run-parts --report /etc/cron.hourly 
25 6 * * * root test -x /usr/sbin/anacron || (cd/&& run-parts --report /etc/cron.daily) 
47 6 * * 7 root test -x /usr/sbin/anacron || (cd/&& run-parts --report /etc/cron.weekly) 
52 6 1 * * root test -x /usr/sbin/anacron || (cd/&& run-parts --report /etc/cron.monthly) 
# 
*/1 * * * * root php -f /var/www/get_all_clients_data_db.php 

我的錯誤道歉。這是我在StackOverflow上的第一個問題。 在此先感謝。

+0

顯示你的結果。你的腳本工作後你會得到什麼。之後也顯示'ls -la/etc/crontab'。 – ksimka

+0

請分享您的代碼。沒人能猜到你想要做什麼。 – Repox

+0

fil_put_contents必須是file_put_contents – schellingerht

回答

0

我發現了錯誤。系統日誌/var/log/syslog有記錄

/usr/sbin/cron[8391]: (*system*) ERROR (Missing newline before EOF, this crontab file will be ignored) 

當cron通過腳本添加時。在我添加新行(\ n)添加到執行的cron的方法後。

0

分享你的代碼,很可能你缺少連接共享庫的錯誤。只需啓用crontab日誌記錄並檢查即將發生的確切錯誤。

有一個用於記錄的命令行選項。輸出保存到screenlog.n文件中,其中n是屏幕的編號。從屏幕的手冊頁:

'-L'告訴屏幕打開自動輸出日誌記錄。

從屏幕啓動程序並檢查日誌爲什麼沒有運行。

/usr/bin/screen -d -m -S -L CRMService /home/CRMService 

讓我知道如果你進一步卡住!

+0

當我在nano中添加這個cron時,它是可行的。我試圖在控制檯中運行腳本。腳本執行沒有錯誤。但是如果從腳本添加cron,它不會啓動。 – RomanP

+0

如果在腳本中添加了cron後,在nano中打開並保存文件crontab,則cron開始執行。 – RomanP