linux开机自启动_chkconfig
linux开机自启动_chkconfig
- 准备可执行程序 g
- 准备shell脚本 s,s里面是执行g, 放在
/etc/init.d
目录下 - 执行 chkconfig --add s
对于凝思,假如程序是守护程序,但是手动启动耗时,需要重启,所以需要上面的方式弄一个重启服务
func (this *Installer) RestartDog() {
shellName := "/etc/init.d/rsdog"
exit, _ := PathExists(shellName)
if exit {
if err := os.Remove(shellName); err != nil {
fmt.Errorf("delete errors:%s", err)
}
}
var buf string
fp, err := os.OpenFile(shellName, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
fmt.Errorf("write shell err:", err.Error())
}
defer func() {
fp.Sync()
fp.Close()
}()
writer := bufio.NewWriter(fp)
log.Println("path", this.Path)
if this.Path == "." {
dir, err := os.Getwd()
if err != nil {
log.Println("Get Present Path file", err)
return
}
this.Path = dir
}
log.Println("path", this.Path)
buf = `#!/bin/sh
### BEGIN INIT INFO
# Provides: rsdog
# 凝思平台重启守护
OPDIR=` + this.Path + "\n\n" +
`
dir=$OPDIR
sleep 60
job=` + "`" + `pidof $OPDIR/g` + "`" + `
pid="$job"
# 进行杀死,重新启动
kill -9 $pid
$dir/g >/dev/null 2>&1 &
`
writer.WriteString(buf)
writer.Flush()
err = exec.Command("/bin/sh", "-c", "chmod +x " + shellName).Run()
if err != nil {
log.Println(err)
}
err = exec.Command("/bin/sh", "-c", "cd /etc/init.d/ && chkconfig --add rsdog").Run()
if err != nil {
log.Println(err)
}
}