Windows Dienste als Liste?

Ävi

Grünschnabel
hallo,

ich hab nichts über die Suche hier im Forum gefunden.

mein problem ist folgendes:

ich möchte den Apache Tomcat dienst starten/stoppen/checken
ansich wenn ich weiß wie dieser dienst heißt z.b 'Apache Tomcat' oder 'Apache Tomcat 6' kann ich ihn direkt starten/stoppen was ich auch machen möchte

nur problem ist folgendes der dienst kann wie die 2 beispiele schon zeigen, verschiedene Bezeichnungen haben.

bsp code wie ich den Apache z.b checke.

das problem das waitfor() gibt nur 0 oder 1 zurück .. 0 er läuft .. 1 er ist aus ODER nicht vorhanden

ich hab versucht auch über andere net befehle andere ECHO %ERRORLEVEL% msgs zu bekommen und evtl über diese somit den richtigen apache zu filtern aber da hab ich nur immer die selben echos bekommen



Java:
public static int checkApache() throws InterruptedException, IOException {
		// setting the name of the service
		String serviceName = "\"Apache Tomcat\"";
		// setting up the command
		String[] command = { "cmd", "/c", "net", "start", "|", "find",
				serviceName };
		// adding the command to a proccessbuilder
		ProcessBuilder processBuilder = new ProcessBuilder(command);
		// starting the process
		Process process = processBuilder.start();
		// saving the echo answer of the command - 0 its running - 1 its offline
		result = process.waitFor();
		//returning the result
		return result;
	}

die Start bzw stop methode

Java:
public static void stopApache() throws Exception {

		checkApache();

		if (result == 0) {
			
			// info logging 
			log.info("Shutting down Apache Server");
			// creating the command to stop the apache
			String serviceStop = "\"Apache Tomcat\"";
			
			String[] commandstop = { "cmd", "/c", "net", "stop", serviceStop };

			
			ProcessBuilder processBuilderStop = new ProcessBuilder(commandstop); 

			
			processBuilderStop.start();
			
			System.out.println("stopped");

		} else {
			log.info("Apache already down");

		}
	}

ich hab nichts gefunden wie ich mir die kompletten Dienste als Liste übergeben könnte und so dann rausfiltere welche Bezeichnung die richtige auf dem jeweiligen System ist.

nun könnte man meinen ich rufe einfach erst die stopApache methode auf wenn ich ihn anhalten will und die startApache wenn er wieder anlaufen soll

nur ich brauche den check als universelle für alle namensgebungen damit ich überprüfen kann in welchem status er sich momentan befindet

das ganze hängt in nem speicher zyklus mit drin und ich kann net einfach starten stoppen wanns mir passt

wenn jemand weiß wie man so ne Windows dienst liste erstellt oder bsp code hat einfach reinposten

mfg

avi
 
Zuletzt bearbeitet von einem Moderator:
okay ich hab da glaub was gefunden wie ich direkt auf das command

net start | find "Apache" das direkte ECHO auswerten kann und somit einfach über das alles filtern kann

ich probiers mal noch voll fertig und post dnan den code rein
 
Java:
public static String versionApache() throws IOException {
		// setting the name of the service
		String serviceName = "\"Apache\"";
		// setting up the command
		String[] command = { "cmd", "/c", "net", "start", "|", "find",
				serviceName };
		// adding the command to a proccessbuilder
		ProcessBuilder processBuilder = new ProcessBuilder(command);
		// starting the process
		Process process = processBuilder.start();
		
		InputStream out = process.getInputStream();
		int dec = 0;
		
		String apacherenewed = new String();
		String apache = new String();
		while ((dec = out.read()) != -1) {
			char c = (char) dec;
			apache+=c;
			System.out.print(c +" - "+ dec+"\n");
		}
		System.out.println(apache);
		
		for(int counter = 0; counter<apache.length();counter++){
			if(counter==0){
				apacherenewed +="\"";
				apacherenewed +=apache.charAt(counter);
			}else if(counter==(apache.length()-3)){
				apacherenewed+=apache.charAt(counter)+"\"";
			}else{
				apacherenewed+=apache.charAt(counter);
			}
			
		}
			System.out.print(apacherenewed);	
		
		return apacherenewed;
	}

so ich hab das echo mal in nen string gespeichert

den string dann wiederrum die notwendigen zeichen hinzugefügt in dem fall nen Apostroph am Anfang und ende

das übergeb ich dann einfach an start/stop und es passt

mfg

avi
 
Zuletzt bearbeitet von einem Moderator:
okay grad getestet und die leerzeichen sind doch von bedeutung :)

muss man die noch aus dem String wegbekommen die ersten 3 die sind afaik bei jedem befehl dabei habs an 2 stations getestet

aber ansonsten stimmt er obige code

mfg

avi
 
Warum liest du das denn nicht über einen BufferedReader ein?

new String() ist übrigens pfui... nimm stattdessen "". Das kommt auf dem Stringpool während new String() explizit ein neues Objekt erstellt.
 
heysen,

danke für die tips noch das mit new String() hab ich irgendwie selber übersehn gehabt war auch noch fix am ende gemacht :)

aber für was bufferedReader ich bekomm den inputstream und konvertier die bytes einfach um.

egal back to topic

gab noch 1 problem das sich gestellt hat. Wenn der apache dienst NICHT gestartet ist bekomm ich auch keinen inputstream über den kompletten namen zurück also muss man zuerst den dienst mal irgendwie gestartet bekommen.



ich hab das jetzt vorerst so gelöst dass ich einfach verschiedene namensvarianten als command durchstarte .. die lösung ist zwar nicht grad elegant aber ich bin noch nicht dahintergekommen wie ich nach nem dienst suchen kann der überhaupt nicht gestartet ist mit irgendwelchen cmd commands. Wenn jemand von euch da nen tipp hätte wäre nice

man muss so wies atm gecoded ist noch per hand zusätzliche apache name tags mit einfügen oder man regelt das über ne extra datei und noch mit nem gui oder whatever zusammen wenn man mehr userfreundlichkeit einbauen möchte
aber das kann ja jeder individuell lösen


also hier mal meine komplett lösung zum Apache handling.

am anfang wie gesagt muss man erstmal den dienst starten falls er down sein sollte

Java:
public static void main(String[] args) {
		{
			PropertyConfigurator.configure("logs/log4j.properties");
			try {
				
				while(parser.bootup.versionApache().equals("")){
					System.out.println("whatthefuck");
					parser.bootup.firstStart();
				}
				trie = parser.bootup.versionApache();
				System.out.println("DONE");
				parser.bootup.apacheSet(trie);
				doc = parser.XMLUtility.createDoc();
				javax.swing.SwingUtilities.invokeLater(new Runnable() {
					public void run() {
						try {
							new View();
						} catch (ParserConfigurationException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (SAXException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				});
			} catch (Exception e) {

			}
		}
	}

Java:
public class bootup {

	static Integer result;
	static Integer result2;
	static Integer result3;
	static Logger log = Logger.getLogger(bootup.class);
	static String apacheresolved = "";
	
	static InputStream out;
	static String apachefinal = "";
	static String apacherenewed = "";
	static String apache = "";
	
	public static void firstStart() throws IOException{
		
		// setting the name of the service
		String serviceName = "\"Apache Tomcat\"";
		// setting up the command
		String[] command = { "cmd", "/c", "net", "start",serviceName };
		// adding the command to a proccessbuilder
		ProcessBuilder processBuilder = new ProcessBuilder(command);
		// starting the process
	
		processBuilder.start();
	
			
		
		
		
		// setting the name of the service
		String serviceName1 = "\"Apache Tomcat 6\"";
		// setting up the command
		String[] command1 = { "cmd", "/c", "net", "start",serviceName1 };
		// adding the command to a proccessbuilder
		ProcessBuilder processBuilder1 = new ProcessBuilder(command1);
		// starting the process
		
		processBuilder1.start();
		
		// setting the name of the service
		String serviceName2 = "\"Apache Tomcat 6.0\"";
		// setting up the command
		String[] command2 = { "cmd", "/c", "net", "start",serviceName2 };
		// adding the command to a proccessbuilder
		ProcessBuilder processBuilder2 = new ProcessBuilder(command2);
		// starting the process
		
		processBuilder2.start();
		
		
		
				
	}
	
	public static String versionApache(){
		apache="";
		apachefinal="";
		apacherenewed="";
		try{
		System.out.println("sdfsdfsdfsdfsd");
		// setting the name of the service
		String serviceName = "\"Apache\"";
		// setting up the command
		String[] command = { "cmd", "/c", "net", "start", "|", "find",
				serviceName };
		// adding the command to a proccessbuilder
		ProcessBuilder processBuilder = new ProcessBuilder(command);
		// starting the process
	
		Process process = processBuilder.start();
		
		 out = process.getInputStream();

		int dec = 0;
		
		while ((dec = out.read()) != -1) {
			char c = (char) dec;
			apache+=c;
		//	System.out.print(c +" - "+ dec+"\n");
		}
		System.out.println(apache);
		System.out.println("sfsdf");
		
		for(int counter = 0; counter<apache.length()-2;counter++){
			if(counter==0){
				apacherenewed +="\"";
				apacherenewed +=apache.charAt(counter);
			}else if(counter==(apache.length()-3)){
				apacherenewed+=apache.charAt(counter)+"\"";
			}else{
				apacherenewed+=apache.charAt(counter);
			}
			
		}
			
			
			for(int counter2=0; counter2<apacherenewed.length();counter2++){
				if(counter2== 1 || counter2 == 2 || counter2 == 3){
					
				}else{
					apachefinal+=apacherenewed.charAt(counter2);
				}
			}
		System.out.println(apachefinal);
		
}catch (IOException e){
			
		}
		
		return apachefinal;
	}

	/**********************************************************************************
	 * 
	 * The Method checkApache() is build to check two Apache Tomcat types. The
	 * first is 'Apache Tomcat' and the second 'Apache Tomcat 6'.
	 * 
	 * 
	 * @return
	 * @throws InterruptedException
	 * @throws IOException
	 * 
	 **********************************************************************************/
	public static int checkApache() throws InterruptedException, IOException {
		// setting the name of the service
		String serviceName = apacheresolved;
		System.out.println(serviceName);
		// setting up the command
		String[] command = { "cmd", "/c", "net", "start", "|", "find",
				serviceName };
		// adding the command to a proccessbuilder
		ProcessBuilder processBuilder = new ProcessBuilder(command);
		// starting the process
		Process process = processBuilder.start();

		
		// saving the echo answer of the command - 0 its running - 1 its offline
		result = process.waitFor();
		System.out.println(result);
		// returning the result
		return result;
	}

	/**********************************************************************************
	 * 
	 *	
	 * 
	 **********************************************************************************/
	public static void stopApache() throws Exception {

		checkApache();

		if (result == 0) {

			// info logging
			log.info("Shutting down Apache Server");
			// creating the command to stop the apache
			String serviceStop = apacheresolved;
			System.out.println(serviceStop);
//			String serviceStop2 = "\"Apache Tomcat 6\"";
			String[] commandstop = { "cmd", "/c", "net", "stop", serviceStop };

//			String[] commandstop2 = { "cmd", "/c", "net", "stop", serviceStop2 };
			ProcessBuilder processBuilderStop = new ProcessBuilder(commandstop); // same

//			ProcessBuilder processBuilderStop2 = new ProcessBuilder(
//					commandstop2);
			processBuilderStop.start();

//			processBuilderStop2.start();
			System.out.println("stopped");

		} else {
			log.info("Apache already down");

		}
	}

	/**********************************************************************************
	 * 
	 * Starts the Apache Server. first at all it checks if the Apache Server is
	 * already started if not it starts the server
	 * 
	 * @throws Exception
	 **********************************************************************************/
	public static void startApache() throws Exception {

		checkApache();
		if (result == 0) {
			log.info("Apache Server already running");

		}else if(result ==2){
			System.out.println("er läuft auch schon");
		}else {
			log.info("Starting Apache Server");
			String serviceStart = apacheresolved;
//			String serviceStart2 = "\"Apache Tomcat 6\"";
			String[] commandStart = { "cmd", "/c", "net", "start", serviceStart };
//			String[] commandStart2 = { "cmd", "/c", "net", "start",
//					serviceStart2 };
			ProcessBuilder processBuilderStart = new ProcessBuilder(
					commandStart);
//			ProcessBuilder processBuilderStart2 = new ProcessBuilder(
//					commandStart2);

			processBuilderStart.start();

//			processBuilderStart2.start();
			System.out.println(serviceStart + " is running!");

		}
	}
	
	public static  void apacheSet(String s){
		
		bootup.apacheresolved = s;
		
	}
}
 
Zuletzt bearbeitet von einem Moderator:
Zurück