Linux程序自启动(nginx,redis,tomcat)

一 nginx

登录linux服务器(我的服务器版本是CentOS release 6.10 (Final))
进入服务文件夹 cd /etc/init.d/
通过ll查看是否存在ngixn的文件,因为通过yum下载安装的nginx的话,是会在该文件目录下生成对应的服务脚本的,如果没有的话,通过vim nginx创建一个空的文件

# chkconfig: 2345 10 90
# description:  #auto start nginx
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
pidfile=/var/run/nginx/nginx.pid

Start_Nginx(){
  if [ -f $pidfile ];then
    echo "Nginx is running"
  else
    /root/java/nginx-1.8.0/sbin/nginx &>/dev/null
    action "Nginx is Started" /bin/true
  fi
}
Stop_Nginx(){
    if [ -f $pidfile ];then
    /root/java/nginx-1.8.0/sbin/nginx -s stop &>/dev/null
    action "Nginx is Stopped" /bin/true
  else
    echo "Nginx is already Stopped"
  fi
}
Reload_Nginx(){
  if [ -f $pidfile ];then
    /root/java/nginx-1.8.0/sbin/nginx -s reload &>/dev/null
    action "Nginx is Reloaded" /bin/true
  else
    echo "Can't open $pidfile ,no such file or directory"
  fi
}

case $1 in
  start)
    Start_Nginx
    RETVAL=$?
    ;;
  stop)
    Stop_Nginx
    RETVAL=$?
    ;;
  restart)
    Stop_Nginx
    sleep 3
    Start_Nginx
    RETVAL=$?
    ;;
  reload)
    Reload_Nginx
    RETVAL=$?
    ;;
  *)
    echo "USAGE: $0 {start|stop|reload|restart}"
    exit 1
esac
exit $RETVAL

添加以上代码其中:
/root/java/nginx-1.8.0/sbin/nginx/var/run/nginx/nginx.pid需要修改为自己的nginx启动文件.pid文件一般在conf文件中可以找到,如果实在找不到的话,可以通过
find / -name nginx.pid查找
为刚刚创建的文件添加执行权限
chmod +x nginx
将nginx添加到服务列表中
chkconfig nginx on
这样nginx的自启动就配置好了

二 redis

redis 的配置基本与上面的nginx相同,配置文件如下:

#!/bin/sh
#chkconfig: 2345 80 90
#description:auto_run
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

# source function library
. /etc/rc.d/init.d/functions

# pull in sysconfig settings
[ -f /etc/sysconfig/sshd ] && . /etc/sysconfig/sshd



REDISPORT=6379
EXEC=/root/web/redis-3.0.0/bin/redis-server
CLIEXEC=/root/web/redis-3.0.0/bin/redis-cli
 
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/root/web/redis-3.0.0/bin/redis.conf"
 
case "$1" in
    start)
        if [ -f $PIDFILE ]
        then
                echo "$PIDFILE exists, process is already running or crashed"
        else
                echo "Starting Redis server..."
                $EXEC $CONF
        fi
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                $CLIEXEC -a zcredis -p $REDISPORT shutdown
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac

其中EXEC(redis-server),CLIEXEC(redis-cli),REDISPORT(port),PIDFILE(pid)CONF(conf)等文件的路径修改为自己的路径
剩下的操作与上面的nginx相同

三 tomcat

配置文件如下:

#!/bin/sh
# chkconfig: 345 99 10
# description: Auto-starts tomcat
# /etc/init.d/tomcat_8083
# Tomcat auto-start
# Source function library.
#. /etc/init.d/functions
# source networking configuration.
#. /etc/sysconfig/network
RETVAL=0
export JRE_HOME=/root/java/jdk1.8.0_151
export CATALINA_HOME=/home/java/apache-tomcat-8.5.23
export CATALINA_BASE=/home/java/apache-tomcat-8.5.23
start()
{
if [ -f $CATALINA_HOME/bin/startup.sh ];
then
echo $"Starting Tomcat"
$CATALINA_HOME/bin/startup.sh
RETVAL=$?
echo " OK"
return $RETVAL
fi
}
stop()
{
if [ -f $CATALINA_HOME/bin/shutdown.sh ];
then
echo $"Stopping Tomcat"
$CATALINA_HOME/bin/shutdown.sh
RETVAL=$?
sleep 1
ps -fwwu tomcat | grep apache-tomcat|grep -v grep | grep -v PID | awk '{print $2}'|xargs kill -9
echo " OK"
# [ $RETVAL -eq 0 ] && rm -f /var/lock/...
return $RETVAL
fi
}
case $1 in
start)start;;
stop)stop;;
restart)
echo $"Restaring Tomcat"
$0 stop
sleep
$0 start
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 1
;;
esac

exit $RETVAL

其中:JRE_HOME,CATALINA_HOME,CATALINA_BASE修改为自己的文件路径
其余操作相同

你可能感兴趣的:(Linux程序自启动(nginx,redis,tomcat))