How to execute linux service from java code - java

I have a service in linux called appSevice, when I start/stop with these cmd, it works :
sudo systemctl start appSevice.service;
sudo systemctl stop appSevice.service;
but the problem when I tried to execute these from JAVA code, I tried form exemple :
Runtime.getRuntime().exec("sudo systemctl stop appService.service");
but it didnt work, I can that the service is always running, any suggestions please to resolve this problem ?
service :
[Service]
Type=simple
ExecStart=/opt/soft/v1/launchAppService.ksh start
User=Jms-User
Restart=on-abort

Steps to help you execute a system command via java program:
create user and give him rights to execute systemctl command, see
this thread allowing user to run systemctl/systemd services without
password
execute your java program using this user
your java code should be:
Runtime.getRuntime().exec(new String[]{"systemctl", "stop", "appService.service"});
the first argument is the command to
execute, others are the arguments

There is one way also. Create new user with LimitedRoot capabilities and use VISUDO to allow the user to run systemctl/systemd services without entering a password.
Example:
%LimitedRootUser ALL=NOPASSWD: /bin/systemctl restart appService.service

Related

Windows - running command as a service

I have a command that I would like to create a service from. For example: "java -jar agent.jar" (the command is blocking - when closing the cmd it will stop the agent). I would like to make the command run as a service in the background so I can log out from my user and it will still work.
On Linux, I know for a fact that I can use nohup mycommand. The problem is on Windows OS. Also, replace java with javaw will solve the blocking issue but when I sign out it will still kill the agent.
How do I do that?
I'll appreciate your help!
Tomer.
Use Windows services feature.
Read this answer.
And this solution.

Centos java custom service

I have a script.sh that set some environment variable and start a java server.
#!/bin/bash
export JAVA_HOME="/opt/java"
export ....
nohup $JAVA_HOME/bin/java "$MEMORY_JAVA_OPS" -classpath "$MY_CLASSPATH" $MAIN_CLASS &
I would like to transform this script (now is launched by /etc/rc.d/rc.local) in a service.
I tried many examples found online and over StackOverflow.
I created myservice.service file using many templates found online... No one work!
one example is:
[Unit]
Description=MyService Java Process Restart Upstart Script
After=auditd.service systemd-user-sessions.service time-sync.target
[Service]
User=root
TimeoutStartSec=0
Type=simple
KillMode=process
#export JAVA_HOME=/opt/java/jdk-9
#export PATH=$PATH:$JAVA_HOME/bin
WorkingDirectory=/tmp/myworkdir
ExecStart=/path/to/myscript.sh
[Install]
WantedBy=multi-user.target
With some configurations, the service starts but the status command says that it is dead (while it is actually running). With others it does not start. With none it stops with the command stop ....
I tried Type=Simple, forking, oneshot... always some problem.
I would simply that after boot or when user launch systemctl start myservice, service start, and if after some time crash will be started again. And if I will run systemclt stop myservice it stops and not need to kill the process.
Firstly it need to be said, that concept "service" greatly differs in Linux/Unix and Windows environment. From your question seems to me you are looking for Unix solution.
In unix you typically register some statup and stop script/command. The startup script just runs your java application via java -jar app.jar. This application does business logic & also opens listening on some SHUTDOWN port.
The stop script/command just invokes another (or the same with different cmd parameters) java application which does nothing else just sending STOP command to original application's SHUTDOWN port.
You can look in more detail for example on tomcat startup/stop scripts - they are doing exactly this.
For windows is better to use some wrappers like WinRun4J or whatever else. Of course you can have one multiplatform maven archetype for "universal multiplatform" service like we do.
EDITED:
If you are still unsure how to configure it on Linux, read https://linuxconfig.org/how-to-create-systemd-service-unit-in-linux
ExecStart will be the startup java -jar app.jar and ExecStop will be the stopping command java -jar app-stopper.jar

Listen to serial port at STARTUP using java with arguments on Debian(Rasbian)

I've been stuck two weeks trying to figure out how to run this at startup.
I use the following chain of commands on the terminal:
1. source ~/.bashrc
2. source ~/.tinyos.sh
3. java net.tinyos.tools.Listen -comm serial#/dev/ttyUSB0:telosb | python demo.py`
The third command uses java to listen to the serial port and pipes it to a python script which cleans, converts and uploads to mysql localhost.
This works fine on ssh terminal. But ive tried using nohup+update-rc.d, upstart, systemd, crontab to make it run on startup and it just wont work! When I reboot and check logs / database, its as if the command never happened. I need this to run like a daemon and continue running until shutdown.
Thanks a lot.
How are you trying to execute the program ? Are there are permission issues accessing / executing the script ?
Which version of debian are you running - look at upstart scripts if you are running Jesse+
I'd put those three lines in a bash script and use upstart scripts to trigger them on start. Another option is to use supervisord to make sure that your scripts run and restart if for any reason the program crashes.

running application from cmd as administrator through java code

I executed the netsh command from the CMD that was manually opened by me by right clicking the CMD icon from the start and then selecting run as administrator from the options.It worked fine.Now I tried to run the netsh command through my java code,then it is not working.Nothing is happening when i run that code.I want to ask that I can run applications like notepad.exe from the cmd by calling appropriate methods of the runtime class from my java code,But how can I open the same application with the administrator priviliges from my java code.r.exec("notepad"); where r is an object reference to the runtime class will run the application,but the notepad so opened will not be in administrator mode.Actually I guess that learning to run the application in administrator mode from CMD will be enough to do the work done as The corresponding CMD command will be passed as the argument to the exec() method of the Runtime class.So my questions are:
How to run any application from CMD in windows 8 with administrator privilliges?
The way i want to implement the use of netsh is a good thing to practise or there is some other way out i must use these commands from my java code.
I have seen some commands while googling but they where not working out for me,like runas /user:administrator "notepad.exe" etc.
Thanks
You cannot use the runas /user:administrator approach, as that requires a password input which you cannot provide from an external source (such as a Java application) for security reasons.
I had a similar issue to you in the past, and I solved it using PsExec, running the process on localhost with an administrator username and password allowed me to execute external applications as an administrator.
Using your example you could run:
PsExec.exe \\\\127.0.0.1 /accepteula -u USER -p PASSWORD notepad.exe
The "/accepteula" flag prevents the requirement to accept the EULA interactively when run on a machine for the first time.
This approach may require a bit of tweaking to get working with your setup, but hope it gives you a starting point.

Start a process with Java [duplicate]

This question already has answers here:
How do I run a batch file from my Java Application?
(12 answers)
How to input password to sudo using java Runtime?
(7 answers)
Closed 9 years ago.
Recently I am stuck with a Java program. My requirement is, I have to restart my snmp service through java code in my Ubuntu machine. Normally We can do the same with
Runtime.getRuntime().exec("service snmpd restart");
Above code is working fine if I log in to the system with ROOT user.
But now the requirement came that, it may possible client machine doesn't have root permission. In that case to restart the snmp one need to execute the command with sudo "sudo service snmpd restart". This command will ask for machine password and after entering the password system will restart the service.
Now whenever I am trying to execute the java code with the below code, it's not restarting the service. ecasue it doesn't have the option to receive the password.
Runtime.getRuntime().exec("sudo service snmpd restart");
So, please help me to find out a way to restart a service with java when user is not a root user and need to start a service with sudo command.
I'm almost sure that you will not be able to intercept the input for password as that would be a security issue. -- See Ricardo Cachiera's answer.
Regardless I don't recommend you do -S. My recommendation is that you configure sudo to let the java user run the snmpd with out a password (ie NOPASSWD).
So you'll have to know what user you are going to use to the Java code. Once you do, do this in a terminal:
sudo visudo
Add a line something like:
myusername ALL = (root) NOPASSWD: /etc/init.d/snmpd
You may have to make a wrapping shell script (as sudo doesn't support argument security) if you want to use the service command instead of sudo /etc/init.d/snmpd.
try that:
Runtime.getRuntime().exec("$echo <password> | sudo -S service snmpd restart");
It's a work solution although it's not the best solution in matter of security, because the password can be read by anyone that have access to JAR File.
My suggestion has nothing to do with programming. Just modify your Sudoers file to allow users of your program to run the desired commands with NOPASSWD.
For a generic solution:
MY_APP_USERS MY_APP_HOSTS= NOPASSWD: MY_APP_CMDS.
When, the user tom (Part of MY_APP_USERS) runs sudo service snmpd restart (Part of MY_APP_CMDS) in one of the MY_APP_HOSTS he will be granted permission without using a password.
And a specific solution (without Aliases):
# tom will be able to run sudo /usr/sbin/service snmpd restart at userver
tom userver=(root) NOPASSWD: /usr/sbin/service snmpd restart

Categories