Hot questions for Using Ubuntu in bash

Question:

I need that my code can get the environment variable when running test with IntelliJ:

private String sdkDir = System.getProperty("sdk.root");

The problem is that this is always null, I have set both in bashrc and also with bash_profile but not really works with Ubuntu.

bashrc and bash_profile both have this:

export JAVA_HOME=/home/xybrek/zulu1.8.0_25-8.4.0.1-x86lx64
export JAVA_OPTIONS="-Dsdk.root=/home/xybrek/java-sdk-1.9.17"
export PATH=$PATH:/home/xybrek/zulu1.8.0_25-8.4.0.1-x86lx64/bin
export PATH

What could be missing with my configuration?

Update:

I cannot change the Java code: System.getProperty("sdk.root"); as this is coming from a compiled Arquillian container I can't modify.

What I need would be a proper way of having this System.getProperty method to get the value.

Where to put this "sdk.root" for IntelliJ to pick it up? In a properties file or bash?


Answer:

You have to use

String val = System.getenv( "PATH" );

for the value of an environment variable. A property value has nothing to do with the Environment of a process.

String val = System.getProperty( "sdk.root");

is for properties.

There should be a way of setting command line arguments in the dialog for preparing the execution of a Java program in your IDE. This is where you should define -Dsdk.root=....

Question:

I have a shell script which I'm trying to call from Java. The shell script contains:

cat /dev/tty.USB0 > file.txt

In my Java code I am using:

Process p= Runtime.getRuntime().exec("/home/myname/Scrivania/capture.sh");

But it does not work. When I run it from the terminal it works as expected.


Answer:

You can't directly execute a .sh script like this, since it's not an executable. Instead, you have to run /bin/sh -c /home/myname/Scrivania/capture.sh instead.

Question:

I've followed each and every step for installing Hadoop from this website https://www.digitalocean.com/community/tutorials/how-to-install-hadoop-on-ubuntu-13-10

All the steps go fine except the last step where I start Hadoop using start-dfs.sh and lot of errors come. Please help. The errors are :

15/04/07 00:08:16 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Starting namenodes on [localhost]

localhost: mkdir: cannot create directory ‘/usr/local/hadoop/logs’: Permission denied

localhost: chown: cannot access ‘/usr/local/hadoop/logs’: No such file or directory

localhost: starting namenode, logging to /usr/local/hadoop/logs/hadoop-tushar-namenode-tushar-VirtualBox.out

localhost: /usr/local/hadoop/sbin/hadoop-daemon.sh: line 159: /usr/local/hadoop/logs/hadoop-tushar-namenode-tushar-VirtualBox.out: No such file or directory

localhost: head: cannot open ‘/usr/local/hadoop/logs/hadoop-tushar-namenode-tushar-VirtualBox.out’ for reading: No such file or directory

localhost: /usr/local/hadoop/sbin/hadoop-daemon.sh: line 177: /usr/local/hadoop/logs/hadoop-tushar-namenode-tushar-VirtualBox.out: No such file or directory

localhost: /usr/local/hadoop/sbin/hadoop-daemon.sh: line 178: /usr/local/hadoop/logs/hadoop-tushar-namenode-tushar-VirtualBox.out: No such file or directory

localhost: mkdir: cannot create directory ‘/usr/local/hadoop/logs’: Permission denied

localhost: chown: cannot access ‘/usr/local/hadoop/logs’: No such file or directory

localhost: starting datanode, logging to /usr/local/hadoop/logs/hadoop-tushar-datanode-tushar-VirtualBox.out

localhost: /usr/local/hadoop/sbin/hadoop-daemon.sh: line 159: /usr/local/hadoop/logs/hadoop-tushar-datanode-tushar-VirtualBox.out: No such file or directory

localhost: head: cannot open ‘/usr/local/hadoop/logs/hadoop-tushar-datanode-tushar-VirtualBox.out’ for reading: No such file or directory

localhost: /usr/local/hadoop/sbin/hadoop-daemon.sh: line 177: /usr/local/hadoop/logs/hadoop-tushar-datanode-tushar-VirtualBox.out: No such file or directory

localhost: /usr/local/hadoop/sbin/hadoop-daemon.sh: line 178: /usr/local/hadoop/logs/hadoop-tushar-datanode-tushar-VirtualBox.out: No such file or directory

Starting secondary namenodes [0.0.0.0]

0.0.0.0: mkdir: cannot create directory ‘/usr/local/hadoop/logs’: Permission denied

0.0.0.0: chown: cannot access ‘/usr/local/hadoop/logs’: No such file or directory

0.0.0.0: starting secondarynamenode, logging to /usr/local/hadoop/logs/hadoop-tushar-secondarynamenode-tushar-VirtualBox.out

0.0.0.0: /usr/local/hadoop/sbin/hadoop-daemon.sh: line 159: /usr/local/hadoop/logs/hadoop-tushar-secondarynamenode-tushar-VirtualBox.out: No such file or directory

0.0.0.0: head: cannot open ‘/usr/local/hadoop/logs/hadoop-tushar-secondarynamenode-tushar-VirtualBox.out’ for reading: No such file or directory

0.0.0.0: /usr/local/hadoop/sbin/hadoop-daemon.sh: line 177: /usr/local/hadoop/logs/hadoop-tushar-secondarynamenode-tushar-VirtualBox.out: No such file or directory

0.0.0.0: /usr/local/hadoop/sbin/hadoop-daemon.sh: line 178: /usr/local/hadoop/logs/hadoop-tushar-secondarynamenode-tushar-VirtualBox.out: No such file or directory
15/04/07 00:08:34 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable

Answer:

Your user account doesn't have the correct permissions to the /usr/local/hadoop directory, so Hadoop is failing when trying to create its logs. Try running sudo chown -R <username> /usr/local/hadoopwhere is your username. To get your username, you can use the whoami command.

Question:

I am trying to write a simple script to compile my java code. When I put

javac Main.java

in my compile.sh and do

bash compile.sh

I get error: invalid flag: Main.java

However, if I just simply use the command javac Main.java , everything works fine.

I am using a Ubuntu VirtualBox on Win10. How can I get my script working?


Answer:

Here's the problem you're seeing:

$ cat compile.sh
javac Main.java

$ bash compile.sh
javac: invalid flag: Main.java
Usage: javac <options> <source files>
use -help for a list of possible options

This happens because the script uses DOS style line endings:

$ cat -v compile.sh
javac Main.java^M

You can fix it by setting your editor to save it with Unix line terminators, or with e.g. tr:

$ tr -d '\r' < compile.sh > fixed.sh
(no output)

$ cat -v fixed.sh
javac Main.java

It now works as expected:

$ bash fixed.sh
(no output)

Question:

So I'd like to have a process running on my Linux computer and have the output from the process taken by line as it's sent, for example (I just made this up):

A process constantly running outputting a random number every five second

52
72
33
67
26
74
2

I would like to get the number when it is printed to the console.

Currently, I have some code that could work for executing a command, though I'm not sure if that even works:

public OutputStream executeCommand(String command) {
    try {
        ProcessBuilder pb = new ProcessBuilder(command);    
        Process p = pb.start();
        OutputStream ops = p.getOutputStream();
        return ops;
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

I'm pretty much just asking how I could get some sort of output and read it as it is printed.


Answer:

Be aware that the code below can throw an IOException.

Runtime runtime = Runtime.getRuntime() ;
            Process process = runtime.exec("Your command") ;
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())) ;
            String line = null ;
            while ((line = reader.readLine())!= null){
                System.out.println(line);
            }

Question:

I have a script to run java applications on ubuntu

nohup java -Xms512m -Xmx8g -jar /data/software/190716/myapp-0.0.1.jar --server.port=8080 --spring.config.location=/data/software/myapp.properties > ~/running.myapp.log &

can I make a .sh script to make the command start, stop and restart the process? thanks


Answer:

As mentioned in the comment, the better way to do that follow running-a-java-process-from-systemd

But if you are really interested in a bash script, then you can do something like this, but keep in mind there is might be a better approach for that.

#!/bin/bash

function start_app() {

    # tested with this , uncomment your command
    nohup node bin/www > logfile.txt  2>&1 & 
    # nohup java -Xms512m -Xmx8g -jar /data/software/190716/myapp-0.0.1.jar --server.port=8080 --spring.config.location=/data/software/myapp.properties
    # write the pid to text to file to use it later
    app_pid=$!
    echo "Process started having PID $app_pid"
    # wait for process to check proper state, you can change this time accordingly 
    sleep 3
    if ps -p $app_pid > /dev/null
    then
        echo "Process successfully running having PID $app_pid"
        # write if success
        echo $app_pid > process_id.txt
    else
        echo "Process stopped before reached to steady state"
    fi
}

function stop_app() {
    # Get the PID from text file
    application_pid=$(cat process_id.txt)
    echo "stopping process, Details:"
    # print details
    ps -p $application_pid
    # check if running
    if ps -p $application_pid > /dev/null
    then
        # if running then kill else print message
        echo "Going to stop process having PId $application_pid"
        kill -9 $application_pid
        if [ $? -eq 0 ]; then
        echo "Process stopped successfully"
        else
        echo "Failed to stop process having PID $application_pid"
        fi
    else
        echo "Failed to stop process, Process is not running"
    fi
}


case "$1" in 
    start_app)   start_app ;;
    stop_app)    stop_app ;;
    restart) stop_app; start_app ;;
    *) echo "usage: $0 start_app|stop_app|restart_app" >&2
       exit 1
       ;;
esac