有时咱们须要将特定操做封装成服务,经过服务启动中止,例如nginx的启动中止,service nginx start 或者service nginx stopnginx
下面咱们将编写一个demoshell
cd /etc/init.d/
sudo vi test,创建一个service名称为test的服务bash
加入下面模版代码ide
#! /bin/sh ### BEGIN INIT INFO # Provides: reboot # Required-Start: # Required-Stop: # Default-Start: # Default-Stop: 6 # Short-Description: Execute the reboot command. # Description: ### END INIT INFO PATH=/sbin:/usr/sbin:/bin:/usr/bin . /lib/lsb/init-functions do_stop () { # Message should end with a newline since kFreeBSD may # print more stuff (see #323749) log_action_msg "Will now restart" reboot -d -f -i } case "$1" in start) nohup /etc/init.d/test.sh >> b.log 2>&1 & ;; stop) do_stop ;; *) echo "Usage: $0 start|stop" >&2 exit 3 ;; esac
能够根据须要编写start方法以及stop方法测试
赋予执行权限ui
sudo chmod +x /etc/init.d/test
而后咱们再写一个shell测试脚本spa
sudo vi test.shrest
#!/bin/bash int=1 while(( $int<=5 )) do date >> ~/a.log sleep 1 # let "int++" done
赋予执行权限code
sudo chmod +x /etc/init.d/test.sh
接下来,咱们启动服务blog
service test start
查看服务是否已经启动
tail -f ~/a.log
会看到不断的打印时间
这说明咱们的脚本已经以服务的形式启动起来了。