Shell scripting system service script

[root@localhost ~]# vim myprog
#!/bin/bash
case  "$1" in
start)
echo -n "正在启动sleep服务……"   
if sleep 120
then
echo "ok"
fi
;;
stop)
echo -n "正在停止sleep服务……"
pkill "sleep" &> /dev/null
echo "ok"
;;
status)
if 
pgrep "sleep" &>/dev/null
then
echo "sleep服务已经启动"
else
echo "sleep服务已经停止"
fi
;;
restart)
$0 stop
$0 start
;;
*)
echo "用法:$0 {start|stop|status|restart}"
esac
[root@localhost ~]# sh myprog                 //未提供任何参数,则按默认处理
用法:myprog {start|stop|status|restart}
[root@localhost ~]# sh myprog start
正在启动sleep服务……ok
[root@localhost ~]# sh myprog stop
正在停止sleep服务……ok

$ 1 specified start, stop, restart, status control parameters script by position variables are used to start, stop, restart, view the process of filling sleep, sleep command to pause the specified number of seconds of time.

In the Linux system, the source package control script after compile and install the service provided by the use of a case branching statements, there are some source package does not provide a service control script, compiled installation can refer to the above script writing service control script yourself.

Guess you like

Origin blog.51cto.com/14157628/2425575