linux将shell脚本设置为开机启动

时间:2022-11-07 20:00:21

以bluebridge.tar.gz包为例子进行相关介绍

1.解压包

tar zxvf bluebridge.tar.gz

2.将解压包放在/usr/local/目录下

mv out /usr/local/bbserver

3.修改相应的配置文件

cd /usr/local/bbserver

修改run_bbserver.sh文件 

linux将shell脚本设置为开机启动

4.将其设置为开机启动服务

cd /etc/init.d

vim bbserver

#!/bin/bash 

#description: bbserver

#chkconfig: 2345 20 81

EXEC_PATH=/usr/local/bbserver/
EXEC=run_bbserver.sh
DAEMON=/usr/local/bbserver/run_bbserver.sh
PID_FILE=/var/run/run_bbserver.sh.pid

. /etc/rc.d/init.d/functions

if [ ! -x $EXEC_PATH/$EXEC ] ; then
echo "ERROR: $DAEMON not found"
exit 1
fi

stop()
{
echo "Stoping $EXEC ..."
ps aux | grep "$DAEMON" | kill -9 `awk '{print $2}'` >/dev/null 2>&1
rm -f $PID_FILE
usleep 100
echo "Shutting down $EXEC: [ OK ]"
}

start()
{
echo "Starting $EXEC ..."
$DAEMON > /dev/null &
pidof $EXEC > $PID_FILE
usleep 100
echo "Starting $EXEC: [ OK ]"
}

restart()
{
stop
start
}

case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
status -p $PID_FILE $DAEMON
;;
*)
echo "Usage: service $EXEC {start|stop|restart|status}"
exit 1
esac

exit $?

5.添加到开机服务中去

cd /etc/init.d
chmod 777 bbserver

linux将shell脚本设置为开机启动

service bbserver start

linux将shell脚本设置为开机启动

6.授权

cd /usr/local/ 
chown -R myoldpc5.myoldpc5 bbserver

7.设置开机启动

vim /etc/rc.d/rc.local

linux将shell脚本设置为开机启动

8.重启机器

reboot