【Linux 运维系列】Ubuntu 开机启动自定义脚本

【0】自定义的脚本

以 SRS 启动为参考

#!/bin/bash
BIN_PATH="~/StreamServer/srs-4.0.107/trunk"
PROG_ARGS="> terminal.log"
SUDO_PWD="md"
APP_NAME="objs/srs"
cd $BIN_PATH
# kill exist process
PID=$(ps -ef|grep objs/srs|grep -v grep|awk '{print $2}')
if [ -z "$PID" ];then
echo "${APP_NAME} not exist"
else
echo "${APP_NAME} id: $PID"
echo "${SUDO_PWD}" | sudo -S kill -9 ${PID}
wait
echo "${APP_NAME} killed"
fi
# start a new process
echo "${APP_NAME} starting"
echo "${SUDO_PWD}" | sudo -S ./objs/srs -c ./conf/my_realtime.conf>/dev/null &
sleep 1
echo "${APP_NAME} started"

【1】开机启动自定义脚本 -- Ubuntu 16 版本

【1.1】/etc/rc.local

#!/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.
#
# 新增部分
# 需要开机启动的自定义脚本
~/StreamServer/srs_run.sh
exit 0

【2】开机启动自定义脚本 -- Ubuntu 18 版本

【2.1】/etc/systemd/system/rc-local.service

[Unit]
Description=/etc/rc.local Compatibility
ConditionPathExists=/etc/rc.local

[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99

[Install]
WantedBy=multi-user.target

【2.2】/etc/rc.local

#!/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.
# 新增部分
~/StreamServer/srs_run.sh
exit 0
添加可执行权限
sudo chmod +x /etc/rc.local

使能服务
sudo systemctl enable rc-local

启动服务
sudo systemctl start rc-local.service
sudo systemctl status rc-local.service

参考与致谢

本博客为博主学习笔记,同时参考了网上众博主的博文以及相关专业书籍,在此表示感谢,本文若存在不足之处,请批评指正。

感谢公司钱志明总工程师的辛苦总结。

Logo

一站式 AI 云服务平台

更多推荐