代码拉取完成,页面将自动刷新
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by
// license that can be found in the LICENSE file.
package daemon
import (
"os"
"os/exec"
"regexp"
"strings"
"text/template"
)
// systemVRecord - standard record (struct) for linux systemV version of daemon package
type systemVRecord struct {
name string
description string
kind Kind
dependencies []string
}
// Standard service path for systemV daemons
func (linux *systemVRecord) servicePath() string {
return "/etc/init.d/" + linux.name
}
// Is a service installed
func (linux *systemVRecord) isInstalled() bool {
if _, err := os.Stat(linux.servicePath()); err == nil {
return true
}
return false
}
// Check service is running
func (linux *systemVRecord) checkRunning() (string, bool) {
output, err := exec.Command("service", linux.name, "status").Output()
if err == nil {
if matched, err := regexp.MatchString(linux.name, string(output)); err == nil && matched {
reg := regexp.MustCompile("pid ([0-9]+)")
data := reg.FindStringSubmatch(string(output))
if len(data) > 1 {
return "Service (pid " + data[1] + ") is running...", true
}
return "Service is running...", true
}
}
return "Service is stopped", false
}
// Install the service
func (linux *systemVRecord) Install(args ...string) (string, error) {
installAction := "Install " + linux.description + ":"
if ok, err := checkPrivileges(); !ok {
return installAction + failed, err
}
srvPath := linux.servicePath()
if linux.isInstalled() {
return installAction + failed, ErrAlreadyInstalled
}
file, err := os.Create(srvPath)
if err != nil {
return installAction + failed, err
}
defer file.Close()
execPatch, err := executablePath(linux.name)
if err != nil {
return installAction + failed, err
}
templ, err := template.New("systemVConfig").Parse(systemVConfig)
if err != nil {
return installAction + failed, err
}
if err := templ.Execute(
file,
&struct {
Name, Description, Path, Args string
}{linux.name, linux.description, execPatch, strings.Join(args, " ")},
); err != nil {
return installAction + failed, err
}
if err := os.Chmod(srvPath, 0755); err != nil {
return installAction + failed, err
}
for _, i := range [...]string{"2", "3", "4", "5"} {
if err := os.Symlink(srvPath, "/etc/rc"+i+".d/S87"+linux.name); err != nil {
continue
}
}
for _, i := range [...]string{"0", "1", "6"} {
if err := os.Symlink(srvPath, "/etc/rc"+i+".d/K17"+linux.name); err != nil {
continue
}
}
return installAction + success, nil
}
// Remove the service
func (linux *systemVRecord) Remove() (string, error) {
removeAction := "Removing " + linux.description + ":"
if ok, err := checkPrivileges(); !ok {
return removeAction + failed, err
}
if !linux.isInstalled() {
return removeAction + failed, ErrNotInstalled
}
if err := os.Remove(linux.servicePath()); err != nil {
return removeAction + failed, err
}
for _, i := range [...]string{"2", "3", "4", "5"} {
if err := os.Remove("/etc/rc" + i + ".d/S87" + linux.name); err != nil {
continue
}
}
for _, i := range [...]string{"0", "1", "6"} {
if err := os.Remove("/etc/rc" + i + ".d/K17" + linux.name); err != nil {
continue
}
}
return removeAction + success, nil
}
// Start the service
func (linux *systemVRecord) Start() (string, error) {
startAction := "Starting " + linux.description + ":"
if ok, err := checkPrivileges(); !ok {
return startAction + failed, err
}
if !linux.isInstalled() {
return startAction + failed, ErrNotInstalled
}
if _, ok := linux.checkRunning(); ok {
return startAction + failed, ErrAlreadyRunning
}
if err := exec.Command("service", linux.name, "start").Run(); err != nil {
return startAction + failed, err
}
return startAction + success, nil
}
// Stop the service
func (linux *systemVRecord) Stop() (string, error) {
stopAction := "Stopping " + linux.description + ":"
if ok, err := checkPrivileges(); !ok {
return stopAction + failed, err
}
if !linux.isInstalled() {
return stopAction + failed, ErrNotInstalled
}
if _, ok := linux.checkRunning(); !ok {
return stopAction + failed, ErrAlreadyStopped
}
if err := exec.Command("service", linux.name, "stop").Run(); err != nil {
return stopAction + failed, err
}
return stopAction + success, nil
}
// Status - Get service status
func (linux *systemVRecord) Status() (string, error) {
if ok, err := checkPrivileges(); !ok {
return "", err
}
if !linux.isInstalled() {
return statNotInstalled, ErrNotInstalled
}
statusAction, _ := linux.checkRunning()
return statusAction, nil
}
// Run - Run service
func (linux *systemVRecord) Run(e Executable) (string, error) {
runAction := "Running " + linux.description + ":"
e.Run()
return runAction + " completed.", nil
}
// GetTemplate - gets service config template
func (linux *systemVRecord) GetTemplate() string {
return systemVConfig
}
// SetTemplate - sets service config template
func (linux *systemVRecord) SetTemplate(tplStr string) error {
systemVConfig = tplStr
return nil
}
var systemVConfig = `#! /bin/sh
#
# /etc/rc.d/init.d/{{.Name}}
#
# Starts {{.Name}} as a daemon
#
# chkconfig: 2345 87 17
# description: Starts and stops a single {{.Name}} instance on this system
### BEGIN INIT INFO
# Provides: {{.Name}}
# Required-Start: $network $named
# Required-Stop: $network $named
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: This service manages the {{.Description}}.
# Description: {{.Description}}
### END INIT INFO
#
# Source function library.
#
if [ -f /etc/rc.d/init.d/functions ]; then
. /etc/rc.d/init.d/functions
fi
exec="{{.Path}}"
servname="{{.Description}}"
proc="{{.Name}}"
pidfile="/var/run/$proc.pid"
lockfile="/var/lock/subsys/$proc"
stdoutlog="/var/log/$proc.log"
stderrlog="/var/log/$proc.err"
[ -d $(dirname $lockfile) ] || mkdir -p $(dirname $lockfile)
[ -e /etc/sysconfig/$proc ] && . /etc/sysconfig/$proc
start() {
[ -x $exec ] || exit 5
if [ -f $pidfile ]; then
if ! [ -d "/proc/$(cat $pidfile)" ]; then
rm $pidfile
if [ -f $lockfile ]; then
rm $lockfile
fi
fi
fi
if ! [ -f $pidfile ]; then
printf "Starting $servname:\t"
echo "$(date)" >> $stdoutlog
$exec {{.Args}} >> $stdoutlog 2>> $stderrlog &
echo $! > $pidfile
touch $lockfile
success
echo
else
# failure
echo
printf "$pidfile still exists...\n"
exit 7
fi
}
stop() {
echo -n $"Stopping $servname: "
killproc -p $pidfile $proc
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
stop
start
}
rh_status() {
status -p $pidfile $proc
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart)
$1
;;
status)
rh_status
;;
*)
echo $"Usage: $0 {start|stop|status|restart}"
exit 2
esac
exit $?
`
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。