#1 編寫crontab腳本
crontab -e
//最後一行添加下面信息
* * * * * php ~/code/artisan schedule:run >> /dev/null 2>&1
crontab -l
tail -f /var/log/syslog | grep CRON
複製代碼
#2 laravel\app\Console\Kernel.php添加運行命令腳本
protected function schedule(Schedule $schedule) {
if(app()->environment('production')){
}
$schedule->exec("cd /var/go/src/schedules && ./log")
->everyMinute()
->runInBackground();
}
複製代碼
#3 go文件
package main
import (
"fmt"
"log"
"os"
"time"
)
func main() {
log.Fatal(record())
}
func record() error {
filename := time.Now().Format("log-2006-01-02.text")
f, err := os.OpenFile("./logs/"+filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return err
}
text := fmt.Sprintf("%s: record something into the logs\n", time.Now())
_, err = f.WriteString(text)
if err != nil {
return err
}
return nil
}
複製代碼