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
  • 31,947
  • At the end, you mentioned 2>&1, can you please explain that. – Veerendra K Sep 21 '15 at 17:27
  • 1
    @Veerendra 2>&1 simply redirects sdterr to stdout, so that both output streams are saved in your nohup.out file. Otherwise it could probably create a nohup.err file for the other stream as well if needed. – Byte Commander Sep 21 '15 at 17:47
  • Thanks @ByteCommander, How can I limit the log size in this scenario? Can you please tell. Thanks – Veerendra K Sep 22 '15 at 09:42
  • @Veerendra I'm sorry, but I don't know. This is probably a bit complex. If you only want the size limited for viewing, you can use the less viewer for example, or the tail command. – Byte Commander Sep 22 '15 at 09:46