@cyysu
2017-10-17T08:26:27.000000Z
字数 1529
阅读 788
- 时间:2017年10月17日
- 作者:Kali
- 邮箱:cyysu.github.io@gmail.com/2869905223@qq.com/微信lwyx1413
- 版本:4.0
- 描述:centos下自动解决依赖脚本,和centos自启动脚本配套,用来启动Python程序
脚本编写
YUM=/var/run/yum.pid
#yum pid error
yumError(){
if [ -e ${YUM} ];then
rm -rf ${YUM}
fi
}
## install dependence
dependence(){
echo -e "\033[41m `date`: Begin to install software!!!\033[0m"
yumError
yum install -y vim epel-release net-tools traceroute python gcc python-devel Cython 2&>/dev/null
}
## update system
update(){
echo -e "\033[41m `date`: Begin to update system!!!\033[0m"
if [ $? -eq 0 ];then
yumError
yum -y update 2&>/dev/null
if [ $? -eq 0 ];then
yumError
yum install -y python-pip 2&>/dev/null
fi
else
yumError
yum -y update 2&>/dev/null
yum install -y python-pip 2&>/dev/null
fi
}
## install python dependence software
pythonSoftware(){
echo -e "\033[41m `date`: Begin to install python dependence software!!!\033[0m"
if [ $? -eq 0 ];then
pip install paho-mqtt 2&>/dev/null
fi
if [ $? -eq 0 ];then
pip install modbus-tk 2&>/dev/null
fi
}
# main function
case "$1" in
start)
dependence
update
pythonSoftware
;;
stop)
;;
status)
echo $?
;;
*)
echo "Usage autostart.sh [start|stop]" >&2
exit 3
esac
将下面的内容增加到/lib/systemd/system/中,然后就可以使用systemctl控制这个服务了
autostart.service
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
[Unit]
Description=autostart python service
After=network.target
[Service]
ExecStart=/opt/autostart.sh start
Type=forking
PIDFile=/opt/start.pid
PrivateTmp=true
[Install]
WantedBy=multi-user.target
支付宝 微信