AshwinK
AshwinK

Reputation: 1088

Run bash script loop in background which will write result of jar command to file

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.

Upvotes: 0

Views: 1550

Answers (2)

xenoson
xenoson

Reputation: 163

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 &

Upvotes: 1

Bruh
Bruh

Reputation: 16

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.

Upvotes: 0

Related Questions