linux开机加载

来源:互联网 发布:网络专科可以自考本科 编辑:程序博客网 时间:2024/06/06 00:52
关于系统启动时自动启动某些自己的应用程序:  
  //Designed   by   ZhouLifa  
  //If   any   bug   found,   please   inform   me   as   soon   as   possible!  
  //Contact   info:  
  //post   addr:   广州市天河区华南理工大学计算机研究所   周立发   510640  
  //Computer   Application   Research   Institution,   South   China   University   of   Technology  
  //e-mail:   zhoulifa@yahoo.com  
  //Tel:   020-87113239-9322  
  //LastModifiedDate:2002.8.4  
  其实linux启动的主要工作在/etc/rc.d目录下的文件里.  
  1.首先是rc,它是一段shell,就是启动时做的事都在那里了,比如屏幕上出现Welcome   to   Red   Hat   Linux几个字也是它打印出来的,然后在set   clock   time,   set   hostname,   set   IP   address,监测硬盘,启动相应的服务等  
  2.如果你仔细看过就知道了它首先确定启动选项0-6,再进入/etc/rc.d/下相应的目录里,比如单用户模式为/etc/rc.d/rc1.d,我们平常用的Xwindow模式为/etc/rc.d/rc5.d  
  3.搜索相应目录(这里假设为/etc/rc.d/rc5.d)里的文件,首先找K开头的个文件(这是要被停止的程序),然后一个一个终止相应的进程.再找S开头的文件,这就是启动时要启动的各个程序.比如一般有:  
  K03rhnsd               K30sendmail       K74ypserv         S17keytable         S90cWnn  
  K05innd                 K34yppasswdd     K74ypxfrd         S20random             S90FreeWnn  
  K15postgresql     K35vncserver     K89bcm5820       S25netfs               S90xfs  
  K16rarpd               K45arpwatch       K91isdn             S26apmd                 S91smb  
  K20nfs                   K45named             K92ipchains     S28autofs             S95anacron  
  K20rstatd             K46radvd             K92iptables     S56rawdevices     S95atd  
  K20rusersd           K50snmpd             S05kudzu           S56xinetd             S99local  
  K20rwalld             K50tux                 S10network       S85gpm                   S99wine  
  K20rwhod               K60lpd                 S12syslog         S85httpd  
  K25squid               K65identd           S13portmap       S90canna  
  K25sshd                 K74ntpd               S14nfslock       S90crond                                                            
  你开机时就会看到:  
  Starting   syslog:           [   OK   ]  
  Starting   sendmail:       [   OK   ]  
  Starting   cWnn:               [   OK   ]  
  Starting   wine                 [   OK   ]  
  等等上面有的东西.  
  这些文件编号有个规则,就是都是S开头加两位数字,再加要启动的程序的名称.  
  数字越大就越在最后启动.即  
  Starting   wine                 [   OK   ]  
  这一行可定在  
  Starting   sendmail:       [   OK   ]  
  之后出现.  
  4.这里的文件都是符号链接,即用ln   -s命令建立的,用ls   -l   /etc/rc.d/rc5.d可以明确地看到他们的相应程序命令所在的位置.  
  5.如果你安装了某个软件,希望它在系统启动时自动启动,安装程序一般在这里加一个S文件.所以,如果你有一个程序希望它在系统启动是自动运行,你也可以加一个在这里.  
  比如我要启动的程序是/home/zhou/test/myapp  
  我在/etc/rc.d/rc5.d目录里用命令ln   -s   /home/zhou/test/myapp   S99myapp建立这个文件S99myapp  
  重新启动时就可以看到  
  Starting   myapp:  
  开机后ps   -A也可以看到我得程序在运行,当然这个程序是一直运行的,不是马上结束的那种  
  注意:关键是你要知道你要运行的程序的路径  
   
  6.另外,rc文件的最后指明在上述init程序启动完毕后要找/etc/rc.d/rc.local文件,会把rc.local文件里的程序全部运行,但rc.local是个shell文件.  
  你可以自己在里面加入  
  echo   "Hello,   welcome   to   my   home,   my   name   is   zhoulifa,   My   own   program   run   here"  
  sleep   5  
  系统启动时你可以看到这句话  
  对于上述我的程序你可以在rc.local文件里加入:  
  echo   "Starting   my   own   programe   myapp   now..."  
  /home/zhou/test/myapp  
  echo   "myapp   started   OK"  
  7.需要强调的一点是:如果你的程序本来不是用root身份运行的,那么在启动时可能就不能运行,因为你的环境变量不是符合的。  
  解决问题的办法就是:1、先在你的程序的开头就用setenv()函数来设定一些环境变量;2、在rc.local里用sudo   username   command去运行你这个程序。  
  ***********************  
  我刚做过试验,先写这样一个含有启动oracle所需的命令的文件OraScript:  
  [test@LinuxServer00   bin]$   cat   OraScript  
  connect   internal  
  startup  
  quit  
  再写这样一个运行上述命令的文件StartOracle,主要作用就是设置变量,最后一行启动svrmgrl  
  [test@LinuxServer00   bin]$   cat   StartOracle  
  export   USERNAME="oracle"  
  export   HOME=/home/oracle  
  export   PATH=$PATH:$HOME/bin  
  export   BASH_ENV=$HOME/.bashrc  
  export   ORACLE_BASE=/oracle  
  export   ORACLE_HOME=$ORACLE_BASE/product/8.1.7  
  export   ORACLE_SID=mydb  
  export   CLASSPATH=$ORACLE_HOME/jdbc/lib/classes111.zip  
  export   LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib  
  export   PATH=$PATH:$ORACLE_HOME:$ORACLE_HOME/bin:$ORACLE_HOME/dbs:./  
  export   USERNAME   BASH_ENV   PATH  
  /oracle/product/8.1.7/bin/svrmgrl   <   /oracle/product/8.1.7/bin/OraScript  
  当然你得到这个文件改为可执行chmod   u+x   StartOracle  
  最后在rc.local里加入:  
  [test@LinuxServer00   bin]$   cat   /etc/rc.d/rc.local  
  #!/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.  
   
  echo   "Starting   oracle   now...My   Script   run   here..."  
  su   oracle   "-c   /oracle/product/8.1.7/bin/StartOracle   >   /dev/null"  
  echo   "Oracle   started   OK!"  
  sleep   10  
  这样,开机时会看到提示"Starting   oracle   now...My   Script   run   here..."和"Oracle   started   OK!"。  
  如果你的程序里不含"   >   /dev/null"这个把输出定位到空设备的命令,你还可以看到启动oracle的详细情况。
原创粉丝点击