标签存档: script

Linux自动运行脚本

windows下开机启动某个程序比较简单,只需要把程序添加到”程序-启动”栏即可.

对于linux下开机自动运行程序笔记麻烦. 以Apache Tomcat为例. 做开机自动启动


				SERVICE_HOME="/home/tomcat"

				SERVICE_DESC="Apache Tomcat"

				SERVICE_USER="tomcat"

				CONSOLE_LOG="$SERVICE_HOME/console.log"

				START_SCRIPT="startup.sh"

				SHUTDOWN_SCRIPT="shutdown.sh"

				start(){

				echo "Starting $SERVICE_DESC Server ..."

				cd $SERVICE_HOME/bin

				sudo -u $SERVICE_USER ./$START_SCRIPT > $CONSOLE_LOG 2> $CONSOLE_LOG &

				}

				stop(){

				echo "Stopping $SERVICE_DESC Server ... "

				cd $SERVICE_HOME/bin

				sudo -u $SERVICE_USER ./$SHUTDOWN_SCRIPT > $CONSOLE_LOG 2> $CONSOLE_LOG &

				}

				restart(){

				stop

				# give stuff some time to stop before we restart

				sleep 5s

				start

				}

				case "$1" in

				start)

				start

				;;

				stop)

				stop

				;;

				restart)

				restart

				;;

				*)

				echo "Usage: tomcat {start|stop|restart}"

				exit 1

				esac

				exit 0

 

 

设置:

1、保存脚本到/ect/init.d/目录下,保存名tomcat.sh

2、进入/etc/rc3.d/目录,新建一个软链接 sudo ln -s  ../init.d/tomcat.sh tomcat

 

注:ect目录下有很多rc1.d、rc2.d….这类的目录,这里是linux用来设置不同程序启动顺序用,数字越小启动优先级越高,一般而言我们自己的程序放在rc3.d目录下即可