分享

Linux开机自动启动脚本方法

 忧郁_小刚 2011-05-01

Linux开机自动启动脚本方法

时间:2010-9-19来源:Online990 作者: Online990点击: 616次
-
大家在平时Linux维护中,经常会自己写一些脚本,用来启动、停用某些服务,或完成特定的一些维护工作。而这些工作经常需在Linux系统启动时自动运行。以下列出如何在Linux启动时自动运行脚本。
 
方法一
将自己写好的脚本或命令写入/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 stop 2>&/dev/null;
fi
将写好的脚本加入/etc/rc.local中
[web@info data]$ 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      0 :::80                       :::*                        LISTEN 
 
方法二
通过启动脚本来创建一个服务,使用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 determine the servers fully qualified domain name, using 127.0.0.1 for ServerName
                                               [  确定  ]    
[root@info ~]# ntsysv  
lqqqqqqqqqqqqu 服务 tqqqqqqqqqqqqk  
 x                                x  
 x 您想自动启动哪些服务?         x  
 x   [*] aparche                    x  
 x    x 确定 x       x 取消  x    x  
 
方法三
在 /home/用户/.bash_profile文件中 加入脚本或命令。在.bash_profile中加载的脚本或命令只能以单用户login时启动。并不是在Linux启动时启动。
[root@info ~]# cat /home/web/.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
       . ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin   
/usr/sbin/httpd  -k start 2>&/dev/null

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多