HI END


一种永不妥协,追求极致与完美的精神与态度。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Linux如何开机自动运行自己的脚本(转)

Posted on 2012-07-12 15:59  HI END  阅读(521)  评论(0编辑  收藏  举报

来源:http://jiajun.iteye.com/blog/387265

一、root权限编辑/etc/rc.d/rc.local

 

Shell代码 复制代码 收藏代码
  1. su   
  2. cd /etc/rc.d/   
  3. vi rc.local  
su
cd /etc/rc.d/
vi rc.local

 

二、在这个文件加上你要执行的脚本,全部内容如下:

 

Shell代码 复制代码 收藏代码
  1. #!/bin/sh   
  2. #   
  3. # This script will be executed *after* all the other init scripts.   
  4. # You can put your own initialization stuff in here if you don't   
  5. # want to do the full Sys V style init stuff.   
  6.     
  7. touch /var/lock/subsys/local   
  8. mount //192.168.0.3/data2-1 /mnt/data2-1 -o username=un,password=123  
  9. mount //192.168.0.3/data2-2 /mnt/data2-2 -o username=un,password=123  
  10. mount //192.168.0.3/data2-3 /mnt/data2-3 -o username=un,password=123  
  11. mount //192.168.0.3/data2-4 /mnt/data2-4 -o username=un,password=123  
  12. mount //192.168.0.3/data2-5 /mnt/data2-4 -o username=un,password=123  
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
 
touch /var/lock/subsys/local
mount //192.168.0.3/data2-1 /mnt/data2-1 -o username=un,password=123
mount //192.168.0.3/data2-2 /mnt/data2-2 -o username=un,password=123
mount //192.168.0.3/data2-3 /mnt/data2-3 -o username=un,password=123
mount //192.168.0.3/data2-4 /mnt/data2-4 -o username=un,password=123
mount //192.168.0.3/data2-5 /mnt/data2-4 -o username=un,password=123

 提示:这里的做法很不成熟,希望不要这样,最好自己写个脚本文件在这里来调用,结构更清晰,但是要注意到是把要执行的命令作为一个参数传递给su。

 
另外复习一个VI编辑命令-拷贝
yy
p

 

三、虽然搞定,还是补充一下

 

    Linux在启动时,会自动执行/etc/rc.d目录下的初始化程序,因此我们可以把启动任务放到该目录下,有下列办法:

 

    方案一:

    比较简单,就是上面的做法,/etc/rc.d/目录下的初始化程序很多,rc.local是在完成所有初始化之后执行的,所以在这里做手脚很合适。

 

    方案二:

    init.d目录下都为可执行程序,他们其实是服务脚本,按照一定格式编写,Linux 在启动时会自动执行,类似Windows下的服务。

  1、编写如下面的脚本simpleTest:

Shell代码 复制代码 收藏代码
  1. #!/bin/bash   
  2. #chkconfig:2345 80 05 --指定在哪几个级别执行,0一般指关机,6指的是重启,其他为正常启动。80为启动的优先级,05为关闭的优先级别   
  3. #description:simple example service   
  4. RETVAL=0  
  5. start(){ #启动服务的入口函数   
  6. echo  "simple example service is started..."  
  7. }   
  8.   
  9. stop(){ #关闭服务的入口函数   
  10. echo  "simple example service is stoped..."  
  11. }   
  12.   
  13. #使用case选择   
  14. case $1 in   
  15. start)   
  16. start   
  17. ;;   
  18. stop)   
  19. stop   
  20. ;;   
  21. *)   
  22. echo "error choice ! please input start or stop";;   
  23. esac   
  24. exit $RETVA  
#!/bin/bash
#chkconfig:2345 80 05 --指定在哪几个级别执行,0一般指关机,6指的是重启,其他为正常启动。80为启动的优先级,05为关闭的优先级别
#description:simple example service
RETVAL=0
start(){ #启动服务的入口函数
echo  "simple example service is started..."
}

stop(){ #关闭服务的入口函数
echo  "simple example service is stoped..."
}

#使用case选择
case $1 in
start)
start
;;
stop)
stop
;;
*)
echo "error choice ! please input start or stop";;
esac
exit $RETVA

3、运行chmod +x /etc/rc.d/init.d/simpleTest,使之可直接执行

4、运行chkconfig --add simpleTest,把该服务添加到配置当中

5、运行chkconfig --list simpleTest,可以查看该服务进程的状态

 

 

方案三、目的是挂载共享目录,所以可以在/etc/fstab中加上

//192.168.0.3/data2-1   /mnt/data2-1            nfs     username=un,password=123     0 0

 

 

 

非常不好意思,方案二中虽然脚本可以运行

# ./simpleTest start
simple example service is started...

但是我在第4步出现

# chkconfig --add simpleTest
在 simpleTest 服务中读取信息时出错:没有那个文件或目录
没有进一步解决抱歉。