zoukankan      html  css  js  c++  java
  • Linux开机自动启动脚本方法

    Linux开机自动启动脚本方法

     
    -
    大家在平时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
     
     
  • 相关阅读:
    java 基础中的字符串
    Java中的基本数据类型
    js 实现焦点图轮播效果和 jquery实现焦点图轮播效果
    Html中 value 和 name 属性的作用
    分别用js 和 html/css实现下拉菜单特效
    divide-conquer-combine(4.1 from the introduction to algorithm)
    1063. Set Similarity (25)
    1085. Perfect Sequence (25)
    1015. Reversible Primes (20)
    1057. Stack (30)
  • 原文地址:https://www.cnblogs.com/shuaixf/p/2696017.html
Copyright © 2011-2022 走看看