0

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

0

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'.
  • any idea how to trigger my script after i reboot my machine ? i tested various method but none of it working, not sure i am misunderstanding any on-line available tutorial – attack Jun 22 '17 at 2:02
  • thanks for your guide, i am able to print my output into nohup.out – attack Jun 22 '17 at 2:05
  • @attack there are several places to run scripts (at boot; when the display manager starts; when a user logs in; when a user's desktop session starts) - the appropriate place will depend what your script does and what environment and resources it needs in order to function correctly – steeldriver Jun 22 '17 at 2:14
  • let me explain what my program will do, my program is a log file analysis program, i will use my script to trigger it to execute as a background service, so i cant wait until user log in – attack Jun 22 '17 at 2:46

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.