我不确定这是否是最好的方法,这是我第一次做这些(包括编写shell脚本)。
操作系统:Centos
我的问题:我想在启动时启动多个shell脚本。 其中一个shell脚本是启动我自己的服务,另外三个是第三方服务。
启动我自己的服务的shell脚本将寻找jar文件。
我目前有两个服务(将改变),用Java编写。
所有服务都按照惯例prefix-service-servicename命名
我所做的:我创build了以下目录结构
/home/username/scripts init.sh boot/ boot/startthirdprtyservice1.sh boot/startthirdprtyservice2.sh boot/startthirdprtyservice3.sh boot/startmyservices.sh /home/username/services prefix-lib-libraryname.jar prefix-lib-libraryname.jar prefix-service-servicename.jar prefix-service-servicename.jar prefix-service-servicename.jar
在init.sh中,我有以下几点:
#!/bin/sh #This scripts run all executable scripts in the boot directory at boot #done by adding this script to the file /etc/rc.d/rc.local #nohup #run-parts /home/username/scripts/boot/* #for each file in the boot dir... # ignore the HUP (hangup) signal for s in ./boot/*;do if [ -x $s ]; then echo "Starting $s" nohup $s & fi done echo "Done starting bootup scripts " echo "\n"
在脚本启动/ startmyservices.sh我有
#!/bin/sh fnmatch () { case "$2" in $1) return 0 ;; esac ; return 1 ; } ##sub strin to match for SUBSTRING="prefix-service" for s in /home/username/services/*;do if [ -x $s ]; then #match service in the filename , ie only services are started if fnmatch "$SUBSTRING" "$s" ; then echo "Starting $s " nohup $s & fi fi done echo "Done starting Services" echo "\n"
最后:
通常你可以在/etc/rc.d/rc.local中join一个程序,让它在启动时运行,但是我不认为这在这种情况下是可行的,或者我不知道该把什么放在那里
我刚刚通过阅读了解了如何做到这一点,所以我不确定这是否是最好的select。
当我运行init.sh nohup.out包含
启动第三方守护进程…第三方开始……
但没有任何myservices.sh和我的Java服务没有运行
我不知道从哪里开始debugging,或者可能会出现什么问题。
编辑
发现一些问题,并得到它的工作,使用-x而不是-n来检查string是否为零,需要子string检查也是如果[[$ s = $ SUBSTRING ]]; 然后这最后一个只是愚蠢的,在$ s前面缺lessjava -jar 仍然不确定如何让init.sh在启动时运行
就我个人而言,我将使用init.d脚本来执行所有服务,而不是创build启动另一个进程的shell脚本。
所以看看创buildinit.d脚本,然后你可以使用chkconfig –add来将你的应用程序添加到启动。
这是一个例子
这是我在网上find的东西:
cp <script-file> /etc/init.d ln -s /etc/init.d/<scriptfile> /etc/rc.d/rc5.d/S50<scriptfile> ln -s /etc/init.d/<scriptfile> /etc/rc.d/rc5.d/K50<scriptfile>
S50是在系统启动时告诉系统启动脚本,K50是告诉系统关机时干净地closures系统。 该数字表示脚本应该以何种顺序启动/closures。