欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

linux /etc/init.d/ 服务脚本示例

程序员文章站 2022-06-24 20:24:09
1. 脚本写好后放入到/etc/init.d/目录下,并设置可执行权限chmod +x scriptnamed 2. 脚本中包含start,stop,restart,status几种方法,...

1. 脚本写好后放入到/etc/init.d/目录下,并设置可执行权限chmod +x scriptnamed

2. 脚本中包含start,stop,restart,status几种方法,

/opt/myservice/myservice.sh脚本可以添加你自己想要执行的操作

例子如下:

#!/bin/bash

#chkconfig:1234 90 60

# Default-Start: 2 3 4 5

# Default-Stop: S 0 1 6

# description: Saves and restores system entropy pool for \

# higher quality random number generation

servername=myservice

serverdir=/opt/myservice

binpath=/opt/myservice/myservice.sh

prog=$(basename $binpath)

. /etc/init.d/functions

restart() {

stop

start

}

reload() {

stop

start

}

start() {

echo -n $"Starting $daemon:"

daemon $binpath start

retval=$

echo

[ $retval -eq 0 ]

}

stop() {

echo -n $"Stopping $daemon:"

daemon $binpath stop

retval=$

echo

[ $retval -eq 0 ]

}

ha_status() {

#status $prog

status $prog

ps -ef|grep $prog && exit 0

}

case "$1" in

start)

$1

;;

stop)

$1

;;

reload)

$1

;;

restart)

$1

;;

status)

ha_status

;;

*)

echo "Usage:$0 {start|stop|reload|restart|status}"

exit 1

esac

3.最后设置为开机启动设置chkconfig --level 2345 scriptname on