Python3将web服务和脚本做成开机自启
1.将bwService文件放到 /etc/init.d/下
bwService文件(类型是文件)
#!/bin/bash
#
# This shell script takes care of starting and stopping
# the Bw system.
#
# chkconfig: - 99 01
# description: Bw Service.
# processname: BwService
# pidfile: /var/run/BwService.pid
# version: V3.0.0.0 myname=`whoami`
pid_file=bwService.pid
#. /lib/lsb/init-functions
python_prog=python3
svc_arg=/home/bw/repo/switch_ssh.py
web_arg="/opt/zabbix3d/manage.py runserver 0.0.0.0:8000"
makeAbsolute() {
case $1 in
/*)
# already absolute, return it
echo "$1"
;;
*)
# relative, prepend $2 made absolute
echo `makeAbsolute "$2" "$PWD"`/"$1" | sed 's,/\.$,,'
;;
esac
} getlibdir(){
for element in `ls $1`
do
dir_or_file=$1"/"$element
if [ -d $dir_or_file ]
then
libdir="$libdir:$dir_or_file"
getlibdir $dir_or_file
fi
done
} BOOTUP=color
RES_COL=60
MOVE_TO_COL="echo -en \\033[${RES_COL}G"
SETCOLOR_SUCCESS="echo -en \\033[1;32m"
SETCOLOR_FAILURE="echo -en \\033[1;31m"
SETCOLOR_NORMAL="echo -en \\033[0;39m" # define some functions to be used by most or all shell scripts
# -------------------------------functions start------------------------------ echo_success() {
txt="OK"
if [ ! "$1" = "" ]; then
txt="$1"
fi
[ "$BOOTUP" = "color" ] && $MOVE_TO_COL
echo -n "[ "
[ "$BOOTUP" = "color" ] && $SETCOLOR_SUCCESS
echo -n $"$txt"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
echo -n " ]"
echo -e "\r"
return 0
} echo_failure() {
txt="FAILED"
if [ ! "$1" = "" ]; then
txt="$1"
fi
[ "$BOOTUP" = "color" ] && $MOVE_TO_COL
echo -n "["
[ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
echo -n $"$txt"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
echo -n "]"
echo -e "\r"
return 1
} #test -x $PROGRAM || exit 0 # Check if $pid (could be plural) are running
checkpid() {
local i for i in $* ; do
[ -d "/proc/$i" ] && return 0
done
return 1
} #get the pid
getpid() {
ps -ef|grep -v grep| grep -v '/usr/bin' |grep "$python_prog $1" | awk '{print $2}'
#cmd="ps -ef|grep -v grep |grep \""
#for arg in $*; do
#cmd=${cmd}" "${arg}
#done
#cmd="${cmd}\" | awk '{print \$2}'"
#echo $cmd
#pid=`$cmd`
#echo $$
#pid=$(ps -ef|grep $1|grep "$2"|awk '{printf $2}')
#echo $pid
#if [ -n $2 ]; then #ps -ef|grep "$1" |grep "$2"| awk '{print $2}'
#else
#ps -ef|grep "$1" | awk '{print $2}'
#fi } #get the port
getport()
{
grep "\sPORT_NUM" "$path" | grep -Eo "[0-9]+"
}
#get root user
get_root_user()
{
if [ "x" = "x$ners_user" ] || [ "$ners_user" = "root" ]; then
ners_user=`ls -ld "$ners_home/." 2>/dev/null | awk 'NR==1{print $3}'`
export ners_user
fi
} #get the pid username
getpiduser()
{
ps -eo user,pid,args | grep -F "$path" | grep "$prog_path/$1" | awk '$3 !~/^[su|-]/{print $1}'
} # A function to stop a program.
killproc() {
RC=0
# Test syntax.
if [ "$#" -eq 0 ]; then
echo $"Usage: killproc {program} [signal]"
return 1
fi notset=0
# check for second arg to be kill level
if [ -n "$2" ]; then
killlevel=$2
else
notset=1
killlevel=""
fi pid=`getpid $1`
echo $pid
# Kill it.
if [ -n "${pid:-}" ] ; then
[ "$BOOTUP" = "verbose" -a -z "$LSB" ] && echo -n "$2 "
if [ "$notset" -eq "1" ] ; then
# TERM first, then KILL if not dead
kill -TERM $pid
while checkpid $pid; do
sleep 1
done
checkpid $pid
RC=$?
[ "$RC" -eq 0 ]
RC=`expr 1 - $RC `
# use specified level only
else
if checkpid $pid; then
kill -9 $pid
while checkpid $pid; do
sleep 1
done
RC=$?
[ "$RC" -eq 0 ]
fi
fi
else
echo $"$1 shutdown"
RC=1
fi # Remove pid file if any.
if [ "$notset" = "1" ]; then
rm -f /var/run/$pid_file
fi return $RC
} start(){
pid=`getpid $2`
if [ -n "$pid" ]
then
echo "$1 服务已经在运行."
return 1
fi
echo -n $"启动$1服务: "
dirPath=`dirname $2`
nohup $python_prog $2 $3 $4 >> "$dirPath/output.log" 2>&1 & ret=$?
if [ $ret -eq 0 ]; then
sleep 5
pid=`getpid $2`
if [ -n "$pid" ]; then
echo_success
else
echo_failure
cat "$dirPath/output.log"
return 1
fi
else
echo_failure
cat "$dirPath/output.log"
return 1
fi if [ $ret -eq 0 ] && [ -w "/var/run/" ]
then
touch /var/run/$pid_file
pid=`getpid $2`
echo $pid > /var/run/$pid_file
fi
return $ret
} stop(){ pid=`getpid $2` dirPath=`dirname $2`
if [ -n "$pid" ]; then
echo -n $"停止$1服务: "
killproc $2 > "$dirPath/output.log" 2>&1
ret=$?
if [ $ret -eq 0 ]; then
echo_success
else
echo_failure
cat "$dirPath/output.log"
fi if [ $ret -eq 0 ] && [ -w "/var/run/$pid_file" ]
then
rm -f /var/run/$pid_file
fi
else
echo -n "停止$1服务"
echo_success
return 0
fi
return $ret
}
startSvc(){
start "交换机" $svc_arg
start "网站" $web_arg
} stopSvc(){
stop "交换机" $svc_arg
stop "网站" $web_arg
} restart(){
stopSvc
startSvc
}
restartWeb(){
stopSvc
startSvc
}
list(){
pid=`getpid $2`
if [ -n "$pid" ]
then
echo -n "$1服务"
echo_success "正在运行"
else
echo -n "$1服务"
echo_failure "停止运行"
fi
return $ret
} case "$1" in
start)
start "交换机" $svc_arg
start "网站" $web_arg
;;
stop)
stop "交换机" $svc_arg
stop "网站" $web_arg
;;
startswitch)
start "交换机" $svc_arg
;;
stopswitch)
stop "交换机" $svc_arg
;;
startweb)
start "网站" $web_arg
;;
stopweb)
stop "网站" $web_arg
;;
restart)
restart
;;
status)
list "交换机" $svc_arg
list "网站" $web_arg
;;
*)
exit 1
esac
exit 0
相关命令
[root@localhost ~]# cd /etc/init.d/
[root@localhost init.d]# rz
[root@localhost init.d]# ls
bwService functions netconsole network README
2.bwService赋予执行权限
[root@localhost init.d]# chmod +x bwService
3.添加bwService到centos服务里
[root@localhost init.d]# chkconfig --add bwService
4.创建相关软连接
[root@localhost init.d]# ln -s /etc/init.d/bwService /etc/rc0.d/K03bwService
[root@localhost init.d]# ln -s /etc/init.d/bwService /etc/rc1.d/K03bwService
[root@localhost init.d]# ln -s /etc/init.d/bwService /etc/rc2.d/S98bwService
[root@localhost init.d]# ln -s /etc/init.d/bwService /etc/rc3.d/S98bwService
[root@localhost init.d]# ln -s /etc/init.d/bwService /etc/rc4.d/S98bwService
[root@localhost init.d]# ln -s /etc/init.d/bwService /etc/rc5.d/S98bwService
5.查看服务状态,开启服务
[root@localhost init.d]# service bwService status
交换机服务 [停止运行]
网站服务 [停止运行]
[root@localhost init.d]# service bwService start
启动交换机服务: [ OK ]
启动网站服务: [ OK ]
[root@localhost init.d]#
Python3将web服务和脚本做成开机自启的更多相关文章
- centos6服务启动脚本及开机启动过程
centos6服务启动脚本 centos6的服务启动脚本都放在/etc/rc.d/init.d/下,/etc/init.d/是/etc/rc.d/init.d/的软链接: centos6的服务启动脚本 ...
- 开发nginx启动脚本及开机自启管理(case)
往往我们在工作中需要自行写一些脚本来管理服务,一旦服务异常或宕机等问题,脚本无法自行管理,当然我们可以写定时任务或将需要管理的脚本加入自启等方法来避免这种尴尬的事情,case适用与写启动脚本,下面给大 ...
- MongoDB安装并设置为windows服务以使其开机自启
在MongoDB的官方下载windows平台的压缩zip文件,地址:https://www.mongodb.org/dr/fastdl.mongodb.org/win32/mongodb-win32- ...
- Centos 下添加开机自启动服务和脚本
最近刚玩Centos7的系统,跟Centos6还是很多方面有改变的,这里记录一下怎么在Centos7下添加开机自启动脚本和服务的方法. 1.添加开机自启服务 我这里以docker 服务为例,设置如下两 ...
- Centos 下添加开机自启动服务和脚本【转】
最近刚玩Centos7的系统,跟Centos6还是很多方面有改变的,这里记录一下怎么在Centos7下添加开机自启动脚本和服务的方法. 1.添加开机自启服务 我这里以docker 服务为例,设置如下两 ...
- Linux开机自启应用&开机执行脚本&监听端口应用挂掉了执行启动脚本
linux开机自启 背景 目前要部署一个spring boot框架的jar包,实现开机启动项目或者应用挂掉了 执行启动脚本 在root目录下有一个启动项目的脚本: app_start.sh app_s ...
- 树莓派 ubuntu16.04 安装SSH 配置SSH 开机自启SSH
入手个树莓派3B 装了 ubuntu 16.04 需要用到SSH 记录下 0.先获得树莓派IP 树莓派 使用网线连接路由器和树莓派 在路由器设置页面(一般是192.168.1.1具体看路由器的型号和设 ...
- svn + nginx unit + python3自动化发布web服务方法
本周将python web服务管理更换成nginx unit以后发现接口性能有了明显的提升,访问速度快了不少.不过有个很大的问题就是使用svn自动化发布以后,服务并没有刷新使用新的代码运行,而又不懂得 ...
- 开机自动启动WEB服务,共享目录。
最近工作中,需要共享一个目录,每次重启总要手动执行一下 nohup python -m SimpleHTTPServer 8000这个命令,想着实现让它开机自动启动,就一劳永逸了. 手动步骤如下: ...
- Linux Oracle服务启动&停止脚本与开机自启动
在CentOS 6.3下安装完Oracle 10g R2,重开机之后,你会发现Oracle没有自行启动,这是正常的,因为在Linux下安装Oracle的确不会自行启动,必须要自行设定相关参数,首先先介 ...
随机推荐
- gcc系列工具 介绍
编译器相关知识学习 GNU GCC简介 GNU GCC是一套面向嵌入式领域的交叉编译工具,支持多种编程语言.多种优化选项并且能够支持分步编译.支持多种反汇编方式.支持多种调试信息格式,目前支持X86. ...
- DVT:华为提出动态级联Vision Transformer,性能杠杠的 | NeurIPS 2021
论文主要处理Vision Transformer中的性能问题,采用推理速度不同的级联模型进行速度优化,搭配层级间的特征复用和自注意力关系复用来提升准确率.从实验结果来看,性能提升不错 来源:晓飞的算法 ...
- SpringBoot集成MQTT
MQTT介绍 MQTT 是基于 Publish/Subscribe(发布/订阅) 模式的物联网通信协议,凭借简单易实现.支持 QoS.报文小等特点. 其具有协议简洁.⼩巧.可扩展性强.省流量.省电等优 ...
- 物联网浏览器(IoTBrowser)-基于计算机视觉开发的应用“智慧眼AIEye”
一.起因 最近毕业在家:),准备筹划社区运营和IoTBrowser升级的事务,遇到了一系列物业管理上的问题,本来出于好心提醒物业人员,结果反被误认为是打广告推销的,当时被激怒一下,后面一想也许这也是一 ...
- ScreenToGif:一款开源免费且好用的录屏转Gif软件
ScreenToGif介绍 GitHub上的介绍:此工具允许您记录屏幕的选定区域.来自网络摄像头的实时提要或来自草图板的实时绘图.之后,您可以编辑动画并将其保存为 gif.apng.视频.psd 或 ...
- 网易数帆内核团队:memory cgroup 泄漏问题的分析与解决
memory cgroup 泄露是 K8s(Kubernetes) 集群中普遍存在的问题,轻则导致节点内存资源紧张,重则导致节点无响应只能重启服务器恢复:大多数的开发人员会采用定期 drop cach ...
- C# DataGridView控件用法大全
动态添加新行 //方法一: int index = this.dataGridView1.Rows.Add(); this.dataGridView1.Rows[index].Cells[0].Val ...
- 整数-笔记C
实际情况也确实如此,C语言并没有严格规定 short.int.long 的长度,只做了宽泛的限制: short 至少占用 2 个字节. int 建议为一个机器字长.32 位环境下机器字长为 4 字节, ...
- JavaScript高级~数组方法reduce
reduce() 方法对数组中的每个元素执行一个由您提供的reducer函数(升序执行),将其结果汇总为单个返回值. 有点抽象,意思可以看做每个元素执行之后,都会有一个汇总结果,你可以通过这个汇总结果 ...
- Java编程指南:高级技巧解析 - Excel单元格样式的编程设置
最新技术资源(建议收藏) https://www.grapecity.com.cn/resources/ 前言 在Java开发中,处理Excel文件是一项常见的任务.在处理Excel文件时,经常需要对 ...