0

I'm novice to running bash script. (you can suggest me, if title I've given is incorrect.) I want to run a jar file using bash script in loop. Then it should write the output of jar command into some file. Bash file datagenerate.sh

#!/bin/bash
echo Total iterations are 500
for i in {1..500}
do
   the_output="$(java -jar data-generator.jar 10 1 mockData.csv data_200GB.csv)"
   echo $the_output
   echo Iteration  $i processed
done
no_of_lines="$(wc -l data_200GB.csv)"
echo "${no_of_lines}"

I'm running above script using command nohup sh datagenerate.sh > datagenerate.log &. As I want to run this script in background, so that even I log out from ssh it should keep running & output should go into datagenerate.log.

But when I ran above command and hit enter or close the terminal it ends the process. Only Total iterations are 500 is getting logged into output file.

Let me know what I'm missing. I followed following two links to create above shell script: link-1 & link2.

2
  • are you sure that your process is not continuing? you can run ps -axjf to view running processes. I think you java process is running but not printing anything (yet). Commented Mar 19, 2019 at 9:37
  • Yes.It's not continuing. I ran the command nohup sh datagenerate.sh > datagenerate.log & & after 2 seconds I hit enter, then it's showing process is Done. I checked whether it's running & result was it didn't show any process with that id. My java process is running & it's printing values.(I'm sure about it & verified as well) Commented Mar 19, 2019 at 10:54

2 Answers 2

1

nohup sh datagenerate.sh > datagenerate.log &

nohup should work this way without using screen program, but depending on your distro your sh shell might be linked to dash. Just make your script executable:

chmod +x datagenerate.sh

and run your command like this:

nohup ./datagenerate.sh > datagenerate.log &
Sign up to request clarification or add additional context in comments.

7 Comments

It helped to keep the process running, but no logs are getting printed in datagenerate.log
Do you get a nohup.out? Because this way it only captures stdout, not stderr to datagenerate.log.
I'm not getting nohup.out. In datagenerate.log it's just printing Total iterations are 500 but not the output & last line` echo`.
There is no error in the bash code. It runs for me and prints expected output for 500 iterations.
thanks, it's working. Actually there was some extra code in my bash, I fixed it.
|
0

You should check this out: https://linux.die.net/man/1/screen

With this programm you can close your shell while a command or script is still running. They will not be aborted and you can pick the session up again later.

1 Comment

This is again some new tool, I'm looking to get it done using simple bash script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.