@cyysu
        
        2017-09-26T07:46:58.000000Z
        字数 2851
        阅读 1133
    
- 时间: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: autostartPythonDIRECTORY=/opt/buildFILE=demo.py# Copy config file to the build directoryfindFile(){# add copy config file to build directorycd $DIRECTORYfor file in `ls`doif [ -d $file ];thencd $file && \cp ./*.json ../ && cd ..fidone}# Main functionmain(){python demo.py}# Start functiondo_start(){#Compile build directoryif [ -d $DIRECTORY ];thenecho "`date`: Enter this directory!!!"findFileelseecho -e "\033[41m`date`: ${DIRECTORY} may not exit!!! Ready to compile file to produce!!!\033[0m "cd /optpython setup.py PF600_ACQ && cd ${DIRECTORY}findFile#exitfi#make sure current directory is /opt/buildcd ${DIRECTORY}# Add start File to buildif [ -e $FILE ];thenecho "File exist success! Ready to run program!!!"mainelseecho -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#exitfi}case "$1" instart)do_start#cd /opt#python mqttTest.py;;stop);;status)echo $?;;*)echo "Usage autostart.sh [start|stop]" >&2exit 3esac
系统系统所需要的文件包括如下:
脚本具有自动执行编译过程,并生成build目录,同时会将配置文件拷贝到需要的文件夹目录下。并且如果脚本中不具有该目录会自动创建并进行编译操作
YUM=/var/run/yum.pid## install dependencedependence(){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 systemupdate(){echo -e "\033[41m `date`: Begin to update system!!!\033[0m"if [ $? -eq 0 ];thenyum -y updateif [ $? -eq 0 ];thenyum install -y python-pipfielseif [ -e ${YUM} ];thenrm -rf ${YUM}yum -y updateyum install -y python-pipfifi}## install python dependence softwarepythonSoftware(){echo -e "\033[41m `date`: Begin to install python dependence software!!!\033[0m"if [ $? -eq 0 ];thenpip install paho-mqttfiif [ $? -eq 0 ];thenpip install modbus-tkfi}# main functioncase "$1" instart)dependenceupdatepythonSoftware;;stop);;status)echo $?;;*)echo "Usage autostart.sh [start|stop]" >&2exit 3esac
第一步: 将文件夹所有内容放到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 #删除要慎重
第四步: 重启验证
rebootsystemctl status autostart.service # 如果看到active代表成功启动,直观上没有红颜色字体
1. 具有了ifconfig命令和netstat命令,方便我们查看服务是否启动和查看网络
2. 具有NFS功能,挂载只需要 mount -t nfs ip:/opt /你本地电脑的位置
3. 防火墙默认关闭,selinux开机会运行我们的脚本,会暂时关闭,不影响程序运行。
4. 系统目录中不可含有以build为名字的文件
重点强调 selinux不可以禁止,否则启动不了系统 
重点强调 selinux不可以禁止,否则启动不了系统 
重点强调 selinux不可以禁止,否则启动不了系统 
重点强调 selinux不可以禁止,否则启动不了系统 
重点强调 selinux不可以禁止,否则启动不了系统