Link two processes Bash - bash

I have two command line programs, a client application and a server application it talks to.
The client depends on the server running in the background. I can launch both via:
java -jar server.jar & java -jar client.jar
When I kill the client, however, the server remains in the background. Is there a way to link the two so that if the client dies, the server dies?

Run the server job in the background and store process id using $!. Then run the client.
After the client exits, kill the server using the stored pid, like this:
java -jar server.jar &
server=$!
java -jar client.jar
kill $server
shorter: no need to store PID of the background process, there's only one
java -jar server.jar &
java -jar client.jar
kill $!

There is no way to link them, but you can manage the processes explicitly.
java -jar server.jar & server_pid=$!
java -jar client.jar
kill $server_pid
The server runs in the background, then the client runs in the foreground. The script blocks at this point, so when the client exits, the script proceeds with the next command, which kills the server.

Related

Start java in windows batch and kill it when timeout, need output java console to a file

In my batch script, I need to run java automation and kill the java process when timeout.
run java
loop to verify if java process exist, if still running during timeout then kill the process.
When I use start java, then the java execution won't output to the console file.
start java -Xms64m -Xmx1024m -cp my.jar 2>&1 >Console.txt
When I remove start, the console could be generated but it will pending at java command, it will not execute to the verify after.
java -Xms64m -Xmx1024m -cp my.jar 2>&1 >Console.txt
I just want to share I use another workaround to realize. What I am using is jenkins job, I use parallel step to get it worked. One job in parallel is to execute "java -Xms64m -Xmx1024m -cp my.jar 2>&1 >Console.txt", the other job is to execute "verify if java pid still running, if running then loop wait, when timeout, kill the pid".

Executing two programs concurrently

I have two programs I need to start in a concrete order:
./server
./client
I want to write a shell script that is running those two programs inside a for loop (script domain is benchmarking the application). For that reason I need to have the client call blocking and the server call async, but I also need to explicitly kill the server after the client has returned (so the server can be started fresh in the next iteration).
What is the easiest way to achieve this behavior?
server &
PID=$!
client
kill $PID
Perhaps start the server up and store its PID $!.
#!/bin/sh
./server & storepid="$!"
./client
kill "$storepid"

Running one command at a time in a shell script

I am using Mac OS and zsh. I am running a shell script that launches several Java programs. They terminate once they have created their output (they are essentially scripts). However, it seems that my current script starts all the Java programs at once, which is very resource-intensive.
Currently my shell script looks like this:
java -Xmx2048M -jar gha.jar params1.yaml
java -Xmx2048M -jar gha.jar params2.yaml
java -Xmx2048M -jar gha.jar params3.yaml
When I run it, I run out of memory. How can I modify my script so that it only launches the next Java program once the first one has terminated, so that memory is refreshed in between?
You are mistaken. The shell will run these one-at-a-time. The only possible explanation is if these programs launch background processes, in which case the shell cannot know how to wait for them to complete.
As #bmargulies mentioned above your command as you have show it should run one command at a time.
Try modifying your shell script as as shown below. Using the construction below a command is run only if the previous command completes successfully (in theory at least).
java -Xmx2048M -jar gha.jar params1.yaml && java -Xmx2048M -jar gha.jar params2.yaml && java -Xmx2048M -jar gha.jar params3.yaml
If this does not work, run the script in the background and list out the currently running processes. Hopefully that'll give you an idea of what's running on your system.

Run java jar file on a server as background process

I need to run a java jar in server in order to communicate between two applications. I have written two shell scripts to run it, but once I start up that script I can't shut down / terminate the process. If I press ctrl+C or close the console, the server will shut down. Could anyone help me how to modify this script to run as a normal server?
#!/bin/sh
java -jar /web/server.jar
echo $!
#> startupApp.pid
You can try this:
#!/bin/sh
nohup java -jar /web/server.jar &
The & symbol, switches the program to run in the background.
The nohup utility makes the command passed as an argument run in the background even after you log out.
Systemd which now runs in the majority of distros
Step 1:
Find your user defined services mine was at /usr/lib/systemd/system/
Step 2:
Create a text file with your favorite text editor name it whatever_you_want.service
Step 3:
Put following
Template to the file whatever_you_want.service
[Unit]
Description=webserver Daemon
[Service]
ExecStart=/usr/bin/java -jar /web/server.jar
User=user
[Install]
WantedBy=multi-user.target
Step 4:
Run your service
as super user
$ systemctl start whatever_you_want.service # starts the service
$ systemctl enable whatever_you_want.service # auto starts the service
$ systemctl disable whatever_you_want.service # stops autostart
$ systemctl stop whatever_you_want.service # stops the service
$ systemctl restart whatever_you_want.service # restarts the service
If you're using Ubuntu and have "Upstart" (http://upstart.ubuntu.com/) you can try this:
Create /var/init/yourservice.conf
with the following content
description "Your Java Service"
author "You"
start on runlevel [3]
stop on shutdown
expect fork
script
cd /web
java -jar server.jar >/var/log/yourservice.log 2>&1
emit yourservice_running
end script
Now you can issue the service yourservice start and service yourservice stop commands. You can tail /var/log/yourservice.log to verify that it's working.
If you just want to run your jar from the console without it hogging the console window, you can just do:
java -jar /web/server.jar > /var/log/yourservice.log 2>&1
Run in background and add logs to log file using the following:
nohup java -jar /web/server.jar > log.log 2>&1 &

What's the nohup on Windows?

I want to run a Java jar file like this:
java -jar spider.jar
How to run it on the background on Windows?
Like this on Linux:
nohup java -jar spider.jar > /var/tmp/spider.log 2>&1 &
You could use the Windows start command:
start /min java -jar spider.jar
This command is not really the same as nohup; but it might be suitable if you're happy with the Java process running in a separate minimised window. See http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds.mspx
On Windows it's not normal that a process terminates once its parent was killed (like Unix-likes do it normally). Therefore there is no direct necessity for something like nohup. If you want to avoid the console window associated with it, you can use javaw but redirection won't work, then.
The only way to get the nohup behavior, where the process still runs after logging off (like for micro-services, analytic tools, server batch jobs etc.), is to run the .bat file with the start javaw -jar ... contents as either a service or a scheduled task.
save the following code to nohup.vbs, and add the directory to PATH.
Set args=Wscript.Arguments
Set ws=CreateObject("wscript.shell")
ws.Run args(0),0,true
then, run:
nohup "java -jar spider.jar > /var/tmp/spider.log"
Note that you should quote the full commands and the redirects.
For windows run following mentioned command in Command Prompt or in Terminal
nohup (file need to run) > (File you need to save the log) 2>&1
Ex:
nohup ./bin/windows/kafka-server-start.bat config/server.properties > ./MyKafka.log 2>&1

Resources