linux基础--开机自启动

来源:互联网 发布:剑灵人男捏脸数据图 编辑:程序博客网 时间:2024/05/19 17:59
方法一
将自己写好的脚本或命令写入/etc/rc.local文件中。系统会根据该文件来启动所指定的脚本或命令。
 
下面为httpd服务做了一个启动脚本。
功能:在Linux系统启动时检查httpd服务是否启动成功,如果服务已启动,将日志写入log。如果没有启动,则立即启动httpd服务,并将日志写入log。
[web@info data]$ vi/data/http.sh
#/bin/bash
log_file=/data/http_state.log
echo "">$log_file;
http_status=`netstat-nat|grep 80|awk {print $4}`
port=`echo${http_status:3:3}`;
#echo$port;
if [ "${port}" =="80" ]; then
             echo "http server already start!">>$log_file;
   elif [ "${port}" == "" ];then
             echo "http server stop!">>$log_file;
             /usr/sbin/httpd  -k stop2>&/dev/null;
fi
将写好的脚本加入/etc/rc.local中
[web@infodata]$ echo"/data/http.sh" >>/etc/rc.local;tail -1 /etc/rc.local
/data/http.sh
[web@info data]$ cat /data/http_status.log
http server stop!
[web@info data]$ netstat -nat|grep 80
tcp           0:::80                      :::*                       LISTEN 
 
方法二
#chkconfig:2345 80 05 --指定在哪几个级别执行,0一般指关机,6指的是重启,其他为正常启动。80为启动的优先级,
05为关闭的优先级别 

通过启动脚本来创建一个服务,使用chkconfig来指定启动服务的级别,并在ntsysv工具下加载让其自动运行。
 
[root@info ~]# cp /etc/rc.d/init.d/httpd/etc/init.d/aparche
[root@info ~]# chmod +x /etc/init.d/aparche
[root@info ~]# chkconfig --list aparche
aparche 服务支持chkconfig,但它在任何级别中都没有被引用(运行“chkconfig --add aparche”)
[root@info ~]# chkconfig --add aparche
[root@info ~]# chkconfig --list aparche
aparche         0:关闭    1:关闭    2:关闭    3:关闭    4:关闭    5:关闭    6:关闭
[root@info ~]# chkconfig --level 2345 aparche on
[root@info ~]# service aparche start
启动 httpd:httpd: Could not determinethe servers fully qualified domain name, using 127.0.0.1 forServerName
                                               确定 ]    
[root@info ~]# ntsysv  
lqqqqqqqqqqqqu 服务tqqqqqqqqqqqqk  
                                
 x您想自动启动哪些服务?         
  [*]aparche                    
   x 确定x      x 取消    
 
方法三
在/home/用户/.bash_profile文件中 加入脚本或命令。在.bash_profile中加载的脚本或命令只能以单用户login时启动。并不是在Linux启动时启动。
[root@info ~]# cat /home/web/.bash_profile
# .bash_profile
# Get the aliases andfunctions
if [ -f ~/.bashrc ];then
      . ~/.bashrc
fi
# User specific environment andstartup programs
PATH=$PATH:$HOME/bin   
/usr/sbin/httpd  -k start2>&/dev/null
0 0