2

test.sh in /root/folder

#!/bin/bash
echo "Hello" #Sample Code

Now in th same folder(/root/folder). I have created nohup_test.sh

#!/bin/bash
while true
do
    sh /root/folder/test.sh
    sleep 1
done

If ran the above file like this nohup sh nohup_test.sh &. It will run in background and creates nohup.out have the output of test.sh which we can tail -f nohup.out

Now I want the above script nohup_test.sh to run start startup(automatically run at startup of OS).

I have created another file start.sh in same directory (/root/folder/)

#!/bin/bash
nohup sh /root/folder/nohup_test.sh &

I followed one procedure HERE.

  vi /etc/rc.local

Add the entry like below

# This script is executed at the end of each multiuser runlevel
/root/folder/start.sh || exit 1   # Added by me
exit 0

I think script is starting(Checked with ps aux | grep "nohup").But I didn't get the nohup.out file. So, how can I get that file nohup.out or I have to follow any procedure? Thanks!

1 Answers1

3

Add cd /root/folder at the top of start.sh. Never trust the current directory in startup jobs.

BTW, this is overly complex. You do not need nohup for jobs that are not started by a terminal; so you can use in /etc/rc.local

/root/folder/nohup_test.sh > /root/folder/my_output_file.log 2>&1 & 

and be done.

Rmano
  • 32,167