Run shell script from Java to send data via USB - java

Exactly as specified in the title, I'm trying to send data to my ODROID-Show external screen via USB. I'm running a shell script that sends such data. The problem is I can simply run the command through Terminal and it runs successfully and data is sent to my little screen through USB port. When I try to run the same command via Java, Nothing happens.
Process proc = Runtime.getRuntime().exec("/bin/bash -c /home/ahmed/ODROID-SHOW-master/example/linux/images.sh /");
The specified command should have root privileges to run. That, I've switched to root then ran the code and nothing happened. Any thoughts how to solve such problem?
Edit:
IF you can show a code that executes given command prefixed by sudo this will absolutely work.
I was able to run the program as root. but, corrupted data are sent to ODROIDscreen rather than valid images. while it transfers successfully when ran through Terminal, Any thoughts why that happens?

I would check if the script executed by the bash interpreter requires certain environment variables set before execution.
I'd add a debug line in the executed shell script to dump the environment like "env > my_dump_env.txt" then run the script both from command line as well as from Java and do a diff see what is missing or is different.

Related

Running Java program in a Docker Image

So I have a Docker network that has a Docker file with a bunch of information. I have a java program that is going to bring up the enviorment and then produce several commands to run within this enviorment. To be clear, the first command I need to run is NOT inside the Docker enviorment. I am having some challenges with the Process and Runtime classes.
First, say I wanted my java program to launch a new gnome terminal and then run a command to get into the docker network. I have this command,
Process process = Runtime.getRuntime().exec(new String[]{"gnome-terminal"});
Gnome terminal sucessfully comes up but any additional arguments I give in this array are just ignored. For example,
Process process = Runtime.getRuntime().exec(new String[]{"gnome-terminal","ls"});
Does not work. The command I ultimatly want to run would look something like this,
Process process = Runtime.getRuntime().exec(new String[]{"gnome-terminal","sudo","docker","exec","-it","sawtooth-shell-default", "bash"});
Second, Once I have this running, will additional commmands I run work within the Docker enviorment? I have a python file with a Stream handler that specifies the correct commands to run.
Other documentation on related issues was limited.
I made sure my code was wrapped in a runtime exception try catch and that I was running the correct .class file. Any help on this would be great!
Edit: I have also tried to run this in another linux terminal like Hyper and Tilda
I also am able to get a sudo sign in when I run the command like so,
Process process = Runtime.getRuntime().exec(new String[]{"gnome-terminal","--","sudo","docker","exec","-it","sawtooth-shell-default", "bash"});
However it closes immediatly after authorizing.
Okay this is what I was attempting to do.
https://www.atlassian.com/blog/software-teams/deploy-java-apps-with-docker-awesome
This site is outdated and I had to use this link for getting that latest version of the java PPA.
This process basically installs java into the docker contatiner so that I can run a java program that uses Runtime.

Impersonating an user in Linux through Java

I have a Java application that runs on a Linux server and performs series of actions (create directory, copies few files, checks if an application is running etc). I am using log4j2 for logging messages and it has been running fine.
I've a requirement to read an account and impersonate as that account (passwordless sudo switch user) and then perform series of above actions. Access to do a passwordless sudo has been taken.
I've already done this using a shell (first switch user and then execute the Java program) however I've raised this question to understand if this can be done within Java program ?
Execution: java -jar xyz.jar parm1
To evaluate, I changed the Java code to run a shell command as below (I am using processbuilder):
When I run the Java program, the logs appear only till the point of sudo shell command execution and no logs are shown post that step.
Is there a way I can retain all logs before and after switching user (impersonating) ? (I'm using log4j2 and writing to a log file which has write access to other users).
Please share if there is any known solution ?
[EDIT] Adding java code snippet used to trigger sudo
public boolean switchUser(String account) {
ShellCommand shellCommand = new ShellCommand();
List<String> cmd = new ArrayList<String>();
cmd.add("su");
//....I construct asu command for the passed account here. I have passwordless sudo su access and tested fine manually
//I've some logger messages here
shellCommand.runCommand(cmd); //Class with method that uses ProcessBuilder and executes shell command
}
[EDIT] I think when I trigger su from Java it opens a new shell while Java is waiting for it to complete. This is possibly the reason of no logs visible there after. So one solution will be to Wrap it in a shell, do a su with command to run the jar.

Command stops whenever I log out of my server

I have a java program (text-based no GUI) that I have written and compiled and uploaded to a server.
I run it with java -cp myjar.jar mypackage.MyClass which starts it running processing a datafile with 20,000,000+ entries in it and printing output to System.out. I have calculated that it will take a very long time to process the data and I didn't want to have my laptop open for the 10 days of number crunching...
When I log out of my shell however, the process stops.
How can I execute the command and log out without it stopping? What is that even called?
I am using an Amazon Ubuntu EC2 server. I log in using a certificate from Mac OSX with terminal. The server seems to be using a bash shell.
Hope someone can help me out!
Jason.
Consider using screen instead of nohup. It allows you to create a virtual terminal that persists even after you logout/disconnect. When you reconnect to the server, you can immediately jump into the screen session you last had open.
Typical workflow on the server:
type screen (you may need to press space to leave intro page)
type in your command that you want to leave long-running (your java program, or an OS upgrade)
press ctrl a+d to leave screen (make sure to hold ctrl down)
To re-enter screen just use screen -r, and you will see the previous terminal and any running programs as you left it.
You can use nohup
nohup java -cp myjar.jar mypackage.MyClass > yourLogFile.log &
-----> http://ss64.com/bash/nohup.html

It can't process the by clicking the spark-shell windows command script. What i do?

In the Process of installing spark 1.0.0 by double clicking the bin/spark-shell windows command script file. Then opened one command prompt file and then immediately closed it self only. Are there any commands required to run this. Could you please tell me step by step process.
First of all, you have to open a terminal. Theorically, you at least have the following on your machine :
cmd (for sure)
powershell (maybe not, if you're using Vista or less).
From there, you have two options :
if you added path_to_spark_folder\bin to your PATH variable (see there for more informations), you can run spark-shell as soon as the console is opened
if you didn't, you'll have to go to path_to_spark_folder\bin yourself, using the cd command.
You now can run spark-shell.

How to Run Commands through a already running Java?

I was wondering if it was possible to execute commands from PHP to a Java prompt which is already running?
I have tried the solution listed here:
How to run a shell command through PHP code?
and this provided no functionality
Let me explain
The java is running on one screen of the linux server
sudo apt-get install screen
and running the .jar file through the command line.
I am then running a webserver, which will have an admin accessibility to restricted areas, which will contain scrips to run specific commands through that already running .jar file?
You can implement some kind of IPC. The java file listens to a port and receives the commands. Or you can write the commands in a specific file which the java programm reads. I think under linux you can also use shared memory: http://www.php.net/manual/en/book.shmop.php
It is possible by sending the command to the screen session. I used this for a minecraft server once.
screen -S <sessionname> -X stuff "<command>\r"
This would (IIRC) provide the same output as if you where inside the screen, typed the command and pressed enter.
I hope this was what you wanted.

Categories

Resources