bjghzly 发表于 2018-8-28 13:44:19

shell学习的点点滴滴—启动脚本

  
# cd/etc/rc.d/init.d/       //进入系统服务所在文件夹
  
# ls                   //查看当前系统服务
  
acpid               halt         multipathd       rpcidmapd
  
anacron             hidd         netconsole       rpcsvcgssd
  
apmd               hplip          netfs            saslauthd
  
atd               hsqldb         netplugd         sendmail
  
auditd            httpd          network          single
  
autofs            ibmasm         NetworkManager   smartd
  
avahi-daemon      ip6tables      nfs            sshd
  
avahi-dnsconfd      iptables       nfslock          sss
  
bluetooth         irda         nscd             syslog
  
capi                irqbalance   ntpd             tcsd
  
conman            isdn         oddjobd          vmware-tools
  
cpuspeed            jiaoben      pand             vmware-tools-thinprint
  
crond               killall      pcscd            vncserver
  
cups                krb524         portmap          vsftpd
  
cups-config-daemonkudzu         psacct         wdaemon
  
dnsmasq             lvm2-monitor   rawdevices       winbind
  
dund                mcstrans       rdisc            wpa_supplicant
  
firstboot         mdmonitor      readahead_earlyxfs
  
functions         mdmpd          readahead_laterypbind
  
gpm               messagebus   restorecond      yum-updatesd
  
haldaemon         microcode_ctlrpcgssd
  

  

  
先看一下服务的格式:
  
#!/bin/bash            //环境变量
  
#chkconfig:234 99 11   //表示在234启动模式中开机自动启动该服务,启动进程级S99,关闭进程级为K11
  
#description:auto_run       //如果chkconfig --add 时不报服务不支持,可以添加
  

  
case "$1" in
  
      start)
  
      ;;
  
      stop)
  
      ;;
  
      restart)
  
      ;;
  
      *)
  
esac
  

  

  
下面我们新建一个服务:
  
# vim fuwu
  
#!/bin/bash
  
#chkconfig:35 55 03
  
#description:auto_run
  

  
case "$1" in
  
         start)
  
          echo "开始的时间是$(date)">>/root/time.log
  
      ;;
  
         stop)
  
         echo "停止的时间是$(date)">>/root/time.log
  
      ;;
  
         restart)
  
         echo "重启的时间是$(date)">>/root/time.log
  
      ;;
  
      *)
  
          echo "Usage: $0{start|stop|restart}"
  
esac
  

  
然后保存退出
  

  
下面我们给fuwu这个文件赋予执行权限:
  
# chmod a+x fuwu
  

  
然后我们来启动该服务:
  
# service fuwustart      //start 其实就是我们格式中的start,这个可以改为其他的,但最好不要改,应遵循默认格式
  
# service fuwurestart      //restart就是我们格式中的restart
  
# service fuwusop         //当我们输错规定的格式,系统就会出现下面的提示,下面的提示其实就是我们在规范格式中的倒数第二行
  
Usage: /etc/init.d/fuwu{start|stop|restart}
  
#
  

  
下面我们看看/root/time.log中是否已经写入时间
  
# cat /root/time.log
  
开始的时间是2015年 09月 15日星期二 09:18:41 CST
  
重启的时间是2015年 09月 15日星期二 09:18:45 CST
  

  
添加fuwu到chkconfig中,并查看服务默认启动级别
  
# chkconfig --addfuwu
  
# chkconfig --listfuwu
  
fuwu             0:关闭    1:关闭    2:关闭    3:启用    4:关闭    5:启用    6:关闭
  
从上面可以看见,当系统在默认启动级别3,5时,开机会自动启动服务,会往/root/time.log中写入开始时间


页: [1]
查看完整版本: shell学习的点点滴滴—启动脚本