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. Linux 内核:设备驱动模型(2)driver-bus-device与probe

    Linux 内核:设备驱动模型(2)driver-bus-device与probe 系列:Linux 内核:设备驱动模型 学习总结 参考: https://blog.csdn.net/lizuobin ...

  2. STM32 CubeMX 学习:001-GPIO的使用

    背景 在上一讲STM32 CubeMX 学习:搭建开发环境中,我们已经利用CubeMx搭建了可编译的工程. 今天就开始来介绍STM32 GPIO的有关知识,以及如何驱动它. HOST-OS : Win ...

  3. js脚本化css

    脚本化CSS 我们刚讲过如何获取和设置行内样式的值,但是我们开发不会所有样式都写在行内,同时js没法获取内嵌样式表和外部样式表中的值. 事实上DOM提供了可靠的API,得到计算后的样式. 1. 获取计 ...

  4. Java助力加固Excel文件,保障数据安全

    前言 Excel文件保护是常用的一种功能,文件保护主要有三种: 添加密码,如果没有密码不允许打开文件. 添加密码,如果没有密码,不能修改文件,但可以打开,只读以及另存文件. 只读推荐,通常推荐打开Ex ...

  5. mapreduce压缩

    这是mr的一种优化策略,通过压缩编码对mapper或者reducer的输出进行压缩,以减少磁盘io,提高mr运行速度(但也相应增加了cpu运算负担) 特性: 1.mr支持将map输出的结果或者redu ...

  6. 【ClickHouse问题】更新表字段类型为Nullable(Int32)的列值,最终结果都是固定一个值:93147008???

    问题描述: clickhouse更新表数据.更新的列数据类型是Nullable(Int32),不管更新为什么数值,最后查询的结果都是一个固定值:93147008 问题复现: 1:建一张测试表 CREA ...

  7. 直接给一个数组项赋值,Vue 能检测到变化吗?

    由于 JavaScript 的限制,Vue 不能检测到以下数组的变动: 当你利用索引直接设置一个数组项时,例如: vm.items[indexOfItem] = newValue 当你修改数组的长度时 ...

  8. C++中的引用(Reference)

    1. 引用(Reference) 在 C++ 中,引用(Reference)是一个变量的别名. 它允许你通过不同的名字访问同一个变量. 与指针不同,引用在定义时必须被初始化,并且一旦绑定到某个变量,之 ...

  9. 使用GSAP制作动画视频

    GSAP 3Blue1Brown给我留下了深刻印象.利用动画制作视频,内容简洁,演示清晰.前两天刚好碰到一件事,我就顺便学习了一下怎么用代码做动画. 以javascrip为例,有两个动画引擎,GSAP ...

  10. 可视化—gojs 超多超实用经验分享(二)

    想了想序号还是接上一篇分享的的序号接着写,如果在本文中没有获取需要的答案,可以移步去看看上一篇的分享.gojs 超多超实用经验分享(一) 目录 22. 指定线段连接到节点的某一个特定的接口上 23. ...