@cyysu
2017-09-26T07:46:58.000000Z
字数 2851
阅读 1027
- 时间:2017年8月25日
- 作者:chen yuan
- 描述:简介脚本使用说明以及系统启动所需文件
- 版本:V2.0
脚本编写
默认的用户为root 登录密码为mj1234,后续如果更改密码可以采用如下方式
passwd root #使用root执行该命令
脚本按照service方式进行编辑,在启动时需要加入启动参数。如:
/bin/bash autostart.sh start
脚本采用低耦合方式进行编写,将统一功能的代码进行封装。为了确保运行在当前目录,可以在脚本中看到cd ${DIRECTORY},在后续脚本的升级中,将需要运行的文件封装成变量。
#! /bin/bash
#chkconfig: 2345 80 90
#description: autostart centos scripts
#processname: autostartPython
DIRECTORY=/opt/build
FILE=demo.py
# Copy config file to the build directory
findFile(){
# add copy config file to build directory
cd $DIRECTORY
for file in `ls`
do
if [ -d $file ];then
cd $file && \cp ./*.json ../ && cd ..
fi
done
}
# Main function
main(){
python demo.py
}
# Start function
do_start(){
#Compile build directory
if [ -d $DIRECTORY ];then
echo "`date`: Enter this directory!!!"
findFile
else
echo -e "\033[41m`date`: ${DIRECTORY} may not exit!!! Ready to compile file to produce!!!\033[0m "
cd /opt
python setup.py PF600_ACQ && cd ${DIRECTORY}
findFile
#exit
fi
#make sure current directory is /opt/build
cd ${DIRECTORY}
# Add start File to build
if [ -e $FILE ];then
echo "File exist success! Ready to run program!!!"
main
else
echo -e "\033[41m`date`: Scripts start failed!!! ${FILE} may not exit!!! Ready to produce bootfile!!!\033[0m "
cd /opt && cp demo.py ${DIRECTORY} && cd ${DIRECTORY}
main
#exit
fi
}
case "$1" in
start)
do_start
#cd /opt
#python mqttTest.py
;;
stop)
;;
status)
echo $?
;;
*)
echo "Usage autostart.sh [start|stop]" >&2
exit 3
esac
系统系统所需要的文件包括如下:
脚本具有自动执行编译过程,并生成build目录,同时会将配置文件拷贝到需要的文件夹目录下。并且如果脚本中不具有该目录会自动创建并进行编译操作
YUM=/var/run/yum.pid
## install dependence
dependence(){
echo -e "\033[41m `date`: Begin to install software!!!\033[0m"
yum install -y vim epel-release 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
yum -y update
if [ $? -eq 0 ];then
yum install -y python-pip
fi
else
if [ -e ${YUM} ];then
rm -rf ${YUM}
yum -y update
yum install -y python-pip
fi
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
fi
if [ $? -eq 0 ];then
pip install modbus-tk
fi
}
# main function
case "$1" in
start)
dependence
update
pythonSoftware
;;
stop)
;;
status)
echo $?
;;
*)
echo "Usage autostart.sh [start|stop]" >&2
exit 3
esac
第一步: 将文件夹所有内容放到opt目录下
cd 文件路径
\cp -rf ./* /opt
第二步: 执行脚本,添加系统系统服务
./autostart.sh #生成build目录
cp ./autostart.service /lib/systemd/system/
systemctl enable autostart.service
然后编辑文件/etc/rc.local
在文件中填入 /bin/bash /opt/autostart.sh start
第三步: 删除源码
rm -rf ./PF600_ACQ #删除要慎重
第四步: 重启验证
reboot
systemctl status autostart.service # 如果看到active代表成功启动,直观上没有红颜色字体
1. 具有了ifconfig命令和netstat命令,方便我们查看服务是否启动和查看网络
2. 具有NFS功能,挂载只需要 mount -t nfs ip:/opt /你本地电脑的位置
3. 防火墙默认关闭,selinux开机会运行我们的脚本,会暂时关闭,不影响程序运行。
4. 系统目录中不可含有以build为名字的文件
重点强调 selinux不可以禁止,否则启动不了系统
重点强调 selinux不可以禁止,否则启动不了系统
重点强调 selinux不可以禁止,否则启动不了系统
重点强调 selinux不可以禁止,否则启动不了系统
重点强调 selinux不可以禁止,否则启动不了系统