如何在 Solaris(SmartOS)上将 git 作为守护进程运行

如何在 Solaris(SmartOS)上将 git 作为守护进程运行

如主题所述。我有这个脚本,可以在命令行中正常运行:

#!/sbin/sh

GITCOMMAND="git daemon --base-path=/my/git/repo --export-all --enable=receive-pack --reuseaddr"

startcmd () {
    `$GITCOMMAND`
}

stopcmd () {
    pkill -f "git *"
}

case "$1" in
'start')
        startcmd
        ;;
'stop')
        stopcmd
        ;;
'restart')
        stopcmd
        sleep 1
        startcmd
        ;;
*)
        echo "Usage: $0 { start | stop | restart }"
        exit 1
        ;;
esac

以及此 SMF 配置:

<?xml version="1.0"?>
<!DOCTYPE service_bundle SYSTEM "/usr/share/lib/xml/dtd/service_bundle.dtd.1">
<service_bundle type="manifest" name="git-server">
  <service name="site/git-server" type="service" version="1">
  <create_default_instance enabled="true"/>
  <single_instance/>
  <dependency name="network" grouping="require_all" restart_on="refresh" type="service">
    <service_fmri value="svc:/milestone/network:default"/>
  </dependency>
  <dependency name="filesystem" grouping="require_all" restart_on="refresh" type="service">
    <service_fmri value="svc:/system/filesystem/local"/>
  </dependency>

  <method_context working_directory="/home/massive/massive-lannister">
    <method_credential user="admin" group="staff" privileges='basic,net_privaddr'  />
    <method_environment>
      <envvar name="PATH" value="/usr/local/bin:/usr/bin:/usr/sbin:/bin"/>
      <envvar name="HOME" value="/develop"/>
    </method_environment>
  </method_context>

  <exec_method
    type="method"
    name="start"
    exec="/usr/local/bin/start-stop-git.sh start"
    timeout_seconds="60"/>

  <exec_method
    type="method"
    name="stop"
    exec="/usr/local/bin/start-stop-git.sh stop"
    timeout_seconds="60"/>

  <property_group name="startd" type="framework">
    <propval name="duration" type="astring" value="child"/>
    <propval name="ignore_error" type="astring" value="core,signal"/>
  </property_group>

  <property_group name="application" type="application">
  </property_group>

  <stability value="Evolving"/>

  <template>
    <common_name>
      <loctext xml:lang="C">git server service</loctext>
    </common_name>
  </template>
  </service>
</service_bundle>

服务注册并启用,命令行上没有任何错误,但无法运行:

# svcs -xv
svc:/site/git-server:default (git server service)
 State: maintenance since Sun Mar 16 02:29:04 2014
Reason: Method failed.
   See: http://illumos.org/msg/SMF-8000-8Q
   See: /var/svc/log/site-git-server:default.log
Impact: This service is not running.

日志文件没有透露太多信息:

# cat /var/svc/log/site-git-server:default.log
[ Mar 16 02:29:04 Enabled. ]
[ Mar 16 02:29:04 Executing start method ("/usr/local/bin/start-stop-git.sh start"). ]
[ Mar 16 02:29:04 Stopping because service exited with a configuration error. ]

我不知道接下来该怎么做。如能得到任何帮助我将不胜感激。

答案1

好的,我在 SMF 配置 XML 中发现了一个问题。如果有人能从中受益,以下是可行的解决方案:

/usr/local/bin/start-stop-git.sh

#!/sbin/sh

GITCOMMAND="git daemon --base-path=/my/git/repo --export-all --enable=receive-pack --reuseaddr"

startcmd () {
    `$GITCOMMAND`
}

stopcmd () {
    pkill -f "git *"
}

case "$1" in
'start')
        startcmd
        ;;
'stop')
        stopcmd
        ;;
'restart')
        stopcmd
        sleep 1
        startcmd
        ;;
*)
        echo "Usage: $0 { start | stop | restart }"
        exit 1
        ;;
esac

git-server.xml:

<?xml version="1.0"?>
<!DOCTYPE service_bundle SYSTEM "/usr/share/lib/xml/dtd/service_bundle.dtd.1">
<service_bundle type="manifest" name="git-server">
  <service name="site/git-server" type="service" version="1">
  <create_default_instance enabled="true"/>
  <single_instance/>
  <dependency name="network" grouping="require_all" restart_on="refresh" type="service">
    <service_fmri value="svc:/milestone/network:default"/>
  </dependency>
  <dependency name="filesystem" grouping="require_all" restart_on="refresh" type="service">
    <service_fmri value="svc:/system/filesystem/local"/>
  </dependency>

  <method_context working_directory="/develop">
    <method_credential user="admin" group="staff" privileges='basic,net_privaddr'  />
    <method_environment>
      <envvar name="PATH" value="/opt/local/bin:/usr/local/bin:/usr/bin:/usr/sbin:/bin"/>
      <envvar name="HOME" value="/develop"/>
    </method_environment>
  </method_context>

  <exec_method
    type="method"
    name="start"
    exec="/usr/local/bin/start-stop-git.sh start"
    timeout_seconds="60"/>

  <exec_method
    type="method"
    name="stop"
    exec="/usr/local/bin/start-stop-git.sh stop"
    timeout_seconds="60"/>

  <property_group name="startd" type="framework">
    <propval name="duration" type="astring" value="child"/>
    <propval name="ignore_error" type="astring" value="core,signal"/>
  </property_group>

  <property_group name="application" type="application">
  </property_group>

  <stability value="Evolving"/>

  <template>
    <common_name>
      <loctext xml:lang="C">git server service</loctext>
    </common_name>
  </template>
  </service>
</service_bundle>

安装并验证:

#svccfg import git-server.xml
#svcadm enable git-server
#svcs -xv

如果系统正常,则最后一个命令不应该打印任何内容。

相关内容