Is there a way to let a tomcat 7 server (running on an Ubuntu Server) execute a sudo command in a command line?
In this specific case I want to make it possible to shut down the system from another server.
What code would I have to use?
And do I have to run the tomcat server as root or can i get these privileges at runtime and only for this action?
Thanks for your help:)
No need to run Tomcat as root, you can just add tomcat user to /etc/sudoers to allow him run superuser commands, like sudo shutdown now. This way allows you to specify desired subset of commands that can be executed by user. E.g. to allow him to run only shutdown:
tomcat ALL=NOPASSWD: /sbin/shutdown
To run shell commands from Java code you can call one of Runtime.exec() or use ProcessBuilder.
P.S. Also try googling about /etc/shutdown.allow file which allows running shutdown command by any user that is listed in it. But I've never used it.
Related
I just bought a vserver and now I'm trying to run a jar file on it permanently.
The problem is, that if I connect to my vserver via PuTTY, the sessions ends when I close the program and that kills my program. How can I open a terminal sessions where I can run my jar file and which never stops? I'm running Ubuntu 20.04 on my server
Try the following:
nohup [your command and parameters] &
nohup is a unix command that means 'no hangup', so it won't kill the session when you disconnect.
The & means 'run this command as a background process'. That will let you disconnect without having to kill the program.
Here's more info on nohup : https://en.wikipedia.org/wiki/Nohup
In the longer-term you'll likely want to install the app as a service to start when you reboot the machine. The way to do so will depend on the flavor of unix/linux you have.
Best of luck!
Use nohup, screen, tmux or create a systemd service unit.
I want to execute shell script on remote windows server through java code without any server setup and installations like SSH. My script file is already there on server. I just want to execute it on server cmd from my machine. Any possible solution?
According to definition of shell script from wiki:
"A shell script is a computer program designed to be run by the Unix shell"
Taking into consideration that you want to run a shell script on a windows server it will simply not work. I would suggest you to use some kind of environment as it is provided by cygwin or any other alternatives.
You can also check the following question.
I am running my project as jar using java -jar command in Linux machine. As soon as this program run , It produces logs in another directory. Running my program this way requires me to keep the shell open. Now If I have to see the logs , I can't do that in the same shell. I am forced to do that by either doing the duplicate session or new session. Is there any way I can run the jar as background process and see the logs in the same shell ?
If you don't care about it staying alive, something as simple as nohup java -jar myjar.jar & should work. If you need it to be automatically restarted if it crashes or start automatically at boot, you'll want to look into something like systemd or monit.
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.
I would like to run a jar file extracted from my java project to be run on a Linux server I connect through SSH Tunneling. There are few problems, first there is something wrong with the Display: I get the error
No X11 DISPLAY variable was set, but this program performed an operation which requires it.
at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:173)
at java.awt.Window.<init>(Window.java:437)
at java.awt.Frame.<init>(Frame.java:419)
at java.awt.Frame.<init>(Frame.java:384)
at javax.swing.JFrame.<init>(JFrame.java:174)
at random_v2.v2Frame.<init>(v2Frame.java:127)
at random_v2.Wrapper.main(Wrapper.java:25)
and second is that I am not sure if I have to install other applications as well. In my code, the java program needs to get run other applications like weka, do I have to install weka with the same directory name and specs that is in my mac?
I appreciate your help in advance.
Best wishes
Assuming you're tunneling into this unix box using PuTTY:
Make sure X11 forwarding is enabled in your PuTTY settings.
Connect via command line using this command:
ssh -X user#server
The -X option enables X11 forwarding.