`
收藏列表
标题 标签 来源
Linux环境下Java操控Tomcat、Apache自动重启 Linux环境下Java操控Tomcat、Apache自动重启
	//软件安装目录
	static final String TOMCAT_DIR = "/usr/local/tomcat/";
	static final String APACHE_DIR = "/usr/local/apache2/";
	//重试次数
	static final int RETRY_TIME = 10;
	
	/**
	 * 重启Tomcat
	 * @auther lupingui
	 * 2010-1-4 下午05:27:24
	 * @param runtime
	 * @return
	 * @throws IOException
	 */
	public static boolean restartTomcat(Runtime runtime) throws IOException{
		//结束tomcat进程
		for (int i = 0; i < RETRY_TIME; i++) {
			if(isExistTomcatProcess(runtime)) {
				//调用tomcat自身脚本结束进程
				shutdownTomcat(runtime);
				try {
					Thread.currentThread().sleep(5000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				if(isExistTomcatProcess(runtime)) {	
					//强制结束进程
					killTomcatBySoft(runtime);
					try {
						Thread.currentThread().sleep(5000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}else {
				break;
			}
		}		
		//启动tomcat
		for (int i = 0; i < RETRY_TIME; i++) {
			if(!isExistTomcatProcess(runtime)) {
				//调用tomcat自身脚本重启程序
				startupTomcat(runtime);
				try {
					Thread.currentThread().sleep(5000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}else {
				break;
			}
		}
		
		if(isExistTomcatProcess(runtime)) {
			return true;
		}else {
			return false;
		}
		
	}
	
	/**
	 * 判断是否含有tomcat进程
	 * @auther lupingui
	 * 2010-1-4 下午04:41:04
	 * @param runtime
	 * @return
	 * @throws IOException
	 */
	public static boolean isExistTomcatProcess(Runtime runtime) throws IOException {
		String program = "org.apache.catalina.startup.Bootstrap start";
		String command = "ps -C java -f --cols=1000";
		return isExistProcess(runtime, command, program);
	}
	
	/**
	 * 判断是否含有apache进程
	 * @auther lupingui
	 * 2010-1-4 下午04:41:14
	 * @param runtime
	 * @return
	 * @throws IOException
	 */
	public static boolean isExistApacheProcess(Runtime runtime) throws IOException {
		String program = APACHE_DIR + "bin/httpd";
		String command = "ps -C httpd -f --cols=1000";
		return isExistProcess(runtime, command, program);
	}
	
	/**
	 * 判断当前进程中是否含有program
	 * @auther lupingui
	 * 2010-1-4 下午04:41:18
	 * @param runtime
	 * @param command
	 * @param program
	 * @return
	 * @throws IOException
	 */
	public static boolean isExistProcess(Runtime runtime, String command, String program) throws IOException {
		boolean isExistTomcatProcess = false;
		
		Process process = runtime.exec(command);
		InputStream in = process.getInputStream();
		InputStreamReader is = new InputStreamReader(in);
		BufferedReader read = new BufferedReader(is);  
		String line = "";  
		while((line = read.readLine()) != null) {
			if(line.indexOf(program) >= 0) {
				isExistTomcatProcess = true;
				break;
			}
		}
		//---------------------
		in.close();
		is.close();
		read.close();
		process.destroy();
		//---------------------
		
		return isExistTomcatProcess;
	}
	
	/**
	 * 关闭Tomcat
	 * @auther lupingui
	 * 2010-1-4 下午04:41:28
	 * @param runtime
	 * @throws IOException
	 */
	public static void shutdownTomcat(Runtime runtime) throws IOException {
		runtime.exec(TOMCAT_DIR + "bin/shutdown.sh");
	}
	
	/**
	 * 启动Tomcat
	 * @auther lupingui
	 * 2010-1-4 下午04:41:56
	 * @param runtime
	 * @throws IOException
	 */
	public static void startupTomcat(Runtime runtime) throws IOException {
		runtime.exec(TOMCAT_DIR + "bin/startup.sh");
	}
	
	/**
	 * 重启Apache
	 * @auther lupingui
	 * 2010-1-4 下午05:30:06
	 * @param runtime
	 * @return
	 * @throws IOException
	 */
	public static boolean restartApache(Runtime runtime) throws IOException {
		if(isExistApacheProcess(runtime)) {
			runtime.exec(APACHE_DIR + "bin/apachectl restart");
		}else {
			runtime.exec(APACHE_DIR + "bin/apachectl start");
		}
		
		if (isExistApacheProcess(runtime)){
			return true;
		}else{
			return false;			
		}
	}
	
	/**
	 * 强制结束Tomcat进程
	 * @auther lupingui
	 * 2010-1-4 下午04:46:34
	 * @param runtime
	 * @throws IOException
	 */
	public static void killTomcatBySoft(Runtime runtime) throws IOException {
		String[] cmd = {"sh", "-c", "ps aux | grep tomcat"};
		Process process = runtime.exec(cmd);
		InputStream in = process.getInputStream();
		InputStreamReader is = new InputStreamReader(in);
		BufferedReader read = new BufferedReader(is);
		String line = null;  
		while((line = read.readLine()) != null) {
			if(line.indexOf("org.apache.catalina.startup.Bootstrap start") >= 0) {
				String tomcatPid = line.split("\\s+")[1];
				runtime.exec("kill -9 "+tomcatPid);
			}
		}
		in.close();
		is.close();
		read.close();
		process.destroy();
	}
MySql数据库创建用户以及赋予简单的权限 mysql MySql数据库创建用户以及赋予简单的权限
/*

//创建数据库用户
Create user yc identified by '123456'

//删除数据库用户
Drop user yc

//修改密码
SET Password FOR hity@"%" = password('1234')

//授予数据库用户对于某个数据库中某张表的具体权限
Grant SELECT,INSERT,UPDATE On xxkld.table_xxx To yc

//授予数据库用户全部权限对于某张表--可以对表进行增删改查
Grant ALL PRIVILEGES On xxkld.table_xxx To yc

//收回数据库用户对于所有表的权限
Revoke INSERT On  *.* To yc

*/
Django分页 django, 分页 Django 分页的简单实现
def list(request):
    after_range_num = 5
    bevor_range_num = 4
    try:
        page = int(request.GET.get("page",1))
        print('page----->',page)
        if page < 1:
            page = 1
    except ValueError:
        page = 1
    
    info = Article.objects.order_by('id').all()
    paginator = Paginator(info,3)
    
    
    try:
        articleList = paginator.page(page)
    except(EmptyPage,InvalidPage,PageNotAnInteger):
        articleList = paginator.page(1)
    print('articleList---->',articleList.object_list)
    #显示范围
    if page >= after_range_num:
        page_range = paginator.page_range[page-after_range_num:page+bevor_range_num]
    else:
        page_range = paginator.page_range[0:int(page)+bevor_range_num]
    return render_to_response("blogsite/list.html",locals())
Global site tag (gtag.js) - Google Analytics