Ubuntu: nohup command not able print the output to nohup.out


I wrote a bash script to execute my jar file in ubuntu or Red Hat environment. Here is my bash script

#!/bin/bash  JAVA_HOME=/usr/lib/java/jdk1.8.0_131  $nohup java -jar /home/chikin/Downloads/hardcoded.jar & tail -f nohup.out  

My bash script will execute the jar that i had developed, i hope to print out the output from the jar file into nohup.out or any txt file,so i can trace out any problem easily but it's seem like no printing out anything when i execute the script, it's print out all the output in terminal. But when i execute nohup java -jar /home/chikin/Downloads/hardcoded.jar & directly from the terminal, it's able to print all the jar output into nohup.out. May i know why my script not working ? My script also not able to trigger every time i reboot my ubuntu machine, i tested many of the solutions but none of them working well


Unless you have exported the value of nohup into the script's environment (e.g. export nohup=/usr/bin/nohup) then $nohup will evaluate to the empty string - hence your command

$nohup java -jar /home/chikin/Downloads/hardcoded.jar  

becomes plain

java -jar /home/chikin/Downloads/hardcoded.jar  

so nohup is not being invoked at all. You probably want

nohup java -jar /home/chikin/Downloads/hardcoded.jar & tail -f nohup.out  

(without the $).

Probably it's better to make the output file explicit:

nohup java -jar /home/chikin/Downloads/hardcoded.jar > nohup.out & tail -f nohup.out  

since as noted in the manpage, the rules for the output file depend on whether standard output is a terminal or not:

If standard output is a terminal, append output to 'nohup.out' if  pos‐  sible,  '$HOME/nohup.out'  otherwise.  If standard error is a terminal,  redirect it to standard output.  To save output  to  FILE,  use  'nohup  COMMAND > FILE'.  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »