ubuntu 15.10 设置shell脚本开机自动运行

我在ubuntu 15.10 系统上安装了 “Linux 802.11n CSI Tool” 工具,并且安装了 Intel 5300 NIC 无线网卡。而此后每次连接wifi都需要手动在命令行执行下列命令以加载网卡驱动:

sudo modprobe iwlwifi connector_log=0x1

为了方便自动开启无线连接功能,我在 /etc/rc.local 文件以及 /etc/init.d/rc.local 文件中加入下列语句:

echo password | sudo -S modprobe iwlwifi connector_log=0x1

其中 password 是我的用户密码(不需要用引号引起来)。
修改后的 /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.

echo ubuntu123456 | sudo -S modprobe iwlwifi connector_log=0x1
exit 0

修改后的 /etc/init.d/rc.local 文件如下:

#! /bin/sh
### BEGIN INIT INFO
# Provides:          rc.local
# Required-Start:    $all
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:
# Short-Description: Run /etc/rc.local if it exist
### END INIT INFO


PATH=/sbin:/usr/sbin:/bin:/usr/bin

. /lib/init/vars.sh
. /lib/lsb/init-functions

do_start() {
    if [ -x /etc/rc.local ]; then
            [ "$VERBOSE" != no ] && log_begin_msg "Running local boot scripts (/etc/rc.local)"
        /etc/rc.local
        ES=$?
        [ "$VERBOSE" != no ] && log_end_msg $ES
        return $ES
    fi
}

case "$1" in
    start)
    do_start
        ;;
    restart|reload|force-reload)
        echo "Error: argument '$1' not supported" >&2
        echo ubuntu123456 | sudo -S modprobe iwlwifi connector_log=0x1
        exit 3
        ;;
    stop|status)
        # No-op
        echo ubuntu123456 | sudo -S modprobe iwlwifi connector_log=0x1
        exit 0
        ;;
    *)
        echo "Usage: $0 start|stop" >&2
        echo ubuntu123456 | sudo -S modprobe iwlwifi connector_log=0x1
        exit 3
        ;;
esac

最后重启系统,成功!

注:
在Ubuntu中,对于需要使用sudo权限来执行的shell命令,都可以按照

echo password | sudo -S command

的格式来执行,从而不需要用户手动输入密码。其中 command 可以是需要执行的shell命令,也可以是其他的可执行文件。

猜你喜欢

转载自blog.csdn.net/yueyinlizun/article/details/79776558