GCE Automation Script Not Working to run Spring Boot Application - spring-boot

I created an instance in Google Cloud Compute Engine (Debian OS) to host a Spring Boot Maven Application. I installed maven.Now while configuring the instance I added below script in the automation startup box -
cd spring-boot-app/
mvn clean package
cd target/
nohup java -jar artifact-1.0.jar &
I used nohup and & to run the application in background.
Now when I stop then start/resume the instance and open terminal through SSH & run the following command -
ps ax | grep java
I don't see my app running. What I am doing wrong here ?

When monitoring the logs, I found this -
cd spring-boot-app/ directory does not exists
So I realized that the script is being executed in the root directory. And when I connected to the terminal using SSH in Google Cloud Compute Engine VM I was redirected to home directory of my username.
Thus by updating the script I was able to run the startup script.
cd home/{username}/
cd spring-boot-app/
mvn clean package
cd target/
nohup java -jar artifact-1.0.jar &

Related

How to run java application automatically when EC2 instance starts without having to run it manually with java -jar file_name-SNAPSHOT.jar?

I tried the following steps but when I access my application in the browser using the url link, it returns a blank page. Here is what I did:
Copy My jar file to /home/ec2-user/
cp myApp-0.0.1-SNAPSHOT.jar /home/ec2-user
Next, I opened the rc.local in the VI editor and appended myApp-0.0.1-SNAPSHOT.jar
vi /etc/rc.local
#
#
#
#
touch /var/lock/subsys/local
//Append the below command:
java -jar /home/ec2-user/myapp-0.0.1-SNAPSHOT.jar
/:wq
Give permission to the symlink by running the following command:
chmod +X /etc/rc.d/rc.local
So I stopped my ec2 instance and after a while, I started it again to see if I was able to test my endpoints without having to manually run my appl with java -jar myApp-0.0.1-SNAPSHOT.jar But it did not succeed. I was told this way of doing is now obsolete. I also tried to follow the following link but the file naming was not obvious to me so I messed up some steps and got to the same result: failed Please can anyone direct me? Thanks! I tried the following steps but when I access my application in the browser using the url link, it returns blank page.

How do I run execute commands from a git repo from Python? Shell + Docker

My goal is to run a docker container that will simulate a physical device that sends requests to an external API.
The simulator lives in a GitHub repo and after cloning it is run by bash scripts that:
Set up a virtualenv.
Run maven build.
Execute other commands via Python that runs a docker container.
It can also remove docker container.
How can I simplify these dependencies and make sure everything is packed in a working container (application that otherwise would be run through shell scripts)?

Cannot pull a repository (from Cloud Source Repositories) in a GCE VM startup script

I have a VM in Google Compute Engine that I want to start and stop daily - I have already written Cloud Functions for this. When the VM starts, I want it to run a startup script. In the bash startup script, I first need to pull data from git - Cloud Source Repository. This causes it to crash:
Error: Permission denied (publickey)
The startup script looks like this:
#!/bin/bash
cd /home/my_home_directory/git_repo
git pull;
cd some_directory_in_repo
python3 some_script.py;
shutdown -h now;
The VM has its own service account, which, as far as I know, runs the script. What I basically want, is to run the script with a "user" - service account - that does not have a home directory on the VM (the service account has the necessary permissions for accessing the repository, though). I also set up SSH key for the service account, then I registered the public key on my user profile and this works when I execute the script under my user.
Is there a solution for this, other than run the script under my user (which works, as I said), please?
Note: If I execute the startup script like the one below, it also works.
#!/bin/bash
cd /home/my_home_directory/git_repo
sudo -u my_username bash -c \
'git pull;
cd some_directory_in_repo
python3 some_script.py;
shutdown -h now;'
Thanks

Installing and starting the H2 database on Ubuntu

I would like to use the H2 database on Ubuntu 12.10, and went to the website and got the platform independent install file.
The installation instructions are quite literally, "To install the software, run the installer or unzip it to a directory of your choice."
I'm not a Linux novice, so I've used many of the usual install procedures before, but I have no idea what I am supposed to do here. There are no configure or makefiles that I can find, and the documentation doesn't mention anything, and there I can't find anything using google.
I don't know if I am missing something obvious. Can anybody help please?
A shell script to start the H2 server and browser GUI is included. I don't have Ubuntu right now, but the steps should be:
Download the H2 zip file (for example h2-2013-07-28.zip).
Open a terminal window
And then run:
cd <download directory>
unzip h2*.zip
cd h2/bin
chmod +x h2.sh
./h2.sh
This should start the H2 server tool and open a browser window that lets you connect to a database.
The content of the h2.sh script is relatively simple, it is:
#!/bin/sh
dir=$(dirname "$0")
java -cp "$dir/h2-1.3.173.jar:$H2DRIVERS:$CLASSPATH" org.h2.tools.Console "$#"
What you can also do is double click the h2*.jar file (if double click is configured to start java), or run this on a command line:
java -jar h2-1.3.173.jar
this bash script start the server:
#!/bin/bash
java -cp h2*.jar org.h2.tools.Server
you need the h2-version.jar in the current directory as well
Download h2 from here http://www.h2database.com/h2-2015-01-16.zip, then once the download has completed, unzip in home directory and run the following:
cd (directory path ).
cd h2/bin.
chmod +x h2.sh.
./h2.sh.
If you're using Maven to download h2 database, then try to find out where does it saves (~/.m2/repository/com/h2database/h2/.../h2-version.jar). Since h2 dependency has all downloaded for you, then you just doubleclick on jar file (the web browser should open h2 GUI or look for h2 icon on system tray). And yes, GUI is the same as Standalone package has.
Update: as long as icon displays on tray - h2 will be launched. To disable it - right mouse click -> Exit

Is it possible to force the maven plugin for jenkins to run as root?

I have a particularly involved java app that needs root access to system resources duing a build for running file mounts. Is there a way to directly invoke maven using "sudo" from jenkins via the maven2/3 plugin? Or does the plugin always run as jenkins.?
Here is how to run Jenkins as root - this will cause the maven plugin processes to also run as root.
Method 1) Modify the following line in JENKINS_USER in /etc/sysconfig/jenkins
#JENKINS_USER=jenkins
JENKINS_USER=root
In Debian-based systems, the file is located at /etc/default/jenkins
Method 2) Directly modify /etc/init.d/jenkins
#daemon --user "$JENKINS_USER" --pidfile "$JENKINS_PID_FILE" $JAVA_CMD $PARAMS > /dev/null
echo "WARNING: RUNNING AS ROOT"
daemon --user root --pidfile "$JENKINS_PID_FILE" $JAVA_CMD $PARAMS > /dev/null
Then, of course, you must run:
service jenkins restart
Try running the jenkins process as root (although not ideal security-wise), it should spawn the maven process as the same user.
When you run maven through Jenkins maven plugin, its executed in jenkins`s process. Running server is a root is a bad idea. You could try running plugin as shell command:
sudo mvn org.apache.maven.plugins:maven-dependency-plugin:2.4:get -DartifactId=...
see also this:
https://superuser.com/questions/67765/sudo-with-password-in-one-command-line
My suggestion for running root things with jenkins is to compile on jenkins a binary and giving it suid bit, so that it can be launched by the jenkins user but executed as root. For instance, I write a C file:
#include <stdio.h>
#include <stdlib.h>
int main(){
system("whoami");
}
compile it (as root)
# gcc -c iamroot.c
# gcc -o iamroot iamroot.o
and give it suid bit
# chmod u+s iamroot
Then you obtain (as any other user)
$ ./iamroot
root
Now this can be run by the jenkins user, and state it's root. In term of security, it's way better than giving jenkins user root or sudo rights.

Resources