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服务和脚本做成开机自启的更多相关文章

  1. centos6服务启动脚本及开机启动过程

    centos6服务启动脚本 centos6的服务启动脚本都放在/etc/rc.d/init.d/下,/etc/init.d/是/etc/rc.d/init.d/的软链接: centos6的服务启动脚本 ...

  2. 开发nginx启动脚本及开机自启管理(case)

    往往我们在工作中需要自行写一些脚本来管理服务,一旦服务异常或宕机等问题,脚本无法自行管理,当然我们可以写定时任务或将需要管理的脚本加入自启等方法来避免这种尴尬的事情,case适用与写启动脚本,下面给大 ...

  3. MongoDB安装并设置为windows服务以使其开机自启

    在MongoDB的官方下载windows平台的压缩zip文件,地址:https://www.mongodb.org/dr/fastdl.mongodb.org/win32/mongodb-win32- ...

  4. Centos 下添加开机自启动服务和脚本

    最近刚玩Centos7的系统,跟Centos6还是很多方面有改变的,这里记录一下怎么在Centos7下添加开机自启动脚本和服务的方法. 1.添加开机自启服务 我这里以docker 服务为例,设置如下两 ...

  5. Centos 下添加开机自启动服务和脚本【转】

    最近刚玩Centos7的系统,跟Centos6还是很多方面有改变的,这里记录一下怎么在Centos7下添加开机自启动脚本和服务的方法. 1.添加开机自启服务 我这里以docker 服务为例,设置如下两 ...

  6. Linux开机自启应用&开机执行脚本&监听端口应用挂掉了执行启动脚本

    linux开机自启 背景 目前要部署一个spring boot框架的jar包,实现开机启动项目或者应用挂掉了 执行启动脚本 在root目录下有一个启动项目的脚本: app_start.sh app_s ...

  7. 树莓派 ubuntu16.04 安装SSH 配置SSH 开机自启SSH

    入手个树莓派3B 装了 ubuntu 16.04 需要用到SSH 记录下 0.先获得树莓派IP 树莓派 使用网线连接路由器和树莓派 在路由器设置页面(一般是192.168.1.1具体看路由器的型号和设 ...

  8. svn + nginx unit + python3自动化发布web服务方法

    本周将python web服务管理更换成nginx unit以后发现接口性能有了明显的提升,访问速度快了不少.不过有个很大的问题就是使用svn自动化发布以后,服务并没有刷新使用新的代码运行,而又不懂得 ...

  9. 开机自动启动WEB服务,共享目录。

    最近工作中,需要共享一个目录,每次重启总要手动执行一下  nohup python -m SimpleHTTPServer 8000这个命令,想着实现让它开机自动启动,就一劳永逸了. 手动步骤如下: ...

  10. Linux Oracle服务启动&停止脚本与开机自启动

    在CentOS 6.3下安装完Oracle 10g R2,重开机之后,你会发现Oracle没有自行启动,这是正常的,因为在Linux下安装Oracle的确不会自行启动,必须要自行设定相关参数,首先先介 ...

随机推荐

  1. 嵌入式编程中使用qemu能够做什么?

    嵌入式编程中使用qemu能够做什么? 背景 学习QEMU以后,为了拓展视野,才发现QEMU可以做这么多的事情. 原文链接(有删改):https://www.eet-china.com/mp/a5634 ...

  2. 三层交换机vlan间路由

    sw1: [Huawei]vlan batch 10 20 [Huawei]int e0/0/1 [Huawei-Ethernet0/0/1]port link-type access [Huawei ...

  3. MFC基于对画框工程笔记->更改窗口图标以及生成的.exe图标

    一.前言 继前一篇生成MFC基于对话框工程->新建MFC对话框后,开始改动对话框图标以及生成的.exe图标. 原对话框图标以及.exe图标: 在菜单栏中选择生成目录为Release 打开Rele ...

  4. CPU的实模式

    实模式是指CPU的寻址方式,寄存器大小,指令用法等,是用来反应CPU在该环境下如何工作的概念. CPU的工作原理:CPU大体分为三个部分,控制.运算.存储单元. 控制单元是CPU的控制中心,大致由指令 ...

  5. CCF 无线网络

    题目原文 问题描述(题目链接登陆账号有问题,要从这个链接登陆,然后点击"模拟考试",进去找本题目) 试题编号: 201403-4 试题名称: 无线网络 时间限制: 1.0s 内存限 ...

  6. 解决Prism中对话框服务中对话框开启时回调函数不会触发的问题

    解决办法 新建一个类DialogServiceExtend,然后在再注册 public class DialogServiceExtend : DialogService { public Dialo ...

  7. 假期小结4hive学习beeline

    学习Beeline是连接和管理Hive的一种有效方式.Beeline是一个用于与Hive交互的命令行工具,它支持通过JDBC连接到Hive服务器,并执行查询和管理Hive表等操作. 以下是一些关于Be ...

  8. docker 容器挂载技术

    创建和使用桥接网络 docker network create --driver bridge --subnet 192.168.13.0/24 --gateway 192.168.13.1 mqyn ...

  9. 关于spring boot中mapper注入到service时IDEA报错的解决办法

    虽然这个错误不影响正常运行但是作为强迫症患者看着实属难受,经过在论坛查看资料学习到以下两种解决方法,可以供大家参考以下,如有什么错误的地方还希望各位大佬指定一下. 1.在注解@Autowired后增加 ...

  10. 【Dos-BatchPrograming】01

    --0. 1.文件后缀的延申 官方教程更推荐使用.cmd作为后缀 .cmd和.bat的区别: http://www.360doc.com/content/12/0810/09/3688062_2293 ...