为 spring boot 程序创建 service

把应用做成一个service

我们经常会自己用Java或Python等写一些程序,例如我会写一些个人管理的 spring boot 小程序(日记/事务/笔记管理等),但是每次都要手动去启动和关闭,好麻烦。
为了方便管理,可以考虑制作成一个service。步骤如下:

创建 upstart 配置文件

首先用mvn package打包成jar类型的spring-boot程序, 然后 sudo vim /etc/init/hellosb.conf

start on filesystem
exec /usr/bin/java -jar /path_to/program.jar

多行的话使用script标记:

# myprogram.conf
start on filesystem
script
/usr/bin/java -jar /path_to/program
echo "Another command"
end script

service 启动

sudo ln -s /etc/init/myprogram.conf /etc/init.d/myprogram

建立链接后,使用service就可以用自动补全功能了:

sudo service myprogram start

上面只是一个简单的启动功能,upstart的配置文件远比这强大,可以做更细节的处理(下图是job的状态变迁图)。

job 状态变迁

更进一步

若想要深入了解,可以查看官方文档和学习一下其他程序(nginx等)的upstart脚本:Upstart Intro, Cookbook and Best Practises

参考


http://upstart.ubuntu.com/cookbook/
http://askubuntu.com/questions/351879/how-to-create-a-service-on-ubuntu-upstart
http://upstart.ubuntu.com/getting-started.html
http://askubuntu.com/questions/299371/correct-way-to-install-a-custom-upstart-service