Chef +自定义systemd脚本

时间:2013-08-29 23:41:01

标签: ubuntu jboss chef systemd

我在Ubuntu 12.04上有一个非常基本的JBoss脚本脚本。我可以通过“sudo service jboss start”或“sudo /etc/init.d/jboss start”成功运行它。我无法与Chef合作。我本以为会这么简单:

service "jboss" do
    action [ :start ]
end

但是没有去。我试过了:

service "jboss" do
    supports :start => true, :stop => true, :status => false
    action [ :start, :enable ]
    start_command "sudo /etc/init.d/jboss start"
end

以及不同的排列。

感谢您的帮助,请参阅下面的系统脚本。

#!/bin/bash
### BEGIN INIT INFO
# Provides:          jboss
# Required-Start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     $local_fs $remote_fs $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start/Stop JBoss AS v7.0.0
# Usage: this file neeed to be renamed jboss
### END INIT INFO


#
#source some script files in order to set and export environmental variables
#as well as add the appropriate executables to $PATH
[ -r /etc/profile.d/java.sh ] && . /etc/profile.d/java.sh
[ -r /etc/profile.d/jboss.sh ] && . /etc/profile.d/jboss.sh

case "$1" in
    start)
        echo "Starting JBoss AS 7.1.1"
        cd /home/jboss/jboss-as-7.1.1.Final/bin/
        sudo -u jboss ./standalone.sh &
    ;;
    stop)
        echo "Stopping JBoss AS 7.1.1"
        sudo -u jboss /home/jboss/jboss-as-7.1.1.Final/bin/jboss-cli.sh --connect command=:shutdown
        #JBOSS_PID=$(ps -ef | grep jboss-as-7.1.1.Final | grep -v grep | awk '{print $2}')
        #kill -15 $JBOSS_PID
        #echo "Sig term sent"
        #echo "Terminating all ml_process"
        #pkill -9 ml_server
    ;;
    *)
        echo "Usage: /etc/init.d/jboss {start|stop}"
        exit 1
    ;;
esac

更新

感谢那些回复的人。我设法取得了一些进展。我从OpsCode下载了JBoss Chef配方:http://community.opscode.com/cookbooks/jboss

原来我的脚本中没有设置JAVA_HOME环境变量。我现在正在使用OpsCode的启动脚本。 JBoss现在从配方开始:

service "jboss" do
  action [ :enable, :start ]
end

但是Chef坚持这条线。我认为它正在等待JBoss“完成”,但因为它应该是一个服务它应该持久。如果我通过SSH连接到实例并终止JBoss,则Chef配方成功完成。如何让Chef配方正常结束?请参阅下面的更新的systemd脚本。

#!/bin/sh
### BEGIN INIT INFO
# Provides:          jboss
# Required-Start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     $local_fs $remote_fs $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start/Stop JBoss AS v7.0.0
### END INIT INFO


export JAVA_HOME=/usr/lib/jvm/java-7-oracle

#define where jboss is - this is the directory containing directories log, bin, conf etc
export JBOSS_HOME=${JBOSS_HOME:-"/home/jboss/jboss-as-7.1.1.Final"}

#define the user under which jboss will run, or use 'RUNASIS' to run as the current user
export JBOSS_USER=${JBOSS_USER:-"jboss"}

#make sure java is in your path
JAVAPTH=${JAVAPTH:-"$JAVA_HOME/bin/java"}
export PATH=$PATH:$JAVAPTH

#source some script files in order to set and export environmental
#variables
#as well as add the appropriate executables to $PATH
[ -r /etc/profile.d/java.sh ] && . /etc/profile.d/java.sh
[ -r /etc/profile.d/jboss.sh ] && . /etc/profile.d/jboss.sh




case "$1" in
    start)
        echo "Starting JBoss AS 7.0.0"
        cd /home/jboss/jboss-as-7.1.1.Final/bin/
        sudo -u jboss sh ${JBOSS_HOME}/bin/standalone.sh
    ;;
    stop)
        echo "Stopping JBoss AS 7.0.0"
        sudo -u jboss sh ${JBOSS_HOME}/bin/jboss-admin.sh --connect
    command=:shutdown
    ;;
    *)
        echo "Usage: /etc/init.d/jboss {start|stop}"
        exit 1
    ;;
esac

exit 0

1 个答案:

答案 0 :(得分:1)

经过很多挫折之后,我能够让Chef用以下方法启动并坚持JBoss:

bash "Start JBoss" do
    cwd "/home/jboss/jboss-as-7.1.1.Final/bin"
    code <<-EOH
    sudo -u jboss start-stop-daemon --start --quiet --background --chdir /home/jboss/jboss-as-7.1.1.Final/bin --chuid jboss --exec /home/jboss/jboss-as-7.1.1.Final/bin/standalone.sh
    EOH
end

非常感谢所有帮助过的人和写过这个的人:https://github.com/octo-technology/master-chef/blob/master/cookbooks/jboss/templates/default/jboss-as-standalone.sh.erb