package org.openslx.taskmanager.tasks; import org.openslx.taskmanager.api.SystemCommandTask; import com.google.gson.annotations.Expose; public class Systemctl extends SystemCommandTask { /** * What to do: start, stop... */ @Expose private String operation; @Expose String service; private StatusObject status = new StatusObject(); @Override protected String[] initCommandLine() { this.timeoutSeconds = 15; return new String[] { "/usr/bin/sudo", "-n", "-u", "root", "/opt/taskmanager/scripts/systemctl", operation, service }; } private boolean errorIfNull( String check, String message ) { if ( check == null ) status.addMsg( message ); return check == null; } @Override protected boolean processEnded( int exitCode ) { return exitCode == 0; } @Override protected void processStdOut( String line ) { status.addMsg( line ); } @Override protected void processStdErr( String line ) { status.addMsg( line ); } @Override protected boolean initTask() { this.setStatusObject( this.status ); return !errorIfNull( operation, "No operation given" ); } class StatusObject { private String messages = ""; public void addMsg( String str ) { messages = messages + "\n" + str; } } }