wtorek, 17 marca 2015

This account is currently not available - how to execute command as user with nologin shell set

How to run a command as a user without bash shell set?

There is actually one nifty way to do this - use the su command with combination of two options s and c.
# su -s /bin/bash -c 'touch /tmp/file' 
What actually will happen here is basically overriding configured shell with the one passed with "-s" option.

How to run a long operation with a spinner/hourglass indicator within eclipse plugin (swt application) ?

From time to time there is a need to run some relatively long operation synchronously. In such a case will be good to indicate the fact to the user. There is very simple way of doing this with BusyIndicator class. Just replace syncExec with BusyIndicator.showWhile.
Display.getCurrent().syncExec(new Runnable() {
    @Override
    public void run() {
           // long operation
    }
});
with the following:
BusyIndicator.showWhile(Display.getCurrent(), new Runnable() {
    @Override
    public void run() {
           // long operation
    }
});

How to invoke java method with a time limit?

Java has no handy way to do so. You can always use Thread to run a method and after some time out interrupt it.

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class Test {

 public static void main(String[] args) throws InterruptedException, ExecutionException, TimeoutException {

  ExecutorService executor = Executors.newSingleThreadExecutor();
  Future future = executor.submit(new Runnable() {
   @Override
   public void run() {
    try {
     TimeUnit.DAYS.sleep(1);
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
   }
  });

  try {
   future.get(5, TimeUnit.SECONDS);
  } catch (InterruptedException e) {
   future.cancel(true);
   throw e;
  } catch (TimeoutException e) {
   future.cancel(true);
   throw e;
  }
 }

}