ubuntu 12.10 32 bit vps 如何实现脚本自启动

sh/bash/dash/ksh/zsh等Shell脚本
回复
wingeong
帖子: 24
注册时间: 2006-02-27 20:45
系统: Ubuntu 13.04
来自: 石家庄
联系:

ubuntu 12.10 32 bit vps 如何实现脚本自启动

#1

帖子 wingeong » 2012-12-09 9:58

各位,

ubuntu 12.10 32位的vps服务器,安装nginx+php-fpm+mysql+magento+discuz!2.5+mumble chat server+mumble-django

发现nginx,php-fpm,mumble chat server,mumble-django均不能实现随机器的启动,自动启动。
即,当系统重启后,这些服务还需手动重启,请问,如何永久性实现这些服务的自启动?

相关脚本如下:

#把php-fpm,nginx,mumble,mumble-django加入服务,开机自启动
sudo nano /etc/rc.local
!/bin/sh -e

ulimit -SHn 65535
/usr/local/sbin/php-fpm start
/usr/local/nginx/sbin/nginx
service mumble-server start
cd /srv/mumble-django/pyweb
python manage.py syncdb
python manage.py runserver 0.0.0.0:8000
exit 0

然后每次开机,sudo netstat -ntlp
总是只发现mysql服务自启动了。而上述几项服务,都不能成功启动。无论是通过ssh登录的vps ,还是本机ubuntu 12.10 32位,均如何?为什么?如何解决?
期待各位高人解答。
上次由 wingeong 在 2013-01-07 15:32,总共编辑 1 次。
onlylove
论坛版主
帖子: 5371
注册时间: 2007-01-14 16:23

Re: ubuntu 12.10 32 bit vps 如何实现脚本自启动

#2

帖子 onlylove » 2012-12-09 12:16

目测少了个#,希望不是笔误,你可以把那些添加到系统服务,让系统给启动(做个软连接到/etc/init.d/下面,然后用服务管理程序添加)
#include <stdio.h>
void main()
{
double world;
unsigned letter;
short stay;
long memories;
printf("I miss you.\n");
}
wingeong
帖子: 24
注册时间: 2006-02-27 20:45
系统: Ubuntu 13.04
来自: 石家庄
联系:

Re: ubuntu 12.10 32 bit vps 如何实现脚本自启动

#3

帖子 wingeong » 2012-12-10 10:04

谢谢onlylove,但是仍无效:

我用先面2种命令均无效:
sudo update-rc.d nginx defaults 96 # 或sudo update-rc.d php-fpm defaults 97
sudo sysv-rc-conf nginx on 或 sudo sysv-rc-conf php-fpm

上述两条命令执行后,经重启,netstat -ntlp , 发现只有默认安装的mysql 启动了。nginx不启动。均无效。


经查,sysv-rc下,mysql与nginx的设定是一样的,却不启动nginx,何解?
同理,自行编译安装的php-fpm也存在同样的问题?何解?

sysv-rc-conf

代码: 全选

                               1                    2              3          4            5       6      0
mysql                                               x          x          x            x        
nginx                                               x          x          x            x     
附:ubuntu中没有chkconfig服务, 当然也可以安装,但ubuntu上推荐使用sysv-rc-conf来进行启动管理
#sudo apt-get install sysv-rc-conf
#sudo sysv-rc-conf
即可启动, 用空格键控制是否开机启动,X表示开机启动, =/+来控制启动, -表示stop,这些操作是实时生效的,所以要慎重操作
sysv-rc-conf的具体操作和chkconfig类似
可以使用sysv-rc-conf --list nginx来查看当前服务在各个运行级别下的开启情况,sysv-rc-conf --list可以查看所有守护进程的开启情况.
sysv-rc-conf [ --level levels ] service <on|off>可以设置在某个级别下是否启动还是关闭, levels如果不设置,默认为2~5级别, 比如sysv-rc-conf nginx on代表在2~5级别中自动启动nginx, sysv-rc-conf --level 35 nginx off表示在3和5级别中关闭nginx的默认启动.
关于linux的启动,要先从运行级别说起
Ubuntu 的系统运行级别:
*0:关机级别
*1:单用户运行级别,运行rc.sysinit和rc1.d目录下的脚本
*2:多用户,但系统不会启动NFS,字符模式,在有些linux系统中,级别2为默认模式,具有网络功能,如ubuntu.debian
*3:多用户,字符模式,系统启动具有网络功能,redhat常用运行级别
*4:用户自定义级别
*5:图形界面模式,redhat常用运行级别(2~5级为多用户)
*6:重启级别
*S:单用户运行级别,只运行rc.sysinit文件
onlylove
论坛版主
帖子: 5371
注册时间: 2007-01-14 16:23

Re: ubuntu 12.10 32 bit vps 如何实现脚本自启动

#4

帖子 onlylove » 2012-12-10 10:21

这不科学……
你先写个shell,如果这个shell能够正常启动那些服务的话,把这个shell脚本加到自启动里面,再试试看
#include <stdio.h>
void main()
{
double world;
unsigned letter;
short stay;
long memories;
printf("I miss you.\n");
}
wingeong
帖子: 24
注册时间: 2006-02-27 20:45
系统: Ubuntu 13.04
来自: 石家庄
联系:

Re: ubuntu 12.10 32 bit vps 如何实现脚本自启动

#5

帖子 wingeong » 2012-12-10 10:29

还望祥解。
命令文件在这,能成功没问题。如何加入系统?

/etc/init.d/nginx

代码: 全选

#! /bin/sh
### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: nginx init.d dash script for Ubuntu <=9.10.
# Description:       nginx init.d dash script for Ubuntu <=9.10.
### END INIT INFO
#------------------------------------------------------------------------------
# nginx - this Debian Almquist shell (dash) script, starts and stops the nginx 
#         daemon for ubuntu 9.10 and lesser version numbered releases.
#
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server.  This \
#		script will manage the initiation of the \
#		server and it's process state.
#
# processname: nginx
# config:      /usr/local/nginx/conf/nginx.conf
# pidfile:     /var/run/nginx.pid
# Provides:    nginx
#
# Author:  Jason Giedymin
#          <jason.giedymin AT gmail.com>.
#
# Version: 2.0 02-NOV-2009 jason.giedymin AT gmail.com
# Notes: nginx init.d dash script for Ubuntu <=9.10.
# 
# This script's project home is:
# 	http://code.google.com/p/nginx-init-ubuntu/
#
#------------------------------------------------------------------------------
#                               MIT X11 License
#------------------------------------------------------------------------------
#
# Copyright (c) 2009 Jason Giedymin, http://Amuxbit.com formerly
#				     http://AcronymLabs.com
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#------------------------------------------------------------------------------
 
#------------------------------------------------------------------------------
#                               Functions
#------------------------------------------------------------------------------
. /lib/lsb/init-functions
 
#------------------------------------------------------------------------------
#                               Consts
#------------------------------------------------------------------------------
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/nginx/sbin/nginx
 
PS="nginx"
PIDNAME="nginx"				#lets you do $PS-slave
PIDFILE=$PIDNAME.pid                    #pid file
PIDSPATH=/var/run
 
DESCRIPTION="Nginx Server..."
 
RUNAS=root                              #user to run as
 
SCRIPT_OK=0                             #ala error codes
SCRIPT_ERROR=1                          #ala error codes
TRUE=1                                  #boolean
FALSE=0                                 #boolean
 
lockfile=/var/lock/subsys/nginx
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
 
#------------------------------------------------------------------------------
#                               Simple Tests
#------------------------------------------------------------------------------
 
#test if nginx is a file and executable
test -x $DAEMON || exit 0
 
# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
        . /etc/default/nginx
fi
 
#set exit condition
#set -e
 
#------------------------------------------------------------------------------
#                               Functions
#------------------------------------------------------------------------------
 
setFilePerms(){
 
        if [ -f $PIDSPATH/$PIDFILE ]; then
                chmod 400 $PIDSPATH/$PIDFILE
        fi
}
 
configtest() {
	$DAEMON -t -c $NGINX_CONF_FILE
}
 
getPSCount() {
	return `pgrep -f $PS | wc -l`
}
 
isRunning() {
        if [ $1 ]; then
                pidof_daemon $1
                PID=$?
 
                if [ $PID -gt 0 ]; then
                        return 1
                else
                        return 0
                fi
        else
                pidof_daemon
                PID=$?
 
                if [ $PID -gt 0 ]; then
                        return 1
                else
                        return 0
                fi
        fi
}
 
#courtesy of php-fpm
wait_for_pid () {
        try=0
 
        while test $try -lt 35 ; do
 
                case "$1" in
                        'created')
                        if [ -f "$2" ] ; then
                                try=''
                                break
                        fi
                        ;;
 
                        'removed')
                        if [ ! -f "$2" ] ; then
                                try=''
                                break
                        fi
                        ;;
                esac
 
                #echo -n .
                try=`expr $try + 1`
                sleep 1
        done
}
 
status(){
	isRunning
	isAlive=$?
 
	if [ "${isAlive}" -eq $TRUE ]; then
                echo "$PIDNAME found running with processes:  `pidof $PS`"
        else
                echo "$PIDNAME is NOT running."
        fi
 
 
}
 
removePIDFile(){
	if [ $1 ]; then
                if [ -f $1 ]; then
        	        rm -f $1
	        fi
        else
		#Do default removal
		if [ -f $PIDSPATH/$PIDFILE ]; then
        	        rm -f $PIDSPATH/$PIDFILE
	        fi
        fi
}
 
start() {
        log_daemon_msg "Starting $DESCRIPTION"
 
	isRunning
	isAlive=$?
 
        if [ "${isAlive}" -eq $TRUE ]; then
                log_end_msg $SCRIPT_ERROR
        else
                start-stop-daemon --start --quiet --chuid $RUNAS --pidfile $PIDSPATH/$PIDFILE --exec $DAEMON \
                -- -c $NGINX_CONF_FILE
                setFilePerms
                log_end_msg $SCRIPT_OK
        fi
}
 
stop() {
	log_daemon_msg "Stopping $DESCRIPTION"
 
	isRunning
	isAlive=$?
        if [ "${isAlive}" -eq $TRUE ]; then
                start-stop-daemon --stop --quiet --pidfile $PIDSPATH/$PIDFILE
 
		wait_for_pid 'removed' $PIDSPATH/$PIDFILE
 
                if [ -n "$try" ] ; then
                        log_end_msg $SCRIPT_ERROR
                else
                        removePIDFile
	                log_end_msg $SCRIPT_OK
                fi
 
        else
                log_end_msg $SCRIPT_ERROR
        fi
}
 
reload() {
	configtest || return $?
 
	log_daemon_msg "Reloading (via HUP) $DESCRIPTION"
 
        isRunning
        if [ $? -eq $TRUE ]; then
		`killall -HUP $PS` #to be safe
 
                log_end_msg $SCRIPT_OK
        else
                log_end_msg $SCRIPT_ERROR
        fi
}
 
quietupgrade() {
	log_daemon_msg "Peforming Quiet Upgrade $DESCRIPTION"
 
        isRunning
        isAlive=$?
        if [ "${isAlive}" -eq $TRUE ]; then
		kill -USR2 `cat $PIDSPATH/$PIDFILE`
		kill -WINCH `cat $PIDSPATH/$PIDFILE.oldbin`
 
		isRunning
		isAlive=$?
		if [ "${isAlive}" -eq $TRUE ]; then
			kill -QUIT `cat $PIDSPATH/$PIDFILE.oldbin`
			wait_for_pid 'removed' $PIDSPATH/$PIDFILE.oldbin
                        removePIDFile $PIDSPATH/$PIDFILE.oldbin
 
			log_end_msg $SCRIPT_OK
		else
			log_end_msg $SCRIPT_ERROR
 
			log_daemon_msg "ERROR! Reverting back to original $DESCRIPTION"
 
			kill -HUP `cat $PIDSPATH/$PIDFILE`
			kill -TERM `cat $PIDSPATH/$PIDFILE.oldbin`
			kill -QUIT `cat $PIDSPATH/$PIDFILE.oldbin`
 
			wait_for_pid 'removed' $PIDSPATH/$PIDFILE.oldbin
                        removePIDFile $PIDSPATH/$PIDFILE.oldbin
 
			log_end_msg $SCRIPT_ok
		fi
        else
                log_end_msg $SCRIPT_ERROR
        fi
}
 
terminate() {
        log_daemon_msg "Force terminating (via KILL) $DESCRIPTION"
 
	PIDS=`pidof $PS` || true
 
	[ -e $PIDSPATH/$PIDFILE ] && PIDS2=`cat $PIDSPATH/$PIDFILE`
 
	for i in $PIDS; do
		if [ "$i" = "$PIDS2" ]; then
	        	kill $i
                        wait_for_pid 'removed' $PIDSPATH/$PIDFILE
			removePIDFile
		fi
	done
 
	log_end_msg $SCRIPT_OK
}
 
destroy() {
	log_daemon_msg "Force terminating and may include self (via KILLALL) $DESCRIPTION"
	killall $PS -q >> /dev/null 2>&1
	log_end_msg $SCRIPT_OK
}
 
pidof_daemon() {
    PIDS=`pidof $PS` || true
 
    [ -e $PIDSPATH/$PIDFILE ] && PIDS2=`cat $PIDSPATH/$PIDFILE`
 
    for i in $PIDS; do
        if [ "$i" = "$PIDS2" ]; then
            return 1
        fi
    done
    return 0
}
 
case "$1" in
  start)
	start
        ;;
  stop)
	stop
        ;;
  restart|force-reload)
	stop
	sleep 1
	start
        ;;
  reload)
	$1
	;;
  status)
	status
	;;
  configtest)
        $1
        ;;
  quietupgrade)
	$1
	;;
  terminate)
	$1
	;;
  destroy)
	$1
	;;
  *)
	FULLPATH=/etc/init.d/$PS
	echo "Usage: $FULLPATH {start|stop|restart|force-reload|status|configtest|quietupgrade|terminate|destroy}"
	echo "       The 'destroy' command should only be used as a last resort." 
	exit 1
	;;
esac
 
exit 0

onlylove
论坛版主
帖子: 5371
注册时间: 2007-01-14 16:23

Re: ubuntu 12.10 32 bit vps 如何实现脚本自启动

#6

帖子 onlylove » 2012-12-10 10:43

这不是nginx的启动脚本么……把这个脚本复制到/etc/init.d/然后,用update-rc.d 添加启动就行了,如果启动不了……我只能说……我也不知道为什么,你要不要看下ubuntu有没有搞什么特殊的启动服务管理程序
#include <stdio.h>
void main()
{
double world;
unsigned letter;
short stay;
long memories;
printf("I miss you.\n");
}
wingeong
帖子: 24
注册时间: 2006-02-27 20:45
系统: Ubuntu 13.04
来自: 石家庄
联系:

Re: ubuntu 12.10 32 bit vps 如何实现脚本自启动

#7

帖子 wingeong » 2012-12-10 10:55

如果启动不了……我只能说……我也不知道为什么,你要不要看下ubuntu有没有搞什么特殊的启动服务管理程序
是的

我记得曾经没问题的

但现在的系统,,,

因为搞不定,刚重装了系统,仍无效?因此很奇怪,而且vps server(ubuntu 12.04 32 bit)上也是同样问题?不知道怎么弄了,google不到解决方案。。。

还是非常感谢!!
头像
sunfish
帖子: 989
注册时间: 2008-05-03 1:53

Re: ubuntu 12.10 32 bit vps 如何实现脚本自启动

#8

帖子 sunfish » 2012-12-20 13:12

用绝对路径
docker && kubernetes
回复