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.

AshwinK
  • 1,039
  • 1
  • 12
  • 31
  • 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). – Chris Maes Mar 19 '19 at 09: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) – AshwinK Mar 19 '19 at 10:54

2 Answers2

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 &
xenoson
  • 163
  • 3
  • It helped to keep the process running, but no logs are getting printed in `datagenerate.log` – AshwinK Mar 20 '19 at 04:24
  • Do you get a nohup.out? Because this way it only captures stdout, not stderr to datagenerate.log. – xenoson Mar 20 '19 at 09:22
  • I'm not getting nohup.out. In `datagenerate.log` it's just printing `Total iterations are 500` but not the output & last line` echo`. – AshwinK Mar 20 '19 at 09:33
  • There is no error in the bash code. It runs for me and prints expected output for 500 iterations. – xenoson Mar 20 '19 at 10:03
  • 1
    thanks, it's working. Actually there was some extra code in my bash, I fixed it. – AshwinK Mar 20 '19 at 10:09
  • You can quickly exchange long running process for a printf to check if the rest is working ok: Here, https://ideone.com/Hwza3I – xenoson Mar 20 '19 at 10:17
  • `nohup ./datagenerate.sh > datagenerate.log &` will this work with ubuntu SSH EXEC?? @xenoson – AATHITH RAJENDRAN Feb 19 '20 at 10:24
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.

Bruh
  • 16
  • 2