一键制作程序启动脚本
🕥 by pyList at 2019-06-16 09:46
VPS 上程序的启动脚本,就是放在/etc/init.d
下的文件,随系统启动,能用 service xxx {start|stop|status|restart|uninstall} 控制程序
简单的一键制作是建立在别人的努力上:
一行命令:
wget 'https://raw.githubusercontent.com/jasonblewis/sample-service-script/master/new-service.sh' && bash new-service.sh
运行如下,需要填写的信息:
- Service name : myapp
- Description : "myapp des"
- Command : /root/bin/myapp
- User : root
全程示例:
# wget 'https://raw.githubusercontent.com/jasonblewis/sample-service-script/master/new-service.sh' && bash new-service.sh
--2019-06-16 01:37:47-- https://raw.githubusercontent.com/jasonblewis/sample-service-script/master/new-service.sh
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.0.133, 151.101.64.133, 151.101.128.133, ...
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.0.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2980 (2.9K) [text/plain]
Saving to: 'new-service.sh'
new-service.sh 100%[================================================================================================================>] 2.91K --.-KB/s in 0s
2019-06-16 01:37:47 (63.8 MB/s) - 'new-service.sh' saved [2980/2980]
--- Download template ---
I'll now download the service.sh, because is is not downloaded.
...
I donloaded the tmplate sucessfully
--- Copy template ---
--- Customize ---
I'll now ask you some information to customize script
Press Ctrl+C anytime to abort.
Empty values are not accepted.
Service name : myapp
Description : "myapp des"
Command : /root/bin/myapp
User : root
--- Installation ---
1. mv "/tmp/file5Sipy2" "/etc/init.d/myapp"
'/tmp/file5Sipy2' -> '/etc/init.d/myapp'
2. touch "/var/log/myapp.log" && chown "root" "/var/log/myapp.log"
3. update-rc.d "myapp" defaults
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
LANGUAGE = (unset),
LC_ALL = (unset),
LANG = "zh_CN.UTF-8"
are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
4. service "myapp" start
---Uninstall instructions ---
The service can uninstall itself:
service "myapp" uninstall
It will simply run update-rc.d -f "myapp" remove && rm -f "/etc/init.d/myapp"
--- Terminated ---
此时, /etc/init.d/
下有一个可实行文件 myapp
,其内容如下:
#!/bin/sh
### BEGIN INIT INFO
# Provides: myapp
# Required-Start: $local_fs $network $named $time $syslog
# Required-Stop: $local_fs $network $named $time $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: \"myapp\ des\"
### END INIT INFO
SCRIPT="/root/bin/myapp"
RUNAS=root
PIDFILE=/var/run/myapp.pid
LOGFILE=/var/log/myapp.log
start() {
if [ -f $PIDFILE ] && [ -s $PIDFILE ] && kill -0 $(cat $PIDFILE); then
echo 'Service already running' >&2
return 1
fi
echo 'Starting service…' >&2
local CMD="$SCRIPT &> \"$LOGFILE\" & echo \$!"
su -c "$CMD" $RUNAS > "$PIDFILE"
# Try with this command line instead of above if not workable
# su -s /bin/sh $RUNAS -c "$CMD" > "$PIDFILE"
sleep 2
PID=$(cat $PIDFILE)
if pgrep -u $RUNAS -f $NAME > /dev/null
then
echo "$NAME is now running, the PID is $PID"
else
echo ''
echo "Error! Could not start $NAME!"
fi
}
stop() {
if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then
echo 'Service not running' >&2
return 1
fi
echo 'Stopping service…' >&2
kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE"
echo 'Service stopped' >&2
}
uninstall() {
echo -n "Are you really sure you want to uninstall this service? That cannot be undone. [yes|No] "
local SURE
read SURE
if [ "$SURE" = "yes" ]; then
stop
rm -f "$PIDFILE"
echo "Notice: log file was not removed: $LOGFILE" >&2
update-rc.d -f $NAME remove
rm -fv "$0"
else
echo "Abort!"
fi
}
status() {
printf "%-50s" "Checking myapp..."
if [ -f $PIDFILE ] && [ -s $PIDFILE ]; then
PID=$(cat $PIDFILE)
if [ -z "$(ps axf | grep ${PID} | grep -v grep)" ]; then
printf "%s\n" "The process appears to be dead but pidfile still exists"
else
echo "Running, the PID is $PID"
fi
else
printf "%s\n" "Service not running"
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
uninstall)
uninstall
;;
restart)
stop
start
;;
*)
echo "Usage: $0 {start|stop|status|restart|uninstall}"
esac
本文网址: https://pylist.com/t/1560649568 (转载注明出处)
如果你有任何建议或疑问可以在下面 留言
发表第一条评论!
相关推荐
小工具
标签
js
fonts
css
html
一键
自动更新
程序
文件
判断
不同
gae
缓存
优化
正确
text
sublime
内存
暴涨
解决
openwrt
usb
上网卡
共享
网络
go2o
golang
开源
完整
商业
md5
计算
语言包
go
镜像
国内
python
中文
实体
转换
cjson
lua
module
错误
字典
排序
最快
传说
ssdb
遍历
数据库
方法
linux
ulimit
永久
设置
系统
socks5
代理
依赖
安装
使用
低功耗
web
爬虫
服务器
组装
bbr
路由
开启
系统启动
ubuntu
mac
debian
默认值
struct
换行
字符串
替换
failed
load
devtools
sourcemap
chrome
microsoft
关闭
app
store
登录
未知
静音
风扇
主机
笔记本
改造
微信
尝鲜
视频
体验
cpu
查看
温度
server
浏览
google
身份验证
authenticator
迁移
手机
webdriver
微博
selenium
gnu
固件
编译
宅家
坑记
屏幕
动手
硬件加速
ubnt
er
nginx
quic
抢先
最近发表
- Chrome 控制台 DevTools failed to load SourceMap 警告的消除方法
- Mac 关闭 Microsoft 自动更新
- Mac 登录 App Store 出现“发生了未知错误”的解决方法
- 老笔记本改造为无风扇静音主机方案
- 自己组装21瓦低功耗家庭爬虫、文件、web服务器
- 微信视频号尝鲜体验
- Ubuntu/Debian 查看CPU温度的方法
- 在Ubuntu/debian Server 系统使用Chrome 无头浏览模式
- 换手机后 Google 身份验证器 Google Authenticator 数据迁移的简单方法
- 使用Golang selenium WebDriver 自动登录微博
- 在 Ubuntu 或其它 GNU/Linux 系统下安装 Debian
- Mac 下制作 USB ubuntu/debian 系统启动、安装盘的几种方法
- ubuntu/debian 下自行编译 OpenWRT 固件
- 宅家自己动手换手机屏幕掉坑记
- 路由 UBNT ER-X 官方固件升级及开启硬件加速的方法
- 在 Nginx 和 Golang web 上抢先体验 QUIC
最近浏览
- 解决Sublime text 内存暴涨的问题
- Openwrt 使用USB 4G 上网卡共享网络
- go2o: 一个完整的golang 商业开源项目
- golang 计算大文件md5
- Go 语言包国内镜像
- 用python 实现中文与html实体相互转换
- 出现 lua module 'cjson' not found 错误的解决方法
- 传说中最快的python字典排序方法
- SSDB 数据库遍历的方法
- Linux 系统永久设置ulimit
- go get 使用socks5 代理安装依赖库
- 自己组装21瓦低功耗家庭爬虫、文件、web服务器
- Openwrt 路由上开启BBR
- Mac 下制作 USB ubuntu/debian 系统启动、安装盘的几种方法
- go struct 设置默认值
- Sublime 把字符串替换为换行