天天看点

Linux 开机执行脚本方法

1. 方式一修改/etc/rc.local

tail -2 /etc/rc.local

touch /root/aa.txt

/bin/bash /root/shell/redis-start.sh >/dev/null 2>/dev/null

2. 方式二chkconfig管理

  1> 第一种脚本

#!/bin/sh
#Configurations injected by install_server below....

EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli
PIDFILE=/var/run/redis_6379.pid
CONF="/etc/redis/6379.conf"
REDISPORT="6379"
###############
# SysV Init Information
# chkconfig: - 58 74
# description: redis_6379 is the redis daemon.
### BEGIN INIT INFO
# Provides: redis_6379
# Required-Start: $network $local_fs $remote_fs
# Required-Stop: $network $local_fs $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Should-Start: $syslog $named
# Should-Stop: $syslog $named
# Short-Description: start and stop redis_6379
# Description: Redis daemon
### END INIT INFO


case "$1" in
    start)
        if [ -f $PIDFILE ]
        then
            echo "$PIDFILE exists, process is already running or crashed"
        else
            echo "Starting Redis server..."
            $EXEC $CONF
        fi
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
            echo "$PIDFILE does not exist, process is not running"
        else
            PID=$(cat $PIDFILE)
            echo "Stopping ..."
            $CLIEXEC -p $REDISPORT shutdown
            while [ -x /proc/${PID} ]
            do
                echo "Waiting for Redis to shutdown ..."
                sleep 1
            done
            echo "Redis stopped"
        fi
        ;;
    status)
        PID=$(cat $PIDFILE)
        if [ ! -x /proc/${PID} ]
        then
            echo 'Redis is not running'
        else
            echo "Redis is running ($PID)"
        fi
        ;;
    restart)
        $0 stop
        $0 start
        ;;
    *)
        echo "Please use start, stop, restart or status as first argument"
        ;;
esac      

  2 > 第二种方式

[root​​@redis​​ ~]# cat /etc/init.d/redis-6379

#!/bin/bash

# chkconfig: 3 88 88

/bin/bash /server/scripts/redis-6379-start.sh >/dev/null 2>/dev/null

[root​​@redis​​ ~]# chmod +x /etc/init.d/redis-6379

#添加到chkconfig,开机自启动

[root​​@redis​​ ~]# chkconfig --add redis-6379

[root​​@redis​​ ~]# chkconfig --list redis-6379

redis-6379               0:off    1:off    2:off    3:on    4:off    5:off    6:off

重启系统,查看结果

[root​​@redis​​ ~]# cat /tmp/redis-6379-start.log

2020-12-19_09:59:10

操作成功

关闭开机启动

[root@redis ~]# chkconfig redis-6379 off

[root@redis ~]# chkconfig --list redis-6379

redis-6379               0:off    1:off    2:off    3:off    4:off    5:off    6:off

从chkconfig管理中删除redis-6379

[root@redis ~]# chkconfig --list redis-6379

redis_6379         0:关    1:关    2:开    3:开    4:开    5:开    6:关

[root@redis ~]# chkconfig --del redis-6379

继续阅读