Backgrounding, Java & init script - java

I am trying to write an init script for a java program. The program has no ability to background itself, so I'm using & to do it. It writes its logfiles to stdout.
The problem I have is regarding the RETVAL in the init script. Here's the function I'm using:
JAR_FILE=/opt/application/server/server.jar
JAVA=/usr/bin/java
start() {
echo -n $"Starting ${NAME}: "
daemon --pidfile=${PID_FILE} --user $USER \
$JAVA $JAVA_ARGS >> /var/log/application/server.log &
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch $LOCKFILE
return $RETVAL
}
When I run the init script, the app starts up or fails nicely, the problem is that the retval is being written to the logfile, rather than to stdout:
[root#rpmbuild rpmbuild]# /etc/init.d/application-server start
Starting application-server:
[root#host]# tail /var/log/application/server.log
JAVA APPLICATION ERRORS ARE HERE
[FAILED]
Is there anyway I can make it log to stdout and also ensure that any retval (whether it good or bad) gets echoed to stdout?

There are options on how to direct the output when using the daemon command
-l, --errlog=spec - Send daemon's error output to syslog or file
-b, --dbglog=spec - Send daemon's debug output to syslog or file
-o, --output=spec - Send client's output to syslog or file
-O, --stdout=spec - Send client's stdout to syslog or file
-E, --stderr=spec - Send client's stderr to syslog or file
I would change the output using the -o option and not redirect. You should not have to background the task either
daemon -o /var/log/application/server.log -name SuperJavaProgram \
--pidfile=${PID_FILE} --user $USER $JAVA $JAVA_ARGS
Then you could test if it's running with daemon --running -n SuperJavaProgram

Maybe, you could print to stderr in java, to separate standard logs and errors. Your >> will redirect only stdout.
BTW, it's surprising to test $? whereas your program as not executed yet, because background-ed.

Related

"-Dapp.pid=%%" passes in an incorrect pid to java jvm arguments in start script

In the start script for my application, the service is started with the following lines:
JVM_OPTS=$DEFAULT_JVM_OPTS" "$JAVA_OPTS" "$${optsEnvironmentVar}" -Dapp.pid=$$ -Dapp.home=$APP_HOME -Dbasedir=$APP_HOME"
exec nohup "$JAVACMD" -jar $JVM_OPTS <% if ( appNameSystemProperty ) { %>\"-D${appNameSystemProperty}=$APP_BASE_NAME\" <% } %> $CLASSPATH server /resources/config.yml > /home/testUser/stdout.out 2> /home/testUser/stderr.err &
The application starts up fine, but on code review, we noticed that the value of -Dapp.pid was incorrect, by checking it in ps -aux | grep appName, and comparing it to the PID of that command, along with the PID outputed by pgrep -f appName. I would like to know if there is any way to assign the correct PID to the parameter. So far, I've tried setting it to be:
-Dapp.pid=`preg -f appName`
But that simply ends up with -Dapp.pid being blank, which I assume is due to it calling that command before the exec is fully run. Has anyone else come across this before?

./server_start.sh: line 41: kill: (21556) - No such process

I was trying to start the jobserver.It fails and i dont see any logs for ERROR
/usr/share/dse/spark/spark-jobserver
./server_start.sh: line 41: kill: (21556) - No such process
You need to start it with the dse command
dse spark-jobserver start [any_spark_submit_options] //Start the job server
dse spark-jobserver stop //Stop the job server
https://docs.datastax.com/en/datastax_enterprise/4.8/datastax_enterprise/spark/sparkJobserverOverview.html
If that doesn't fix it it probably means a false start or improper shutdown has left a spark-jobserver.pid file in the spark-jobserver resource dir. Remove this so that the following code can pass.
if [ -f "$PIDFILE" ] && kill -0 $(cat "$PIDFILE"); then
echo 'Job server is already running'
exit 1
fi
This code is checking to see if that PID file exists and if it does it tries to get info from the process. If the process doesn't exist then that means the pid file is there in error and must be removed (or you lack permission).

How to grab a particular log file and show its content in jenkins Console output

I have the following Jenkins post-build shell script:
ssh user#my_server <<EOF
service my_service stop
service my_service start
tail -f /opt/services/my_service/logs/current
exit
EOF
This script restarts my_service on a remote host (my_server).
My problem is: command service my_service start just makes a request to RUNIT to run a my_service, i.e service my_service start returns immediately after execution.
But service my_service start runs a SpringBoot java web application that writes all log info into .../logs/current log file. To catch this log info I've added command tail -f /opt/services/my_service/logs/current but in this case Jenkins build is never ends)) such as tail -f command never stops.
Is there a way to execute my post-build script (which only start my web app on a remote server) and grabbing the .../logs/current log file during 2 minutes or until this log has the line "Web app MyApplication has been Started".
I wanna see the content of .../logs/current log file right in Jenkins's Console output and kill tail -f after 2 minutes
tail -f will not end until it gets interrupted, so your script will never finish running.
what you can do is use grep -q on your log, which will exit with 0 exit status when it finds it's pattern:
grep -q 'Web app MyApplication has been Started' <(tail -f /opt/services/my_service/logs/current)

bash scripting: search Java STDOUT for a variable

When I run myTest.jar, it outputs alot of information in STDOUT (not in a file) and I am trying to read/search that file for a specific string to put as a variable in my bash script.
java stdout:
line 1 info............
line 2 info...........
....
...
Successful (either 'Successful' or 'Failed')
How do I search for, in bash, the last line in the stdout or for ('Successful' or 'Failed') without redirecting the stdout to a file?
Thanks in advance
This is not a good way of checking for success or failure.
You should instead rewrite myTest.jar to use System.exit(0) on success and System.exit(1) (or higher) on error. If the program is well written, it will already do this.
You can then check for success or failure in bash using e.g.
if java -jar mytest.jar
then
echo "The command succeeded :D"
else
echo "The command failed :("
fi
All UNIX programs work this way, and you should make sure that myTest.jar is no exception.
To make this work, you would need to redirect stdout to a file and then cat/vim/search that file. Bash doesn't save the output of commands on its own.
Alternatively, you could use a program like screen that allows you to save a transcript of your session to a file. You would get the output of everything and the command lines though.
for future users, I used "that other guys' " method with some minor adjustments:
1) the exit code and what your doing have to be right after the running jar, wouldn't work for me in other segments
2) I read the exit code I was receiving and made an if loop for my conditions (0-2) exit code was given in java
3) piece of my bash code
EXIT_CODE=$?
echo Exit code: $EXIT_CODE
if [ $EXIT_CODE = "0" ]
then
...............do something
elif [$EXIT_CODE = "1"]
then
...............do something
else
...............do something
fi

How start jboss service remotely with SSH?

I need to start the service of Jboss 7.1.1 remotely through SSH. But when execute the command does not happened.
The command: ssh user#server '/etc/init.d/jboss-as start' #(no error, no service started)
The script jboss-as:
#!/bin/sh
case "$1" in
start)
echo "Starting JBoss AS 7"
su --command "/path/to/jboss-as-7.1.1.Final/bin/standalone.sh >& /dev/null &" root
;;
stop)
echo "Stopping JBoss AS 7"
su --command "/path/to/jboss-as-7.1.1.Final/bin/jboss-cli.sh --connect command=:shutdown" root
;;
*)
echo "Usage: /etc/init.d/jboss-as {start|stop}"
exit 1
;;
esac
exit 0
How to execute the command: ssh user#server 'service jboss-as start' or ssh user#server '/etc/init.d/jboss-as start'?
The connection with ssh is OK
The Jboss Server is OK
If i execute the code: ssh user#server '/etc/init.d/mysql restart' it happens!
One of a few things are limiting your ability to run this service with the command as that is a valid method of starting the service.
user#server '/etc/init.d/jboss-as start'
All of which you can test remotely after initiating the SSH connection. SSH into the server and start the service with the same user you are going to connect with using the above command.
Firstly make sure the service is actually called 'jboos-as' with ls /etc/init.d/ |grep 'jboss'. The result will be exactly how you will call the command so replace jboss-as with the output from the grep.
Secondly it is a permissions issue on the init script. From what I could see online you have to create this script so if the permissions are not setup correctly it will not execute.
To check run ls -al /etc/init.d/ |grep 'jboss' and your output should appear as follows:
Output:
-rwxr-xr-x. 1 root root 2979 Sep 19 05:34 jboss*
The user issuing the start command will need to match the first user listed. In this case the first 'root' and/or be in the same group as the group listing which is the second 'root' in the example. This can vary if for instance your user is in the wheel group, but generally services are run as root or a specific user for that service.
Lastly The more important aspect is that the file is executable. This is listed as the x value in the ls -al output above. If no 'x's are listed you will need to make the file executable with the following:
chmod +x /etc/init.d/jboss
IMPORTANT all the above command will need you to referrence the file as it output in the first grep command, so /etc/init.d/jboss-as or /etc/init.d/jboss or /etc/init.d/jboss-something different.
I hope this helps you out and if it does not please post the results of the ls -al output and we can help you further.
Ok.
Let`s go.
Search the name of the jboss service:
ls /etc/init.d/ |grep 'jboss' Returned "jboss-as". It is ok!
Permissions:
ls -al /etc/init.d/ |grep 'jboss' Returned exactly the same output: -rwxr-xr-x. 1 root root 2979 Sep 19 05:34 jboss-as Its ok!
Still do not works.
The principal objective to execute this command is an action of button in the Java program using SWT and the lib that implements SSH called JSCH. Look the code:
Session session = jsch.getSession("user", "SERVER_IP_ADDRESS", PORT);
session.setPassword("pass");
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
Channel channel = session.openChannel("exec");
((ChannelExec)channel).setCommand("'/etc/init.d/jboss-as start'"); #command to start jboss service
channel.setInputStream(null);
((ChannelExec)channel).setErrStream(System.err);
InputStream in=channel.getInputStream();
channel.connect();

Categories

Resources