有很多方法可以剥猫皮...
您可以使用 sbt-start-script https://github.com/sbt/sbt-start-script 甚至 sbt-native-packager https://github.com/sbt/sbt-native-packager
您可以将Spray的Boot示例脚本包装在一个简单的init.d脚本中,该脚本调用sbt,如本答案 https://stackoverflow.com/a/17399574/155689 中所述,或者仅使用纯java命令。nohup
您可以创建更大的守护程序感知类和脚本,或者使用使用 Jsvc http://commons.apache.org/proper/commons-daemon/jsvc.html 或 Java Service Wrapper 的 init.d 脚本扩展这些类和脚本。http://wrapper.tanukisoftware.com/
守护程序和应用程序类的示例:
package com.example.myapplication.server
import akka.actor.{Props, ActorSystem}
import spray.can.Http
import akka.io.IO
import com.example.myapplication.api.MyServiceActor
import org.apache.commons.daemon._
trait ApplicationLifecycle {
def start(): Unit
def stop(): Unit
}
abstract class AbstractApplicationDaemon extends Daemon {
def application: ApplicationLifecycle
def init(daemonContext: DaemonContext) {}
def start() = application.start()
def stop() = application.stop()
def destroy() = application.stop()
}
class ApplicationDaemon() extends AbstractApplicationDaemon {
def application = new Application
}
object ServiceApplication extends App {
val application = createApplication()
def createApplication() = new ApplicationDaemon
private[this] var cleanupAlreadyRun: Boolean = false
def cleanup(){
val previouslyRun = cleanupAlreadyRun
cleanupAlreadyRun = true
if (!previouslyRun) application.stop()
}
Runtime.getRuntime.addShutdownHook(new Thread(new Runnable {
def run() {
cleanup()
}
}))
application.start()
}
class Application() extends ApplicationLifecycle with Logging {
private[this] var started: Boolean = false
private val applicationName = "MyApplication"
implicit val actorSystem = ActorSystem(s"$applicationName-system")
def start() {
logger.info(s"Starting $applicationName Service")
if (!started) {
started = true
val myService = actorSystem.actorOf(Props[MyServiceActor], "my-service")
IO(Http) ! Http.Bind(myService, interface = "0.0.0.0", port = 8280)
}
}
def stop() {
logger.info(s"Stopping $applicationName Service")
if (started) {
started = false
actorSystem.shutdown()
}
}
}
如果在 中部署 jar(对胖 jar 使用 sbt-assembly),请在文件夹中添加一些外部配置,则可以将其包装在脚本中,例如使用 Jsvc:/opt/myapplication/myapplication.jar
/etc/mycompany
/etc/init.d/myapplication
#!/bin/sh
### BEGIN INIT INFO
# Provides: myapplication
# Required-Start: $local_fs $remote_fs $network
# Required-Stop: $local_fs $remote_fs $network
# Should-Start: $named
# Should-Stop: $named
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Control myapplication
# Description: Control the myapplication daemon.
### END INIT INFO
set -e
if [ -z "${JAVA_HOME}" ]; then
JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:/bin/java::")
fi
JAVA_OPTS="-Xms512m -Xmx1024m"
APP=myapplication
PID=/var/run/${APP}.pid
OUT_LOG=/var/log/myapplication/${APP}_out.log
ERR_LOG=/var/log/myapplication/${APP}_err.log
DAEMON_USER=yourserviceuser
APP_LOG_CONFIG=/etc/mycompany/${APP}_logback.xml
APP_CONFIG=/etc/mycompany/${APP}.conf
APP_HOME=/opt/${APP}
APP_CLASSPATH=$APP_HOME/${APP}.jar
APP_CLASS=com.example.myapplication.server.ApplicationDaemon
if [ -n "$APP_LOG_CONFIG}" ]; then
JAVA_OPTS="-Dlogback.configurationFile=${APP_LOG_CONFIG} ${JAVA_OPTS}"
fi
DAEMON_ARGS="-home ${JAVA_HOME} -Dconfig.file=${APP_CONFIG} ${JAVA_OPTS} -pidfile ${PID} -user ${DAEMON_USER} -outfile ${OUT_LOG} -errfile ${ERR_LOG} -cp ${APP_CLASSPATH} ${APP_CLASS}"
. /lib/lsb/init-functions
case "$1" in
start)
log_daemon_msg "Starting ${APP}"
cd ${APP_HOME} && jsvc ${DAEMON_ARGS}
log_end_msg 0
;;
stop)
log_daemon_msg "Stopping ${APP}"
cd ${APP_HOME} && jsvc -stop ${DAEMON_ARGS}
log_end_msg 0
;;
*)
log_success_msg "Usage: {start|stop}"
echo "Usage: {start|stop}"
exit 1
;;
esac
exit 0
有了这个,你现在可以sudo service myapplication start|stop
如果如前所述,您希望它在启动时自动启动,请运行此命令
sudo update-rc.d myapplication defaults
此守护程序方法适用于我拥有的Spray应用程序。