Run exe outside of gradle scope - gradle

I am trying to kick of an exe (mongodb) from gradle, but need that exe to run outside of gradle scope so that the gradle task is not blocked for ever.
task startMongo(type: Exec) {
executable "$buildDir/mongo/mongod.exe"
args "--dbpath=$buildDir/mongo/data/db"
}
Mongodb starts fine, but the task is blocked as the mongo server waits for connections.
2014-12-10T14:30:33.018-0700 [initandlisten] waiting for connections on port 27017

Robert - thank you. I wrote a custom gradle task and started the exe in background.
class MyTask extends DefaultTask {
#TaskAction
void startProcess() {
ProcessBuilder processBuilder = new ProcessBuilder()
processBuilder.command('exe-path', 'arg')
processBuilder.start()
}

Related

Gradle HttpFileServer, keep running, but kill on ctrl+c

During my Gradle build I need to run a simple web server to serve some static content. I’m using Gradle’s integrated classes for that.
Here's a very simple version of my build.gradle:
apply plugin: 'groovy'
task server() {
doLast {
def root = new File(project.buildDir, '/site')
def port = 8765
def factory = new SimpleHttpFileServerFactory();
def server = factory.start(root, port)
println "HTTP server started on $port"
}
}
I'm facing the following two issues:
When running gradle server, gradle executes, starts the server, and then quits (with the daemon obviously staying in the background and the server running, which leads to the second problem)
I’ve no possibility to stop the server through gradle. When running gradle server again, I get an exception because the port is in use.
I’d fancy either of the following solutions:
When running gradle server, gradle keeps running until I hit ctrl+c, then the server is killed as well
There are two tasks, gradle startServer and gradle stopServer which are used to start and stop
How can I achieve this or is there a different, better solution?
A little hack-ish but it works, the server task will hang and the SIGKILL will kill the server too
apply plugin: 'groovy'
task server() {
doLast {
def root = new File(project.buildDir, '/site')
def port = 8765
def factory = new SimpleHttpFileServerFactory();
def server = factory.start(root, port)
println "HTTP server started on $port"
while(true) Thread.sleep(1000)
}
}

Cannot launch GUI programs from systemd service

I've a Spring boot app, i execute it through systemd service in Ubuntu, after starting the X server with sudo startkde, i cannot launch GUI programs from the app using command line like gedit in the meantime it works when i launch the app using sudo java -jar demo.jar, i've tried putting gedit commande inside a shell script but the problem persists.
is there any solution to use the service and launch GUI programs, or launch the spring boot with another kind services that could solve the the problem.
here's the systemd service :
[Unit]
Description=demo
After=syslog.target
[Service]
User=ubuntu
ExecStart=/home/ubuntu/demo.jar --logging.file=logfile.log
SuccessExitStatus=143
[Install]
WantedBy=multi-user.target
here's the spring boot code :
#RestController
#EnableAutoConfiguration
#SpringBootApplication
public class DemoApplication {
#RequestMapping("/")
String home() {
ProcessBuilder builder = new ProcessBuilder("gedit");
builder.redirectErrorStream(true);
try {
final Process process = builder.start();
try {
process.waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return "Hello World!";
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
systemd isn't a good fit for auto-starting GUI apps directly. As #jaysee explained, it's not connected to particularly GUI.
What systemd can do is start a window-manager, the window manager can be set to automatically log in a particular user, and that user can use the window manager's "autostart" feature to launch the GUI app.
I went down the same path myself trying to use exclusively systemd and the other route is what I found to work.
This is a common use-case for Raspberry Pis. So if you search for tutorials about [Raspberry PI autostart kiosk], you should find a number of options (whether you are using a Raspberry Pi or not). The newer Raspberry Pis use a newer systemd-based version of Debian, so in practice it's very similar to what you want to do with Ubuntu 16.04.

Running Play service from Gradle

I am attempting to use a Gradle task to run a Play service, but I'm finding the Gradle task will hang (presumably waiting for a return value from the Play bootstrap script).
What I'm doing from the Play side is simply:
sbt dist
Which produces a .zip distribution (like 'myproject.zip'), which I then expand where I want to run this service from.
On the Gradle side, I was thinking I would do something like this:
task start(type: Exec) {
workingDir "myproject/bin"
commandLine './myproject'
}
This does indeed start up the Play service just fine, but the Gradle task will hang indefinitely (until you do a control+C).
The most obvious thing that came to mind to try was something like:
task start(type: Exec) {
workingDir "myproject/bin"
commandLine 'nohup ./myproject &'
}
But that ends in a dead end:
Execution failed for task ':start'.
> A problem occurred starting process 'command 'nohup ./playservicetemplate &''
It seems like this is a really common use case, so I'm wondering if there is an obvious solution that I'm overlooking.
There is perhaps a more Gradle-ish way to do this, but I solved it by leveraging ProcessBuilder. My new task looks like:
task start {
ProcessBuilder builder = new ProcessBuilder("./myproject")
builder.directory(new File("myproject/bin"))
builder.start()
}
Obviously, you can get a lot fancier with this (a quick Google of 'java processbuilder' will net you pages upon pages of examples), but this will do the trick for my purposes.

Cakephp 2 use task in another task

In a relatively big shell I am using a few tasks, however it appeared a necessity to use some functions of one task (lets call it a main task) in another tasks as well.
So, how I can use a task in another task. Cakephp 2.x
Thanks
Use the Shell::$tasks property to define the additional tasks that your task should load, or load them manually using TaskCollection::load(), avaiable via the Shell::$Tasks property.
The additional tasks can be accessed via magic properties using the task names.
class SubTask extends AppShell
{
public $tasks = array(
'Main'
);
// ...
public function subMethod()
{
$this->Main->mainMethod();
$this->Tasks->load('Other');
$this->Other->otherMethod();
}
// ...
}
See also
Cookbook > Shells, Tasks & Console Tools > Shell tasks
Cookbook > Shells, Tasks & Console Tools > Shell API > Shell::$tasks
API > Shell::$Tasks
API > TaskCollection.html::load()

Running two Gradle application tasks concurrently in same project

It looks like when I try to run a second Gradle task for the same project in a different window, the second one blocks on a lock. Is there a straightforward way around this?
What I'm trying to do: My project has a couple server subprojects that both use the application plugin. I'd like to start both (e.g., $ gradle :server1:run) so I could connect and try them out.
I know I can write a task to deploy the two servers to a test area and start them there, but the application:run task is convenient during development for one application, so I'd like to use it for two if possible.
I'm using Gradle 2.7.
Here is what I ended up doing. Rather than using the application plugin's run task, I used installDist, and wrote a simple task to run the generated start script. Then I extended it by creating a simple service script that I stored under src/dist/bin. The script handles start, stop, and status operations. The final result:
ext.installDir = "$buildDir/install/" + project.name
ext.command = "$installDir/bin/" + project.name
task start(type:Exec, dependsOn:'installDist') {
description "Starts the " + applicationName + " application"
workingDir "$installDir"
commandLine "$command", "start"
}
task stop(type:Exec, dependsOn:'installDist') {
description "Stops the " + applicationName + " application"
workingDir "$installDir"
commandLine "$command", "stop"
}
task status(type:Exec, dependsOn:'installDist') {
description "Displays the " + applicationName + " application status"
workingDir "$installDir"
commandLine "$command", "status"
}
Now from the parent project I can just type:
$ gradlew start
to start both servers and
$ gradlew stop
to shut them both down.

Resources