laravel任務調度go寫的程序

#1 編寫crontab腳本

#編輯
crontab -e

//最後一行添加下面信息
* * * * * php ~/code/artisan schedule:run >> /dev/null 2>&1

#查看crontab列表
crontab -l

#查看crontab運行狀況
tail -f /var/log/syslog | grep CRON
複製代碼

#2 laravel\app\Console\Kernel.php添加運行命令腳本

/** * Define the application's command schedule. * * @param \Illuminate\Console\Scheduling\Schedule $schedule * @return void */
    protected function schedule(Schedule $schedule) {
        if(app()->environment('production')){
            //TODOS

        }
        //test go schedules
        $schedule->exec("cd /var/go/src/schedules && ./log")
            ->everyMinute()
            ->runInBackground();
            //->sendOutputTo(storage_path('logs/go/test.log'));
    }
複製代碼

#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
}
複製代碼
相關文章
相關標籤/搜索