1.关机时执行某个脚本的具体思路
(1)在文件夹/etc/init.d/下创建关机时需要执行的脚本file_name;
(2)分别在文件夹/etc/rc0.d/和/etc/rc6.d/下创建该该脚本文件的链接文件K07file_name:
1
2
|
sudo ln -s /etc/init.d/file_name /etc/rc0.d/K07file_name sudo ln -s /etc/init.d/file_name /etc/rc6.d/K07file_name |
(3)在文件夹/var/lock/subsys/下生成与file_name同名的文件
1
2
|
sudo mkdir -p /var/lock/subsys/ sudo touch /var/lock/subsys/file_name |
说明:关键字K07仅需要在文件夹/etc/rc0.d/和/etc/rc6.d/内添加,在/etc/init.d/和
/var/lock/subsys/里面不需要添加。
2.举例—在Linux关机时执行脚本snaking616
脚本snaking616内容如下:
1
2
3
4
5
6
|
#!/bin/bash cd /etc/init.d echo "admin" |sudo -S touch eth_set_100 echo "admin" |sudo -S chmod 777 eth_set_100 echo "#!/bin/bash echo "admin" |sudo -S ethtool -s eth1 autoneg off speed 100 duplex full" > eth_set_100 |
脚本在系统关机时会在文件夹/etc/init.d下自动生成脚本文件eth_set_100,并将以下内容写入该文件内:
1
2
|
#!/bin/bash echo "admin" |sudo -S ethtool -s eth1 autoneg off speed 100 duplex full |
具体实现方法如下:
(1)打开终端创建脚本文件snaking616
1
2
3
|
sudo touch /etc/init.d/snaking616 sudo chmod 777 /etc/init.d/snaking616 sudo gedit /etc/init.d/snaking616 |
填写以下代码:
1
2
3
4
5
6
|
#!/bin/bash cd /etc/init.d echo "admin" |sudo -S touch eth_set_100 echo "admin" |sudo -S chmod 777 eth_set_100 echo "#!/bin/bash echo "admin" |sudo -S ethtool -s eth1 autoneg off speed 100 duplex full" > eth_set_100 |
(2)创建连接文件
1
2
|
sudo ln -s /etc/init.d/snaking616 /etc/rc0.d/K07snaking616 sudo ln -s /etc/init.d/snaking616 /etc/rc6.d/K07snaking616 |
(3)在文件夹/var/lock/subsys/下生成与snaking616同名的文件
1
2
|
sudo mkdir -p /var/lock/subsys/ sudo touch /var/lock/subsys/snaking616 |
当系统关机时间,在文件夹/etc/init.d下,会自动生成脚本文件eth_set_100。
原文链接:http://blog.csdn.net/snaking616/article/details/78680021