linux shell script in java - java

I am using below shell code in linux i want it to make it in Java.I want to call this command in JAVA
mailq | grep -B1 -i temporarily | grep -iv deferred | \
egrep -i 'jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec' | \
awk -F" " '{print $1}' | awk '{print substr($0,10,14)}'
mailq command shows the queued mail messages with there respective Id and there errors .
Like in command above i am looking for messages with temporarily disabled message in it and taking the above message where its id is present in **09089 like this checking the month and than printing the last 5 characters of that id

String cmd = "mailq | grep -B1 -i temporarily | grep -iv deferred | "
+"egrep -i 'jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec' |"
+ " \'{print $1}\' | awk \'{print substr($0,10,14)}\'";
try
{
ProcessBuilder pb = new ProcessBuilder("bash", "-c", cmd);
Process p = pb.start();
p.destroy();
}
catch (Exception e)
{e.printStackTrace();}

here
http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

In general this type of issues is handled in java by 'Runtime' class.
Here Runtime example java
you can find an example of how to do that
The newer approach would be using ProcessBuilder class though

If I understood correctly, you want to do the same thing that this shell command, but in pure Java, not by running it from Java. One advantage of the port can be portability.
I think you want JavaMail to do the mail access. I am not a specialist of this API, but I used it in a small Processing program to show its usage: Email sketch.
Then you need to filter out the results and to isolate the part you want, which isn't very hard.

Related

How to kill a process which is running form a directory in CentOS?

We have a requirement where we have copies of the same Spark application (basically a .jar file) running from different folders. I am looking for a shell command using which I can kill the Spark app running in one folder without killing other spark jobs.
Ex:
/home/user/Folder1 - CapStoneSpark.jar
/home/user/Folder2 - CapStoneSpark.jar
/home/user/Folder3 - CapStoneSpark.jar
The main class in the jar file is "CapStoneSparkMain". Suppose, I want to kill the CapStoneSpark.jar running in Folder2 only without touching or killing the CapStoneSpark.jar running from Folder1 and Folder3, then what should I do?
I have already tried:
kill $(ps aux | grep 'CapStoneSparkMain' | awk '{print $2}')
But, it kills all the process which have "CapStoneSparkMain" class in them.
I only want to kill the process originating from a single directory and don't want to touch the copies of the processes running from other directories.
You can find all Proccess ID's which using these folders:
lsof | grep /home/user/Folder | awk '{print "kill " $2}'
And execute it:
lsof | grep /home/user/Folder | awk '{print "kill " $2}'|sh
It's not clear how the jobs are started, but assuming that each job is started with a different working direectory, it is possible kill the specific job. Given that it's possible to find the working directory of each process via the /proc/$$/cwd (symlink to the job folder). Building on the commands/loop suggested above:
kill_folder=/home/user/Folder2
for pp in $(ps aux | grep 'CapStoneSparkMain' | awk '{print $2}') ; do
if [[ /proc/$$/cwd -ef "$kill_folder" ]] && kill $$
done
The code will check if the symlink /proc/$$/cwd matches the named folder (kill_folder), and will only issue the kill to processes in that folder.
Hello
A for loop that check for the user in...
for i in $(pidof application); do grep -o ${user} /proc/${i}/cmdline; done
And if it fits your desire then put kill command on ${i} in loop.

grep md5has from java command

I have tried many combinations, but I can't get a specific string from a Java command to generate an md5 hash:
java -cp /var/lib/rundeck/bootstrap/jetty-all-7.6.0.v20120127.jar org.eclipse.jetty.util.security.Password admin outsideit.net | grep -o "^MD5"
outsideit.net
OBF:1y0q1w9b1xtx1l1g155w1toa1t331tok1wui1kxm1xtl1w8f1y10
MD5:a7da14229ea147aaa364e503947cbe35
CRYPT:adiwf3pJ9m8Vw
Whichever grep statement I try it always outputs the above.
As the java command throws the output as stderr, the bash will not be able to grep it untill you specify "&> >(grep MD5)" instead of " | grep ". The below command show work for you.
java -cp /var/lib/rundeck/bootstrap/jetty-all-7.6.0.v20120127.jar org.eclipse.jetty.util.security.Password admin outsideit.net &> >(grep MD5)

how to get only listening ports of a process in linux

I tried different commands netstat , lsof, but my requirement is,
I want to pass the listening port say a process x is running on chl21000303.sesson.net:32456.
I want a linux command which reads only the port 32456 and want to pass it in java like java -jar runs.jar port.
So I need some commands to get only the running port or listening port. If I try netstat commands like that I am getting say some 20 lines of ports.
netstat | awk '{print $4}' | sed -nr 's/.*:([0-9]+)/\1/p' | xargs java -jar runs.jar
I am kinda unclear about the question. The above would plug all the port numbers into the arguments of your java file
If you are sure that it will be on chl21000303.sesson.net you could instead do
netstat | awk '{print $4}' | sed -nr 's/chl21000303.sesson.net:([0-9]+)/\1/p' | xargs java -jar runs.jar
edit-
for sake of completion ill give a quick overview of what everything is doing.
netstat gets all the connections and pipes it to awk. awk gets the 4th column and pipes it to sed. sed gets the port number using a regular expression and pipes it to your java program as arguments
You can do this in shell script
var=`"netstat -atn | grep LISTEN | awk {print $4}"`
for x in $var
do
port=`echo $x | awk -F':' '{print $2}'`;
echo $port;
done
You have your port number in $port you can pass it to your java script

Android: Passing outputstream of top command to the inputstream of grep to achieve piping " | "similar in shells

I'm essentially trying to accomplish the same result that running the command:
"top -n 1 -s cpu | grep -E 'list_of_package_names'"
on a machine that supports shell features such as piping ( | ) would return. That being the rows that are returned by the top command that contain the package name filtered by grep. Normally pretty simple stuff with shell features, however to my knowlegde android does not come with a commands shell and access to these command can only be achieved using Runtime:
Process p = Runtime.getRuntime().exec(...);
and then getting the inputstream and reading from it.
The problem is that Runtime.getRuntime().exec(...); does not know how to deal with shell language and the piping of commands. So is there a way to pipe the ouput of the the top command to the input of the grep command to essentially create the same functionality? Or is there a way to run the commands from a script or anything else that can achieve this result?
On a side note, I have been using adb client/server protocol in my shell commands to test the correctness of the syntax for example:
Runtime.getRuntime().exec("adb shell top -n 1 -s cpu | grep -E '" + shellParams + "'");
This returns the expected result as the adb shell contains shell features however cannot be used outside of debugging i.e. at runtime.
Any help would be greatly appreciated, this is my first post so please do let me know if I should be structuring my answer at all differently.

Python - how can i read in and read out so that other readers can read it for further pursing?

I have to run the Java application and read the syslog to trigger some other Python based events.
At the same time i also need to dump and store it in /var/tmp/log.log of all java outputs, but because the new Python event controller is added that /var/tmp/log.log i was not able to create. Any idea how can i still make it? . For example: java | python >> java logs as >> log.log for tail -f
BEFORE: (worked)
$ java -cp /var/tmp/Audio.jar Main.Boot >> /var/tmp/log.log &
$ tail -f /var/tmp/log.log
AFTER: (not working)
$ java -cp /var/tmp/Audio.jar Main.Boot | python -u /var/tmp/consumer.py &
$ tail -f ????? how can i have the java syslog still dumped as like my BEFORE ????
/var/tmp/consumer.py
import sys, time, os
while True:
line = sys.stdin.readline()
if line:
sys.stdout.flush()
if "wall:on" in line:
os.system("/var/tmp/me.sh")
else:
time.sleep(1)
/var/tmp/me.sh
#!/bin/bash
export DISPLAY=:0.0
ps aux | grep "/var/tmp/pp.py" | awk '{print $2}' | xargs kill -9;
# System maintain..
python /var/tmp/pp.py &
sleep 3
ps aux | grep "/var/tmp/pp.py" | awk '{print $2}' | xargs kill -9;
Off the top of my head:
$ java -cp /var/tmp/Audio.jar Main.Boot | tee /var/tmp/log.log | python -u /var/tmp/consumer.py &
$ tail -f /var/tmp/log.log
I would also do the awk/grep stuff inside python instead of going through a subprocess.

Categories