upstart把应用封装成系统服务
http://blog.fens.me/linux-upstart/
永驻后台之程序或者脚本
-
# /etc/init/cs.conf
-
# cat /etc/issule -> 14.04 或者 Debian (可能有debian 由的systemd的方案, 查看最下面) GNU/Linux 8 已验证
-
# 启用 sudo start cs
-
# 停止 sudo stop cs
-
-
description "iibul_service"
-
author "iibull"
-
-
start on startup
-
stop on shutdown
-
-
respawn
-
-
console output
-
-
# if to run App, then
-
#exec /work/myServ #启用一个后台的应用程序
-
# if you want to script
-
env var=bar
-
export var
-
-
pre-start script
-
logger "pre-start: before: var=$var"
-
var=pre-start
-
export var
-
logger "pre-start: after: var=$var"
-
end script
-
-
post-start script
-
logger "post-start: before: var=$var"
-
var=post-start
-
export var
-
logger "post-start: after: var=$var"
-
end script
-
-
script
-
logger "script: before: var=$var"
-
var=main
-
export var
-
sleep 60000
-
logger "script: after: var=$var"
-
end script
-
-
post-stop script
-
logger "post-stop: before: var=$var"
-
var=post-stop
-
export var
-
logger "post-stop: after: var=$var"
-
end script
%E4%B8%AD%E8%87%AA%E5%AE%9A%E4%B9%89%E6%9C%8D%E5%8A%A1/
在Ubuntu中定义一些服务,可以对该服务进行全局控制,比较常见的就是用sudo service servcie_name start/stop/restart 进行服务的启停
在/etc/init.d/目录下面定义控制服务的脚本
-
service_name="service_name"
-
service_script_path="service_script_path"
-
-
start()
-
{
-
echo "$service_name service start"
-
chmod a+x $service_script_path && nohup ./$service_script_path
-
}
-
-
stop()
-
{
-
echo "$service_name service stop"
-
server_process_id=`ps -aux | grep $service_name | cut -d " " -f 6`
-
echo "server_process_id="server_process_id
-
kill -9 $server_process_id
-
}
-
-
case $1 in
-
start)
-
start
-
;;
-
stop)
-
stop
-
;;
-
restart)
-
echo "resert the $service_name"
-
stop
-
start
-
;;
-
*)
-
echo "undefine operation"
-
;;
-
esac
自己写了个服务脚本扔到/etc/init.d/,service启停服务目前OK了。
使用update-rc.d命令可以设置服务2-5级别开机自动启动。
参照 /etc/init.d/README文档。
-
### BEGIN INIT INFO
-
# Provides: skeleton
-
# Required-Start: $remote_fs $syslog
-
# Required-Stop: $remote_fs $syslog
-
# Should-Start: $portmap
-
# Should-Stop: $portmap
-
# X-Start-Before: nis
-
# X-Stop-After: nis
-
# Default-Start: 2 3 4 5
-
# Default-Stop: 0 1 6
-
# X-Interactive: true
-
# Short-Description: Example initscript
-
# Description: This file should be used to construct scripts to be
-
# placed in /etc/init.d.
-
### END INIT INFO
然后使用命令update-rc.d default myserv 来设置我自定义的服务开机自启动
在rc2-5.d目录下生成了S20myserv
改变优先级别
-
/etc/rc2.d/S20redis-server
-
/etc/rc2.d/S21rtds2
-
2是Ubuntu默认的runlevel,S表示Start的意思。
-
20的优先级比21高,也就是说S20服务先启动,然后才轮到S21服务启动。
-
-
删除原来运行级的启动软链接:
-
sudo update-rc.d -f redis-server remove
-
sudo update-rc.d -f rtds2 remove
-
-
设置新的服务启动优先级:
-
sudo update-rc.d redis-server defaults 20
-
sudo update-rc.d rtds2 defaults 21
例如我们要添加一个开机脚本 运行
# 采自于 http://minecraft.gamepedia.com/Tutorials/Ubuntu_startup_script
#为服务制定专用的用户组 和 分配服务组目录权限
sudo adduser --system --no-create-home --home /srv/minecraft-server minecraft
sudo addgroup --system minecraft
sudo adduser minecraft minecraft # this adds user "minecraft" the group "minecraft"
sudo chown -R minecraft.minecraft /srv/minecraft-server
#写开机脚本 /etc/init/minecraft-server.conf
#create and edit the start/stop script creating a new file under /etc/init/minecraft-server
#(NOT /etc/init.d but /etc/init (without the .d) )
#make sure that you save the file as minecraft-server.conf
sudo nano /etc/init/minecraft-server.conf
#脚本内容如下
## description "start and stop the minecraft-server"
start on runlevel [2345]
stop on runlevel [^2345]
console log
chdir YOUR_INSTALLATION_DIRECTORY
setuid minecraft
setgid minecraft
respawn
respawn limit 20 5
exec /usr/bin/java -Xms1536M -Xmx2048M -jar minecraft_server.jar nogui
#脚本各行的解释信息如下
-----------------------------------------------------------------
# description "start and stop the minecraft-server" <-- Adds a tip to what this script does.
start on runlevel [2345] <-- 何时开启服务. Runlevels 2, 3, 4, and 5 are the "normal operation".
stop on runlevel [^2345] <-- 何时停止服务. The ^ means NOT.
console log <-- Log的提示方式
chdir YOUR_INSTALLATION_DIRECTORY <-- 改变当前的活动目录.
setuid minecraft <-- 切换到 minecraft 用户
setgid minecraft <-- 切换到 minecraft 用户组
respawn <-- 当检测到程序异常时, 允许程序自动重启
respawn limit 20 5 <--自动重启 20次/5秒内
exec /usr/bin/java -Xms1536M -Xmx2048M -jar minecraft_server.jar nogui <-- Below...
'exec' > 执行命令
'/usr/bin/java' > 调用的程序
'-Xms1536M' > Sets minimum allocated memory limit.
'-Xmx2048M' > Sets MAXIMUM allowable memory limit.
'-jar minecraft-server.jar' > Tells Java to run the jarfile minecraft_server.jar
'nogui' > Tells minecraft-server.jar to run without a GUI. (Graphical screen).
#现在是的脚本生效
sudo initctl reload-configuration
#开关服务
sudo start minecraft-server
sudo stop minecraft-server
#查看日志
sudo tail /var/log/upstart/minecraft-server.log
###########################################################################################
#在有systemd的系统内,可以这样做,15.04之后systemd 被取消了。
sudo gedit /etc/systemd/system/minecraft-server.service
[Unit]
Description=start and stop the minecraft-server
[Service]
WorkingDirectory=/srv/minecraft-server
User=minecraft
Group=minecraft
Restart=on-failure
RestartSec=20 5
ExecStart=/usr/bin/java -Xms1536M -Xmx1536M -jar minecraft_server.jar nogui
[Install]
WantedBy=multi-user.target
Alias=minecraft-server.service
sudo service minecraft-server start
sudo systemctl enable minecraft-server
systemctl status minecraft-server.service
SSH 客户端执行GUI程序,实现在客户端上
DISPLAY=:0.0
执行你的程序即可。
阅读(4689) | 评论(0) | 转发(0) |