1. 程式人生 > >shell腳本--制作自己的服務腳本

shell腳本--制作自己的服務腳本

sta 重啟 bash pos 制作 blog option status valid

首先看一個實例:假設有一個test的服務,可以通過命令對test進行啟動、關閉或者重啟,下面這個腳本就模擬這個功能:

#!/bin/bash
#test.sh

case $1 in
    start)
        echo "starting service......"
        sleep 1
        echo "started the service!!"
        ;;
    stop)
        echo "stopping service......"
        sleep 1
        echo "stopped the service!!"
        ;;
    restart)
        echo "restarting service......"
        sleep 1
        echo "restarted the service!!"
        ;;
    *)
        echo "warning: invalid option -> ${1}"
        ;;
esac

  運行這個腳本,結果如下

ubuntu@ubuntu:~$ ./test.sh start
starting service......
started the service!!
ubuntu@ubuntu:~$ ./test.sh stop
stopping service......
stopped the service!!
ubuntu@ubuntu:~$ ./test.sh restart
restarting service......
restarted the service!!
ubuntu@ubuntu:~$ ./test.sh status
warning: invalid option -> status
ubuntu@ubuntu:~$ 

  

shell腳本--制作自己的服務腳本