1.准备好你的可执行文件
这里以 hello 为例
执行./hello
输出 hello
2.在/etc/init.d/目录下生成hello.sh脚本
hello.sh:
#!/bin/bash
SERVERNAME="hello"
start()
{
echo "start $SERVERNAME"
/home/yao/projects/$SERVERNAME
echo "start $SERVERNAME ok!"
exit 0;
}
stop()
{
echo "stop $SERVERNAME"
killall $SERVERNAME
echo "stop $SERVERNAME ok!"
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo "usage: $0 start|stop|restart"
exit 0;
esac
exit
3.更改脚本文件属性
chmod +x hello.sh
4.运行
(1)启动:
root@localhost:/home/yao/projects# service hello.sh start
start hello
hello world
(2)停止:
root@localhost:/home/yao/projects# service hello.sh stop
stop hello
stop hello ok!
(3)重启:
root@localhost:/etc/init.d# service hello.sh restart
stop hello
stop hello ok!
start hello
hello world
hello world
5.完事了!
网友评论