## 开启自启动 ### Tomcat 开机自启动 ```shell #1.新增脚本 cd /etc/init.d vim tomcat chmod +x tomcat #2.配置为系统服务器 chkconfig --add tomcat #3.启动服务 service tomcat start ``` 脚本 /etc/init.d/tomcat 内容如下: ```shell #!/bin/sh # chkconfig: 2345 40 60 # description: Start Or Stop Tomcat # author: liuchuanwei.ex # version: v1.01 EXEC=/app/tomcat/bin/startup.sh DIR=/app/tomcat PID=`ps --no-heading -C java -f --width 1000 | grep $DIR |awk '{print $2}'` if [[ ! -x /proc/${PID} || -z $PID ]]; then PID=0 fi case "$1" in start) if [ $PID -eq 0 ]; then sh $EXEC else echo "Tomcat is already running" fi ;; stop) if [ $PID -eq 0 ]; then echo "Tomcat is not running" else echo "Shutting down tomcat ($PID)" kill -9 $PID > /dev/null 2>&1 RETVAL=$? if [ $RETVAL -eq 0 ]; then echo "Tomcat is stoped" fi fi ;; status) if [ $PID -eq 0 ]; then echo 'Tomcat is not running' else echo "Tomcat is running ($PID)" fi ;; restart|force-reload) ${0} stop ${0} start ;; *) echo "Please use start or stop as first argument" ;; esac ``` ### Redis 开机自启动 ```shell #1.复制脚本 cp /home/redis/utils/redis_init_script /etc/init.d/redis #2.复制配置 mkdir /etc/redis cp /home/redis/redis.conf /etc/redis/redis_6379.conf cp /home/redis/sentinel.conf /etc/redis/redis-sentinel_26379.conf #3.修改脚本,在 `#!bin/sh` 下新增一行 `# chkconfig: 2345 10 90 ` vim /etc/init.d/redis #4.新增系统服务 chkconfig --add redis #5.启动服务 service redis start ``` 脚本 /etc/init.d/redis 内容如下: ```shell #!/bin/sh # chkconfig: 2345 10 90 # description: Start Or Stop Redis Server # author: liuchuanwei.ex # version: v1.01 #redis配置 REDISPORT=6379 EXEC=/home/redis/src/redis-server CLIEXEC=/home/redis/src/redis-cli PIDFILE=/var/run/redis_${REDISPORT}.pid CONF="/etc/redis/redis_${REDISPORT}.conf" REDISPASSWORD="i!E@22h98lpQ5" #哨兵配置 SENTINEL_PORT=26379 SENTINEL_EXEC=/home/redis/src/redis-sentinel SENTINEL_CONF="/etc/redis/redis-sentinel_${SENTINEL_PORT}.conf --sentinel" case "$1" in start) if [ -f $PIDFILE ] then echo "$PIDFILE exists, process is already running or crashed" else echo "Starting Redis server..." #启动redis $EXEC $CONF #启动哨兵 $EXEC $SENTINEL_CONF fi ;; stop) if [ ! -f $PIDFILE ] then echo "$PIDFILE does not exist, process is not running" else PID=$(cat $PIDFILE) echo "Stopping ..." #关闭redis $CLIEXEC -a $REDISPASSWORD -p $REDISPORT shutdown #关闭哨兵 $SENTINEL_EXEC -p $SENTINEL_PORT shutdown sleep 2 while [ -x /proc/${PID} ] do echo "Waiting for Redis to shutdown ..." sleep 1 done echo "Redis stopped" fi ;; status) PID=$(cat $PIDFILE) if [ ! -x /proc/${PID} ] then echo 'Redis is not running' else echo "Redis is running ($PID)" fi ;; restart|force-reload) ${0} stop ${0} start ;; *) echo "Please use start or stop as first argument" ;; esac ``` ## 后台运行 **首先理解任务是如何中断的** 当我们关闭终端,shell默认会发送中断信号给该终端session关联的进程,从而导致进程跟随终端退出。 **两种中断信号:** 1)sigint:signal interrupt,ctrl+c会发送此信号,主动关闭程序 2)sighup: signal hang up,关闭终端,ssh掉线,关闭屏幕会发送此挂断信号。 几种后台运行任务的方法: **1)&符号** 将&符号放在命令最后面,使启动的程序忽略sigint信号,此时执行ctrl+c不会关闭进程,但当断网或关闭终端时,仍会造成进程退出。 例:bash test.sh & **2)nohup指令** nohup(no hang up),指不挂断运行,使用nohup在后台执行任务,和用户终端没有关系,所以断开ssh也不影响运行,nohup捕获了sighup信号,并做了忽略处理,因此断开终端不会关闭进程,但使用ctrl+c可以关闭该进程,所以一般用nohup+&结合使用。 例:nohup command **3)结合使用** 方法1 ```shell nohup command &>> log.out & ``` 方法2 ```shell su - root -c "command &>> log.out &" ``` 4)运行后补救措施 当我们已经在前台开启了一个任务,想让其在后台运行,但又不想中断后重新开启该怎么办呢? ①将作业挂起 ctrl+z ②查看作业号 ```shell jobs ``` ③将挂起的作业在后台运行 ```shell bg jobid ``` 如果想继续放在前台运行:`fg jobid`