1.脚本放到 /etc/init.d 下 (init.d 是一个文件,不是文件夹)
2.随便找个目录放入脚本,将脚本copy 到 /etc/init.d 下
3.执行命令
cp /etc/opt/aams /etc/init.d/ 4.修改脚本中服务路径
vi 脚本 5.chkconfig 是管理系统服务(service)的命令行工具 。所谓系统服务(service),就是随系统启动而启动,随系统关闭而关闭的程序 。
chkconfig --add 脚本 6.查看是否加入系统自启动
chkconfig --list 7.赋予脚本权限
chmod +x 脚本 8.查看是否配置成功
service 脚本 status 脚本
#!/bin/bash# chkconfig: 2345 20 10# description: aams service#这里可替换为你自己的执行程序,其他代码无需更改DEPLOYDIR=/服务地址APP_NAME=//服务名stopF() { systemctl status firewalld | grep "running" &>/dev/nullif [ $? -eq 0 ]thensystemctl stop firewalld &>/dev/nullfi}#使用说明,用来提示输入参数usage() {echo "Usage: sh 脚本名.sh [start|stop|restart|status]"exit 1}#检查程序是否在运行is_exist(){pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}' `#如果不存在返回1,存在返回0if [ -z "${pid}" ]; thenreturn 1elsereturn 0fi}#启动方法start(){is_existif [ $? -eq "0" ]; thenecho "${APP_NAME} is already running. pid=${pid} ."elsestopFcd ${DEPLOYDIR}nohup /usr/local/jdk/jdk1.8.0_301/bin/java -jar ./$APP_NAME >/dev/null 2>&1 &echo "${APP_NAME} start success"fi}#停止方法stop(){is_existif [ $? -eq "0" ]; thenkill -9 $pidelseecho "${APP_NAME} is not running"fi}#输出运行状态status(){is_existif [ $? -eq "0" ]; thenecho "${APP_NAME} is running. Pid is ${pid}"elseecho "${APP_NAME} is NOT running."fi}#重启restart(){stopstart}#根据输入参数,选择执行对应方法,不输入则执行使用说明case "$1" in"start")start;;"stop")stop;;"status")status;;"restart")restart;;*)usage;;esac ng 脚本
#!/bin/sh## nginx - this script starts and stops the nginx daemon## chkconfig:- 85 15# description:NGINX is an HTTP(S) server, HTTP(S) reverse \#proxy and IMAP/POP3 proxy server# processname: nginx# config:/etc/nginx/nginx.conf# config:/etc/sysconfig/nginx# pidfile:/var/run/nginx.pid# Source function library.. /etc/rc.d/init.d/functions# Source networking configuration.. /etc/sysconfig/network# Check that networking is up.[ "$NETWORKING" = "no" ] && exit 0nginx="/usr/sbin/nginx" # nginx 默认启动路径 prog=$(basename $nginx)NGINX_CONF_FILE="/etc/nginx/nginx.conf"#nginx 配置文件[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginxlockfile=/var/lock/subsys/nginxmake_dirs() {# make required directoriesuser=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`if [ -n "$user" ]; thenif [ -z "`grep $user /etc/passwd`" ]; thenuseradd -M -s /bin/nologin $userfioptions=`$nginx -V 2>&1 | grep 'configure arguments:'`for opt in $options; doif [ `echo $opt | grep '.*-temp-path'` ]; thenvalue=https://tazarkount.com/read/`echo $opt | cut -d"=" -f 2`if [ ! -d "$value" ]; then# echo "creating" $valuemkdir -p $value && chown -R $user $valuefifidonefi}start() {[ -x $nginx ] || exit 5[ -f $NGINX_CONF_FILE ] || exit 6make_dirsecho -n $"Starting $prog: "daemon $nginx -c $NGINX_CONF_FILEretval=$?echo[ $retval -eq 0 ] && touch $lockfilereturn $retval}stop() {echo -n $"Stopping $prog: "killproc $prog -QUITretval=$?echo[ $retval -eq 0 ] && rm -f $lockfilereturn $retval}restart() {configtest || return $?stopsleep 1start}reload() {configtest || return $?echo -n $"Reloading $prog: "killproc $prog -HUPretval=$?echo}force_reload() {restart}configtest() {$nginx -t -c $NGINX_CONF_FILE}rh_status() {status $prog}rh_status_q() {rh_status >/dev/null 2>&1}case "$1" instart)rh_status_q && exit 0$1;;stop)rh_status_q || exit 0$1;;restart|configtest)$1;;reload)rh_status_q || exit 7$1;;force-reload)force_reload;;status)rh_status;;condrestart|try-restart)rh_status_q || exit 0;;*)echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"exit 2esac 【java 服务自启动】也可以通过配置软连接的方式修改脚本中的配置信息 软连接
- 春季老年人吃什么养肝?土豆、米饭换着吃
- 三八妇女节节日祝福分享 三八妇女节节日语录
- 老人谨慎!选好你的“第三只脚”
- 校方进行了深刻的反思 青岛一大学生坠亡校方整改校规
- 脸皮厚的人长寿!有这特征的老人最长寿
- 长寿秘诀:记住这10大妙招 100%增寿
- 春季老年人心血管病高发 3条保命要诀
- 眼睛花不花要看四十八 老年人怎样延缓老花眼
- 香槟然能防治老年痴呆症? 一天三杯它人到90不痴呆
- 老人手抖的原因 为什么老人手会抖
