Git

如何在 Solaris (SmartOS) 上將 git 作為守護程序執行

  • March 16, 2014

如主題所述。我有這個腳本可以從命令行正常工作:

#!/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. ]

我不知道從這裡去哪裡。任何幫助將不勝感激。

好的,所以我在我的 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

如果系統健康,最後一個命令不應列印任何內容。

引用自:https://serverfault.com/questions/582378