【基于Python+Flask项目部署系列--07】Ubuntu增加开机自动运行脚本

由于生产环境可能出现重启的情况,因此每次启动机器的时候执行一些项目代码开启的命令,方法如下

一、 update-rc.d增加开机启动服务


给Ubuntu添加一个开机启动脚本,操作如下:
1、新建个脚本文件fortisiem.sh
root@fortisiemweb:/etc/init.d# cat fortisiem.sh
#!/bin/bash
### BEGIN INIT INFO
#
# Provides: location_server
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: ss-server initscript
# Description: This file should be used to construct scripts to be placed in /etc/init.d.
#
### END INIT INFO

/etc/init.d ssh start
service nginx start
supervisord -c /opt/fortisiemweb/supervisord.conf
python /opt/fortisiemweb/manager.py start &
service mongod start
exit 0

注意:蓝色实体部分是必填的,红色字体部分是实际的命令

2、设置权限并开机启动
root@fortisiemweb:~# vi fortisiem.sh
root@fortisiemweb:~# chmod 755 fortisiem.sh
root@fortisiemweb:~# cp fortisiem.sh /etc/init.d/
root@fortisiemweb:~# cd /etc/init.d/
root@fortisiemweb:/etc/init.d# update-rc.d fortisiem.sh defaults 90

############################################################# 以下为拓展部分,实际未用#########################################
5、移除Ubuntu开机脚本
sudo update-rc.d -f new_service.sh remove
6、通过sysv-rc-conf来管理上面启动服务的启动级别等,还是开机不启动
sudo sysv-rc-conf 
7、update-rc.d的详细参数
使用update-rc.d命令需要指定脚本名称和一些参数,它的格式看起来是这样的(需要在 root 权限下):
update-rc.d [-n] [-f] <basename> remove
update-rc.d [-n] <basename> defaults
update-rc.d [-n] <basename> disable|enable [S|2|3|4|5]
update-rc.d <basename> start|stop <NN> <runlevels>
-n: not really
-f: force
其中:
• disable|enable:代表脚本还在/etc/init.d中,并设置当前状态是手动启动还是自动启动。
• start|stop:代表脚本还在/etc/init.d中,开机,并设置当前状态是开始运行还是停止运行。(启用后可配置开始运行与否)
• NN:是一个决定启动顺序的两位数字值。(例如90大于80,因此80对应的脚本先启动或先停止)
• runlevels:则指定了运行级别。
实例:
(1)、添加一个新的启动脚本sample_init_script,并且指定为默认启动顺序、默认运行级别(还记得前面说的吗,首先要有实际的文件存在于/etc/init.d,即若文件/etc/init.d/sample_init_script不存在,则该命令不会执行):
update-rc.d sample_init_script defaults
上一条命令等效于(中间是一个英文句点符号):
update-rc.d sample_init_script start 20 2 3 4 5 . stop 20 0 1 6
(2)、安装一个启动脚本sample_init_script,指定默认运行级别,但启动顺序为50:
update-rc.d sample_init_script defaults 50
(3)、安装两个启动脚本A、B,让A先于B启动,后于B停止:
update-rc.d A 10 40
update-rc.d B 20 30
(4)、删除一个启动脚本sample_init_script,如果脚本不存在则直接跳过:
update-rc.d -f sample_init_script remove
这一条命令实际上做的就是一一删除所有位于/etc/rcX.d目录下指向/etc/init.d中sample_init_script的链接(可能存在多个链接文件),update-rc.d只不过简化了这一步骤。
(5)禁止Apache/MySQL相关组件开机自启:
update-rc.d -f apache2 remove
update-rc.d -f mysql remove
8、服务的启动停止状态
#通过service,比如
sudo service xxx status
sudo service xxx start
sudo service xxx stop
sudo service xxx restart
9、查看全部服务列表
sudo service --status-all

来自 <https://www.linuxidc.com/Linux/2017-09/147166.htm>

Ubuntu 16.04设置rc.local开机启动命令/脚本的方法(通过update-rc.d管理Ubuntu开机启动程序/服务)
注意:rc.local脚本里面启动的用户默认为root权限。
一、rc.local脚本
rc.local脚本是一个Ubuntu开机后会自动执行的脚本,我们可以在该脚本内添加命令行指令。该脚本位于/etc/路径下,需要root权限才能修改。
该脚本具体格式如下:
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

exit 0
注意: 一定要将命令添加在exit 0之前。里面可以直接写命令或者执行Shell脚本文件sh。

来自 <https://www.linuxidc.com/Linux/2017-09/147166.htm>

 

转载于:https://www.cnblogs.com/forfreewill/articles/10223748.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值