Hot questions for Using Ubuntu in service

Question:

So after 2 days (yes I'm a complete rookie when it comes to servers) trying to get this working I give up and turn to SO for help :)

I want to start my java app on start, log to a logfile. That's it :)

start on runlevel [2345]
stop on runlevel [!2345]


#Respawn the process if it crashes
#If it respawns more than 10 times in 5 seconds stop
respawn
respawn limit 10 5

expect fork

script
    cd /home/ubuntu/admin/
    mvn spring-boot:run > /var/log/upstart/admin.log 2>&1
end script

Running "sudo start admin" works and I get "admin start/running" in console.. No log is created and the java app is not started.. ?

What am I missing?

How do I run Java as a service on Ubuntu?


Answer:

I don't mean to sidetrack, but I've deployed Java applications on Ubuntu in production since 2010 and had very little success with Upstart. I use init.d scripts and start-stop-daemon. Side bonus: it works on more distros.

Create /etc/init.d/my-java-app:

#!/bin/sh
#
# my-java-app My Java App
#
# chkconfig: - 80 05
# description: Enable My Java Application
#

### BEGIN INIT INFO
# Provides:          my-java-app
# Required-Start:    $remote_fs $network
# Required-Stop:     $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description:       My Java Application
# Short-Description: Enable My Java Application
### END INIT INFO

DESC="my java app"
NAME=my-java-app
PIDFILE=/var/run/$NAME.pid
RUN_AS=ubuntu
WORK_DIR=/home/ubuntu/admin
DAEMON=/usr/bin/mvn
DAEMON_OPTS="spring-boot:run"

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions

do_start() {
    start-stop-daemon --start --quiet --make-pidfile --pidfile $PIDFILE \
        --background \
        --chuid $RUN_AS \
        --chdir $WORK_DIR \
        --exec $DAEMON -- $DAEMON_OPTS
}

do_stop() {
    start-stop-daemon --stop --quiet --pidfile $PIDFILE
    if [ -e $PIDFILE ]
        then rm $PIDFILE
    fi
}

case "$1" in
    start)
    echo -n "Starting $DESC: $NAME"
    do_start
    echo "."
    ;;
    stop)
    echo -n "Stopping $DESC: $NAME"
    do_stop
    echo "."
    ;;
    restart)
    echo -n "Restarting $DESC: $NAME"
    do_stop
    sleep 1
    do_start
    echo "."
    ;;
    status)
    status_of_proc -p $PIDFILE "$DAEMON" "$NAME" && exit 0 || exit $?
    ;;
    *)
    echo "usage: $NAME {start|stop|restart}"
    exit 1
    ;;
esac

Make it belong to root, make it executable, and set it up to run on startup with:

sudo chown root:root /etc/init.d/my-java-app
sudo chmod 755 /etc/init.d/my-java-app
sudo update-rc.d my-java-app defaults

To start the service you can run:

sudo service my-java-app start

To stop the service you can run:

sudo service my-java-app stop

This is based on a simplified version of the /etc/init.d/skeleton file included by Ubuntu.

The man page for start-stop-daemon is worth looking at if you want to tweak this further.b

Question:

I have some doubts and I need some clarification on that, this is regarding the Kurento 6.0 server setup on AWS instance having ubuntu 14.04. 1: I have setup the Kurento successfully. 2: I also installed the turnserver. 3: I configured the turnserver as guided here. 4: I follow that link till here... ...know how many Media Pipelines do I need for my Application? After doing all setting I run this commands... cd kurento-tutorial-java/kurento-hello-world git checkout 6.2.1 mvn compile exec:java

After this I get my kurento is successfully running as putting log into end of the question. I am doing all this by connecting my instance remotely using WinScp. After this I try to access it on my windows machine in browser (Chrome/Mozilla), I try the urls: http://:8080 and https://:8443 But I found the result as server not found. On the same instance I have PHP Apache server I am able to get that on http://:80 Can any once tell me that what mistake I am doing here? Or any once can tell me is this possible to access kurento server into local machines browser, if yes than how? Thanks in advance.


Heading

Answer:

The only thing I can think of is to make sure that you've edited the AWS instance's security group to allow inbound traffic on the ports you're using.

Also, I've personally had minor difficulty pointing a browser at my AWS server using its public DNS, so try a url like this:

http://<your AWS instance's public IP>:<port>

Question:

I'm having openJDK 1.7 version and Have installed Cassandra 2.1.8. I have changed the CLASSPATH of CASSANDRA_HOME as well but I'm getting following error.

> root@ip-172-31-57-69:/usr/local/apache-cassandra/bin# export CASSANDRA_HOME=/usr/local/apache-cassandra/
root@ip-172-31-57-69:/usr/local/apache-cassandra/bin#  echo $CASSANDRA_HOME;
/usr/local/apache-cassandra/
root@ip-172-31-57-69:/usr/local/apache-cassandra/bin# cd /usr/local/apache-cassandra/
root@ip-172-31-57-69:/usr/local/apache-cassandra# bin/cassandra
root@ip-172-31-57-69:/usr/local/apache-cassandra# 
CompilerOracle: inline org/apache/cassandra/db/AbstractNativeCell.compareTo (Lorg/apache/cassandra/db/composites/Composite;)I
CompilerOracle: inline org/apache/cassandra/db/composites/AbstractSimpleCellNameType.compareUnsigned (Lorg/apache/cassandra/db/composites/Composite;Lorg/apache/cassandra/db/composites/Composite;)I
CompilerOracle: inline org/apache/cassandra/io/util/Memory.checkBounds (JJ)V
CompilerOracle: inline org/apache/cassandra/io/util/SafeMemory.checkBounds (JJ)V
CompilerOracle: inline org/apache/cassandra/utils/ByteBufferUtil.compare (Ljava/nio/ByteBuffer;[B)I
CompilerOracle: inline org/apache/cassandra/utils/ByteBufferUtil.compare ([BLjava/nio/ByteBuffer;)I
CompilerOracle: inline org/apache/cassandra/utils/ByteBufferUtil.compareUnsigned (Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;)I
CompilerOracle: inline org/apache/cassandra/utils/FastByteOperations$UnsafeOperations.compareTo (Ljava/lang/Object;JILjava/lang/Object;JI)I
CompilerOracle: inline org/apache/cassandra/utils/FastByteOperations$UnsafeOperations.compareTo (Ljava/lang/Object;JILjava/nio/ByteBuffer;)I
CompilerOracle: inline org/apache/cassandra/utils/FastByteOperations$UnsafeOperations.compareTo (Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;)I
Error: Could not find or load main class org.apache.cassandra.service.CassandraDaemon

Any help will be appreciated. I have been stuck in this for past 2 days.


Answer:

1: set java home env variable

2:set python path : C:\Python27 3: If other Python installed move this up.

Question:

actually I'm not able to run a Tomcat9-WebApp properly. I'd be thankful for any help pointing me in the right direction.


Setup:

OS: Ubuntu 18.04

App-Server: Tomcat 9.0.12 (also tested with 8.5.34)

Web-App: SpringBoot 1.5.7

JDK: 1.8.0_181

Script-Command for the startup: $CATALINA_HOME/bin/startup.sh

setenv.sh:

    JRE_HOME=path/to/jdk
    CATALINA_PID="$CATALINA_BASE/tomcat.pid"
    LOGGING_CONFIG="-Dlog4j.configurationFile=$CATALINA_BASE/conf/log4j2.xml"
    LOGGING_MANAGER="-Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager"

export CLASSPATH=$CLASSPATH:$CATALINA_HOME/lib/log4j-jul-2.11.1.jar:$CATALINA_HOME/lib/log4j-core-2.11.1.jar:$CATALINA_HOME/lib/log4j-api-2.11.1.jar:$CATALINA_HOME/lib/log4j-slf4j-impl-2.11.1.jar

If I'm running the script as a root-user it works properly and the tomcat is fully functional.

But if I'm running the same script as a service it hangs during deployment of the WAR. CPU-usage raises constantly to 100%

systemctl start tomcat

Trace-Logs make me thinking, Tomcat is scanning each directory of the filesystem for Configuration-classes (e.g.: /sys/devices/pci0000:00/0000:00:18.6/subsystem/)

o.a.c.s.ContextConfig: Scanning directory for class files with annotations [/sys/devices/pci0000:00/0000:00:18.6/subsystem ....

The Logfiles itselfs are bigger than 200MB. The upper line is just a sample.

As said before, any help would be greatly appreciated.


@Edit

Progress: I reseted the tomcat-logging to default. As soon as this is done. Everything works fine.


Best wishes

Mike


Answer:

Finally I got it working:

The issue depends on the export CLASSPATH-Command:

I don't know why, but there must not be the extension of the CLASSPATH.

Solution: Replace

export CLASSPATH=$CLASSPATH:$CATALINA_HOME/lib/log4j-jul-2.11.1.jar:$CATALINA_HOME/lib/log4j-core-2.11.1.jar:$CATALINA_HOME/lib/log4j-api-2.11.1.jar:$CATALINA_HOME/lib/log4j-slf4j-impl-2.11.1.jar

with

export CLASSPATH=$CATALINA_HOME/lib/log4j-jul-2.11.1.jar:$CATALINA_HOME/lib/log4j-core-2.11.1.jar:$CATALINA_HOME/lib/log4j-api-2.11.1.jar:$CATALINA_HOME/lib/log4j-slf4j-impl-2.11.1.jar

and the service is starting properly.

If anyone could give an explanation to this behavior. Me and I'll bet others too would love get deeper into this.


@Edit

Well I have a possible explanation for this behavior. As $CLASSPATH is not defined during startup the result is possible "". So the starting of this line seems to be ":/another/path..." It could interepreted like "/:/another/path...". The result is, that "/" is member of the classpath. That could force spring to scan the whole filesystem.

Important: This is only a guess and it is not verified.

Question:

As per the link I successfully be able to connect HSDB to running jvm process on Ubuntu by following steps below:

  1. (On Ubuntu Terminal) set SA_JAVA=/usr/lib/jvm/java-8-oracle/bin/java
  2. (On Ubuntu Terminal) echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
  3. (On Ubuntu Terminal) java -Dsun.jvm.hotspot.debugger.useProcDebugger=true -classpath /usr/lib/jvm/java-8-orcale/lib/sa-jdi.jar sun.jvm.hotspot.HSDB

For Windows 7 32 Bit and from the same link I tried to connect sun.jvm.hotspot.HSDB with running jvm process but I am facing following problems:

  1. (On command prompt) set PATH=C:\Program Files\Java\jdk1.8.0_40\bin;d:\windbg;%PATH% . But in the above command I am not being able to find d:\windbg. For this dependency I Google a lot and also installed Microsoft Windows Debugger but after installation it has different folder hierarchy (c:\WinDDK\7600.16385.1).
  2. For a while I skipped "windbg" and execute the following command. Which gives the following Screen. On the above screen I entered running jvm process id which I gathered from Windows Task Manager and clicked on OK button. The following screen appears and caused an exception. I know the link I followed is very old. How can I run sun.jvm.hotspot.HSDB on windows. Or is there any other tool that I should follow?

Answer:

The problem is, that you are using java.exe from your jre insteand of your jdk to start hdsb. That's why it's looking for sawindbg.dll in your jre\bin (where it isn't found) instead of your jdk\bin (where the dll is). You can see that in the LinkError error message.

So use:

C:\Program Files\Java\jdk1.8.0_40\bin\java.exe -cp C:\Program Files\Java\jdk1.8.0_40\lib\sa-jdi.jar sun.jvm.hotspot.HSDB

Or better change your path to always use the java.exe in your jdk. Other tools also have problem with the jre one as well.

Question:

What i wanted to do

I installed fresh linux Ubuntu 16.04 to setup a test developing environment.

I wanted to install

  1. java 8
  2. tomcat 8

So i applied this tutorial and know that i dont have any previously java or tomcat installed before.

Tutorial for java 8 and tomcat 8 Tutorial 2 same as the before with some more little hints

What is the problem

When i reached the step of starting tomcat , it failed. Failed to start apache tomcat web server tomcat.service: Control process exited, code=exited status=203.


Answer:

How i tried to reproduce:

I followed the tutorial on a fresh virtual box on ubuntu 16.04.

And after i tried lot of the solution like the below:

  1. Playing with chmod and permissions.
  2. Un install and re install
  3. Updating my ubuntu
  4. Restarting my ubuntu
  5. Reloading services
  6. Double Quotation suggestions in .service paths in (/etc/systemd/system/tomcat.service).
  7. Commenting CATALINA_HOME line in .service file in (/etc/systemd/system/tomcat.service).
  8. Other googling solutions.

Solution:

From a very small comments from here ( Thanks for the hint )) ) After i got the fail i ran

You should run ‘journalctl -xn’ for more details about why Tomcat failed to start. Thanks.

It gave me the main reason here is screen shot and notice in RED.

  • ALL PATHS WAS WRONG ==> So i had to fix about five places for the right path... check Number 2 in Orange check image .

  • JAVA_HOME PATH was wrong ==> so to change and find the correct one do this

Then change in tomcat.service check this image again this time look at 1 in bold yellow it should point to JDK.

Result:

So, apparently i had the path wrong , and it WORKED.

Summary:

When applying some tutorial keep an eye when you extract tomcat or etc.. because it may differ.

Question:

I have a Spring Boot app that I want to start using System V Init. My jar file is in /var/my-app/my-app-0.0.1-SNAPSHOT.jar. So, I created a symbolic link using the command below

sudo ln -s /var/my-app/my-app-0.0.1-SNAPSHOT.jar /etc/init.d/my-app-0.0.1-SNAPSHOT

without any issue. However, when I try

sudo service my-app-0.0.1-SNAPSHOT start

I get the error:

/etc/init.d/my-app-0.0.1-SNAPSHOT: 1: /etc/init.d/myapp-0.0.1-SNAPSHOT: Syntax error: Unterminated quoted string

Does anyone have an idea what I'm doing wrong? Thanks.


Answer:

What does a symbolic link have to do with the service? Nothing :)

You will first have to define the Upstart script (in case of Ubuntu 14.04 ), it is in /etc/init/ and ending in .conf. (http://upstart.ubuntu.com/cookbook/)

# <service name>.conf
start on filesystem
exec /usr/bin/java -jar /path_to/program

in case of Ubuntu 16.06+ (you can find nice article here)

sudo vim /etc/systemd/system/my-webapp.service

Then you can run it as:

sudo service <your service name> start

Hope that helps.

Question:

I have a springboot web application packaged as a jar file. And the product server is Ubuntu 14.04.1 with openjdk-8-jdk installed.

I can successfully run the application using:

sudo java -jar /home/myUser/my_web_app.jar

Then I make this appliaction as a SystemV service by following commands:

sudo useradd webUser
sudo passwd webUser
sudo chown webUser:webUser /home/myUser/my_web_app.jar
sudo chmod 500 /home/myUser/my_web_app.jar
sudo ln -s /home/myUser/my_web_app.jar /etc/init.d/my_web_app

After that, when I run:

sudo service my_web_app start

And error raise as:

/etc/init.d/my_web_app: 1: /etc/init.d/my_web_app: Syntax error: ")" unexpected.

I wonder what cause this error and how to reslove it. Thanks.

BTW, Same method works on my devlopment machine (Ubuntu 14.04.6).


Answer:

The problem is these two lines:

sudo chmod 500 /home/myUser/my_web_app.jar
sudo ln -s /home/myUser/my_web_app.jar /etc/init.d/my_web_app

Problem #1: regular executable JAR files are not an executable format that the OS recognizes1.

Setting the execute bit on a regular JAR file won't help. The OS kernel does not know how to run it. To run a regular JAR, you have to execute the command java -jar /path/to/the.jar. If necessary, you can create a simple wrapper script to do this.

1 - There is a way to generate a special "fully executable" SpringBoot JAR file which has an shell script prepended to it; see "Installing Spring Boot Applications". This is one way to get around this problem, though the documentation states that these special JAR files cause problems for some tools.

Problem #2: The files in /etc/init.d are supposed to be service scripts.

They are not simply the executable for a service. The scripts are supposed to be shell scripts that understand verbs such as start, stop, restart, reload and so on. And (AFAIK) they must be coded as sh compatible shell scripts. Here is an article that describes the structure of service scripts:

(But read Problem #4 first!!)

Problem #3: Running a service as root can be a security risk.

It is better to create a (non-privileged) service account to run the service. This is especially important if the service is exposed to the network. (If the bad guys can "hack" the service over the network and cause it to do undesirable things, the fact that it is running as root places the entire system at risk.)

Problem #4: /etc/init.d/ scripts are obsolete.

If you are using a recent Ubuntu release (15.04 or later), those /etc/init.d/ scripts are the "legacy" way of configuring. The current iteration of the initd mechanism is systemd. It uses systemd unit files files rather than service scripts. The following article gives more information:

The systemd service includes for legacy service scripts, but they are not as powerful, flexible and ... concise ... as unit files.

Problem #5: Ubuntu 14.04 LTS has reached end-of-life.

You should upgrade to 16.04 LTS or preferably 18.04 LTS. It is unwise to use an end-of-life operating system for your production server(s).


Note that there is a lot of documentation, and many articles on the web about both the old and new ways of configuring services. ( Google is your friend. )

Question:

I have 2 questions I am trying to run a axis 2 sample. In the last part of the instruction file, there is this line which it says, should be run in the terminal(ubuntu), is not working

java -Djava.ext.dirs=%AXIS2_HOME%\lib;%JAVA_HOME%\jre\lib\ext -cp target/classes org.apache.axis2.jaxws.addressbook.AddressBookClient.class

I am not an expert in this field, and I am not familiar with ubuntu commands. I feel that this is not an ubuntu command The error I get is, "Invalid Job"

  1. Can someone convert this into an ubuntu command?

Since it was not working, I built the jar using,

mvn clean install

Then I copied the jar file to the servicejars directory under repository, in axis2

Then the axis server says that the jar does not contain the WebServices annotation

"No @WebService annotated service implementations found in the jar: file:/home/dodan/Programs/axis2-1.6.0/repository/servicejars/jaxws-addressbook-1.6.0-client.jar. Service deployment failed."

So I added it to the original java file which did not have that annotation(and the import too)

Yet the axis2 server still sys that there is not webservices annotation 2. Can somebody say whether I have missed anything?

here is the java file I changed

import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import javax.jws.WebService;
import java.util.Map;

/**
 * Simple JAX-WS Dispatch client for the address book service implementation.
 */
@WebService
public class AddressBookClient {
    private static String NAMESPACE = "http://addressbook.jaxws.axis2.apache.org";
    private static QName QNAME_SERVICE = new QName(NAMESPACE, "service");
    private static QName QNAME_PORT = new QName(NAMESPACE, "port");
    private static String ENDPOINT_URL = "http://localhost:8080/axis2/services/AddressBookImplService.AddressBookImplPort";

private static String ADD_ENTRY_BODY_CONTENTS = 
    "<ns1:addEntry xmlns:ns1=\"http://addressbook.jaxws.axis2.apache.org\">" + 
      "<ns1:firstName xmlns=\"http://addressbook.jaxws.axis2.apache.org\">myFirstName</ns1:firstName>" + 
      "<ns1:lastName xmlns=\"http://addressbook.jaxws.axis2.apache.org\">myLastName</ns1:lastName>" + 
      "<ns1:phone xmlns=\"http://addressbook.jaxws.axis2.apache.org\">myPhone</ns1:phone>" + 
      "<ns1:street xmlns=\"http://addressbook.jaxws.axis2.apache.org\">myStreet</ns1:street>" + 
      "<ns1:city xmlns=\"http://addressbook.jaxws.axis2.apache.org\">myCity</ns1:city>" + 
      "<ns1:state xmlns=\"http://addressbook.jaxws.axis2.apache.org\">myState</ns1:state>" + 
    "</ns1:addEntry>";

private static String FIND_BODY_CONTENTS = 
    "<ns1:findByLastName xmlns:ns1=\"http://addressbook.jaxws.axis2.apache.org\">" +
      "<ns1:lastName xmlns=\"http://addressbook.jaxws.axis2.apache.org\">myLastName</ns1:lastName>" +        
    "</ns1:findByLastName>";

public static void main(String[] args) {
    try {
        System.out.println("AddressBookClient ...");

        Service svc = Service.create(QNAME_SERVICE);
        svc.addPort(QNAME_PORT, null, ENDPOINT_URL);

        // A Dispatch<String> client sends the request and receives the response as 
        // Strings.  Since it is PAYLOAD mode, the client will provide the SOAP body to be 
        // sent; the SOAP envelope and any required SOAP headers will be added by JAX-WS.
        Dispatch<String> dispatch = svc.createDispatch(QNAME_PORT, 
                String.class, Service.Mode.PAYLOAD);

        // Invoke the Dispatch
        System.out.println(">> Invoking sync Dispatch for AddEntry");
        String response = dispatch.invoke(ADD_ENTRY_BODY_CONTENTS);
        System.out.println("Add Entry response: " + response);

        System.out.println(">> Invoking Dispatch for findByLastName");
        String response2 = dispatch.invoke(FIND_BODY_CONTENTS);
        System.out.println("Find response: " + response2);
    } catch (Exception e) {
        System.out.println("Caught exception: " + e);
        e.printStackTrace();
    }
}
}

Answer:

You're using Windows syntax for environment variables.

Instead of %AXIS_HOME%, you would use $AXIS_HOME.

That said, you do not need any version of Axis to learn about web services these days. JAX-WS implementations exist in the JDK for Java 6 and newer.

There's a lot of tutorials around for it.

Question:

As an introduction, let's just say I'm a real noob using linux. I try to do the things right, don't hit me if it's ugly.

So, the problematic. I'm trying to run some jars as webservices on an ubuntu server. I created a specific user (nuxservice) with no pwd. I edited sudoers to enable a few users (myself & root) to sudo using this account with no password.

I then took a lot of inspiration from : http://www.jcgonzalez.com/linux-java-service-wrapper-example

Most is working, only one real problem, my java process seems to not have the right to create its logging files.

I run my services with a classic

sudo service myservice start/stop/restart

The command line that are launching my services are :

nohup sudo -u nuxservice java -jar myjar.jar myargs

When I do a ps -ef, the services are launches with my nuxservice user.

When I do ls -ld, nuxservice is the owner and have the rights.

If I launch the command in a terminal myself, it works. When launching as a service, my logs files are not created.

Any clues mates ?


Answer:

So...

It was kinda silly.

My user rights were fine. Problem is, I did not set the working folder in my script so Java was all lost considering the creation of the folder/files for logging.

All I had to do was adding a little

cd $PATH_TO_JAR

And it was all set !

Question:

when i am trying to run a java services through php file(xyz), it is throwing this error (Firebug) NetworkError: 500 Internal Server Error http://127.0.0.1/dev/en/abc/xyz

/var/log/apache2/error.log is showing PHP Fatal error: Uncaught [[o:Exception]:"java.lang.Exception: ReferenceClass failed: new com.intelligent.abc.main.RunABCHandler. Cause: java.lang.ClassNotFoundException:

While other java services are responding perfectly fine.

Can i get suggestion, so that i can get rid from this.


Answer:

You need to place, one more instance of services onto the given paths into the properties file: like /usr/lib/jvm/java-7-oracle/jre/lib/ext

Surprisingly it was throwing the error, because of due path.