go项目编译和后台运行(日志)

1、go项目采用gox进行编译 先安装gox包 go get github.com/mitchellh/gox 安装完成后go -h查看是否安装成功,下图表示安装成功 node

图片.png

在你使用 Gox 以前,你必须先有一套交叉编译工具链。Gox 能够自动帮你完成这个。你须要作的只是运行(每次更新 Go 都要这样作这步): gox -build-toolchain 下面便可进行交叉编译,若是你想编译全部平台,只需下面命令便可linux

$ gox

Number of parallel builds: 4

--> darwin/386: github.com/mitchellh/gox
--> darwin/amd64: github.com/mitchellh/gox
--> linux/386: github.com/mitchellh/gox
--> linux/amd64: github.com/mitchellh/gox
--> linux/arm: github.com/mitchellh/gox
--> freebsd/386: github.com/mitchellh/gox
--> freebsd/amd64: github.com/mitchellh/gox
--> openbsd/386: github.com/mitchellh/gox
--> openbsd/amd64: github.com/mitchellh/gox
--> windows/386: github.com/mitchellh/gox
--> windows/amd64: github.com/mitchellh/gox
--> freebsd/arm: github.com/mitchellh/gox
--> netbsd/386: github.com/mitchellh/gox
--> netbsd/amd64: github.com/mitchellh/gox
--> netbsd/arm: github.com/mitchellh/gox
--> plan9/386: github.com/mitchellh/gox
复制代码

由于要在Linux上面运行,因此下面命令进行交叉编译 gox -osarch="linux/amd64"git

2、后台运行,采用nohup(下面linux_amd64文件为上面编译好的文件)github

nohup ./linux_amd64  > nohup.log 2>&1 &
复制代码

执行上面命令,项目已经在后台进行运行,同时在相同目录生成nohup.log的日志文件,日志目录能够指定,...> /var/log/nohup.log >...便可,上面最后&符是后台运行的意思,必须加上。vim

到这里,go项目就完整的部署运行了,可是nohup.log日志文件是一个,长时间的话文件内容会很大,很差管理和查看,下面介绍怎么分割日志文件。windows

3、用系统自带Logrotate,按日期分割日志文件 Logrotate能够自定义分割日志的方式,进入下面目录,建立nohup.logbash

cd /etc/logrotate.d
vim test.log
复制代码

进入文件进行配置添加便可 看一下主配置文件/etc/logrotate.conf的参数解释:工具

/var/log/nohup.log  #定义/var/log/nohup.log这个日志文件;
/home/nohup/nohup.log
{
        missingok
        daily
        create 0664 root utmp
        notifempty
        compress
        delaycompress
        copytruncate
        rotate 2
}
复制代码

logrotate经常使用参数介绍post

daily                     #指定转储周期为天天
weekly                    #指定转储周期为每周;
monthly                   #指定转储周期为每个月;
rotate count              #指定日志文件删除以前转储的次数,0指没有备份,5指保留5个备份;
compress                  #经过gzip压缩转储之后的日志;
nocompress                #不须要压缩时,用这个参数;
delaycompress             #延迟压缩,和compress一块儿使用时,转储的日志文件到下一次转储时才压缩;
nodelaycompress           #覆盖delaycompress选项,转储同时压缩;
copytruncate              #用于还在打开中的日志文件,把当前日志备份并截断;
nocopytruncate            #备份日志文件可是不截断;
create mode owner group   #转储文件,使用指定的文件模式建立新的日志文件;
nocreate                  #不创建新的日志文件;
errors address            #专储时的错误信息发送到指定的Email地址;
ifempty                   #即便是空文件也转储,这个是logrotate的缺省选项;
notifempty                #若是是空文件的话,不转储;
mail address              #把转储的日志文件发送到指定的E-mail地;
nomail                    #转储时不发送日志文件;
olddir directory          #转储后的日志文件放入指定的目录,必须和当前日志文件在同一个文件系统;
noolddir                  #转储后的日志文件和当前日志文件放在同一个目录下;
prerotate/endscript       #在转储之前须要执行的命令能够放入这个对,这两个关键字必须单独成行;
postrotate/endscript      #在转储之后须要执行的命令能够放入这个对,这两个关键字必须单独成行;
tabootext [+] list        #让logrotate不转储指定扩展名的文件,缺省的扩展名是:.rpm-orig, .rpmsave,v,和~ ;
size size                 #当日志文件到达指定的大小时才转储,Size能够指定bytes(缺省)以及KB(sizek)或者MB(sizem);
postrotate                #日志轮换事后指定指定的脚本,endscript参数表示结束脚本;
sharedscripts             #共享脚本,下面的postrotate中的脚本只执行一次便可;

以上参数均可以已定义在全局配置,或者指定为某个日志文件对的配置,但注意使用时参数之间不要冲突。
复制代码

更详细的能够看这篇文章测试

配置完毕,能够手动如下命令测试一下

/usr/sbin/logrotate -f /etc/logrotate.conf
复制代码

若是看到相同目录下

图片.png
即成功
相关文章
相关标签/搜索