Execute bash commands via Java in Ubuntu - java

I have such command:
top -bn1 | grep \"Cpu(s)\" | sed \"s/.*, *\\([0-9.]*\\)%* id.*/\\1/\" | awk '{print 100 - $1}
I tried to run it with ProcessBuilder or Runtime, but I get nothing. Why?

Related

Runtime.getRuntime().exec(command) - Cannot run program, error=2, No such file or directory

I want to get video duration with help of ffmpeg:
String command = "ffmpeg -i /home/user/Videos/my-video.mp4 2>&1 | grep Duration | awk '{print $2}' | tr -d ,"
Runtime.getRuntime().exec(cmdarray);
But i always get
java.io.IOException: Cannot run program "ffmpeg -i /home/user/Videos/my-video.mp4 2>&1 | grep Duration | awk '{print $2}' | tr -d ,": error=2, No such file or directory
If I run this command from terminal - all is ok
You've got several issues here. Firstly as #joy points out there could be a problem with the Path used by Java to locate the command so Java may not be finding a command called "ffmpeg". Fixing the Path used for launching your VM should resolve that, or just insert the fully qualified pathname to "ffmpeg".
Secondly: you are trying to run a terminal / shell command. The "|" pipes are normally interpretted correctly by a terminal / shell which breaks the chain into sub-processes linking stdout/stdin. But Java is being asked to run "ffmpeg" passing in some arguments containing "|" which would not be handled as you wish by "ffmpeg".
Check the shell you use:
echo $SHELL
Let's say that printed /bin/bash - you can fix by getting Java to launch the shell and make that interpret the pipe command:
String[] command = new String[] { "/bin/bash", "-c", "ffmpeg -i /home/user/Videos/my-video.mp4 2>&1 | grep Duration | awk '{print $2}' | tr -d ," };
Runtime.getRuntime().exec(cmdarray);
Most likely the path isn't the same when you run from terminal vs when you run from Java. 1. you can try using the full path of ffmpeg (run "which ffmpeg" in terminal). 2. perhaps ffmpeg is an alias in your .profile file in that case you can try to source(load) your .profile file first before executing the command in Java.

Get the Main Package Name of an .apk File

I have to launch an application on android emulator using command line.for that I have knew that there is a method by adb command;
adb shell am start -a android.intent.action.MAIN -n com.example.myApplication
but I have to find the application package name before executing this command, so anybody help me to find the package name, or any alternate solution for running apk on emulator
On Linux, simply use this command to get package name:
aapt dump badging {apk-file.apk} | grep package | awk '{print $2}' | sed s/name=//g | sed s/\'//g
Getting main (launcher) activity is similar:
aapt dump badging {apk-file.apk} | grep launchable-activity: | awk '{print $2}' | sed s/name=//g | sed s/\'//g
For my personal usage, I've written a shell script called apkinfo.sh as following:
#!/bin/bash
package=`aapt dump badging $* | grep package | awk '{print $2}' | sed s/name=//g | sed s/\'//g`
activity=`aapt dump badging $* | grep launchable-activity: | awk '{print $2}' | sed s/name=//g | sed s/\'//g`
echo
echo package : $package
echo activity: $activity
And use it as following:
./apkinfo.sh {path-to-apk-file.apk}
aapt dump badging <path-to-apk>
extract the apk file and rebuilt the manifest file, then by parsing the xml manifest file you can find the main activity package name.
refer this link for parsing, and refer this link for extracting apk file.
This is what I use for this in one of my projects, inside a shell script.
apk= #apk path
package=`.//aapt dump badging $apk | grep package | awk '{print $2}' | sed s/name=//g | sed s/\'//g`
echo $package
adb install $apk
if you want to just enter in cmd prompt, then it would be
.//aapt dump badging ***apk relative path*** | grep package | awk '{print $2}' | sed s/name=//g | sed s/\'//g
that should work.
You can use Andgroguard
This is how I did in Python3:
from androguard.misc import AnalyzeAPK
a, d, dx = AnalyzeAPK(apk_file)
package_name = a.get_package()
print(package_name)

java How can I find out the original username a process was started with?

How would I get to know the real/ogiginal username ?
On linux, we can run the below command, which would give the real user name based on the link but what is the java equivalent of the same ?
REALUSERNAME=$(ps uhp `ps -AjH | grep \`ps -u $USER fh | awk '{ print $0; if(index($0, "ps -u $USER fh")) exit 0;}' | tac | awk '{if(!index($0, "\\\\\_")){print $1; exit 0;}}'\` | awk '{print $3}'` | awk '{print $1}')
How can I find out the original username a process was started with?

Jboss 7.1.1 start/stop script

Could someone tell how to start/stop the Jboss-7.1.1 server in MAC using Shell Script.
stop_viewer(){
echo "********* Stopping JBoss Server by killing the process **********";
ps | grep domain.sh | grep -v grep | awk '{print $1}' | xargs kill
ps | grep java | grep -v grep | awk '{print $1}' | xargs kill
ps -ef | grep superuser | grep java | grep -v grep | awk '{print $2}'| xargs kill
echo "********* Stopped JBoss Server by killing the process **********";
}
The above script is working fine in Jboss-7.0.2 to stop the server. But in Jboss-7.1.1, it doesn't stop the server. Please someone help to solve this.
1) First you need to have JBoss downloaded. (I assume you already have valid Java version installed).
2) Once it is downloaded, unzip the folder:
cd /Users/eugene/Downloads
mkdir JBOSS-7
cp /Users/eugene/Downloads/jboss-as-7.1.1.Final.zip /Users/eugene/Downloads/JBOSS-7
cd /Users/eugene/Downloads/JBOSS-7
unzip /Users/eugene/Downloads/jboss-as-7.1.1.Final.zip
3)
cd Users/eugene/Downloads/JBOSS-7/jboss-as-7.1.1.Final/bin
./standalone.sh
If you want to stop it:
ctrl + c
of course your path may be different. If you want to run it in background, then just do:
./standalone.sh &
Stopping it :
ps -ef | grep jboss
You will get an output close to this one:
eugene#eugenes-MacBook-Pro ~/D/J/j/bin> ps -ef | grep jboss
501 1471 1446 0 1:32AM ttys000 0:03.31 /usr/....
And then issue:
kill -9 1471
Finally with JBoss CLI you can execute:
./jboss-cli.sh --connect ":shutdown"
EDIT
The Script seems to do it's job, all you have to do is edit it a bit:
#!/bin/sh
echo "********* Stopping JBoss Server by killing the process **********";
ps -e | grep jboss | grep -v grep | awk '{print $1}' | xargs kill
echo "********* Stopped JBoss Server by killing the process **********";
Notice that I removed a few lines and changed java with jboss
Put this in a file called stopJboss.sh
Then :
sudo chmod +x stopJBoss.sh
Then invoke it when needed:
./stopJBoss.sh
This will work only if you have a single instance of JBoss running, for more you will need a different script.
P.S. I am not a guru in scripting but here is what this line does:
ps -e | grep jboss | grep -v grep | awk '{print $1}' | xargs kill
It is going to look for every process that contains the jboss keyword. But it also going to output the grep command itself, thus you will get an output of two commands, but you need only the first one.
You could run ps -e | grep jboss and see that the output contains two lines and not one.
That is why you invoke grep -v grep - which means : in those two lines found grep for "grep" but invert the result, in this way you omit the second unneeded result.
Then awk '{print $1}' splits the string into tokens and takes the first one, which is the PID that you need and then you pass this PID to the kill command using the xargs command.
To shutdown the server via command line
sh ./bin/jboss-cli.sh --connect command=:shutdown
assuming you are running on localhost and using the default native management port i.e. 9999
if not you need to specify the IP (jboss.bind.address) and the native management port(jboss.management.native.port) configured in standalone.xml
sh ./bin/jboss-cli.sh --connect controller=<IP>:<native-mgmt-port> command=:shutdown
This is how I do it:
ps -ef | grep jboss | grep -v grep | awk '{print $2}' | xargs kill -9
I have this in a bash file that i call killjboss and it works well with me.
After dive on the Google, i managed to put this work:
#!/bin/sh
### BEGIN INIT INFO
# Provides: jboss
# Required-Start: $local_fs $remote_fs $network $syslog
# Required-Stop: $local_fs $remote_fs $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start/Stop JBoss AS v7.1.1
### END INIT INFO
#
#source some script files in order to set and export environmental variables
#as well as add the appropriate executables to $PATH
export JAVA_HOME=/usr/lib/jvm/java-7-oracle
export PATH=$JAVA_HOME/bin:$PATH
export JBOSS_HOME=/home/gaspar/jboss-as-7.1.1.Final
export PATH=$JBOSS_HOME/bin:$PATH
case "$1" in
start)
echo "Starting JBoss AS 7.1.1"
#original:
#sudo -u jboss sh ${JBOSS_HOME}/bin/standalone.sh
#updated:
start-stop-daemon --start --quiet --background --chuid jboss --exec ${JBOSS_HOME}/bin/standalone.sh
;;
stop)
echo "Stopping JBoss AS 7.1.1"
#original:
#sudo -u jboss sh ${JBOSS_HOME}/bin/jboss-admin.sh --connect command=:shutdown
#updated:
sudo -u jboss sh ${JBOSS_HOME}/bin/jboss-cli.sh --connect command=:shutdown
;;
*)
echo "Usage: /etc/init.d/jboss {start|stop}"
exit 1
;;
esac
exit 0
:)

Shell script to stop a java program

Is there a way to stop a java program running using a shell script by knowing the name alone.I am using ksh shell
following up on Mnementh' suggestion:
this should do the job
jps -l | grep org.example.MyMain | cut -d ' ' -f 1 | xargs -rn1 kill
jps -l: list java process with "full package name for the application's main class or the full path name to the application's JAR file."
grep: choose the process you like
cut -d -' ' -f 1: split the output in columns using delimiter ' ' and print only the first one (the pid)
xargs -rn1 kill: execute kill for each PID (if any)
note that you must run jps and xargs with the same user (or root) as you're running the process
Add a unique property to the JVM to identify it easily, e.g. for test.class
java -Duniquename=1 test
To kill it:
ps ax | grep uniquename | grep -v grep | awk '{print $1}' | xargs kill
You can use jps identifying the process-id associated with the name of the started java-program (jps is a process-manager for java-programs). With this id you can kill the process normally.
You can use pkill:
pkill your_java_program_name
This would work if you run only one instance of the your program is running.
you can use -o option of ps to format your output,
ps -eo cmd,pid | awk '!/awk/&&/mycommand/{cmd="kill -9 "$2;system(cmd)}'

Categories

Resources