bash and nohup with infinite loop

Back with some exciting stuff again!

If you have tried to convert a bash script to daemon (which is pretty common use case) this post might be of little extra help to you!

Nohup doesn't by default like for loop in your script; take it or leave it that's the truth in my experience. Therefore something like (lets name this script test.sh):


#!/bin/bash

for i in 1 2 3 4 5 6 7 8
do
#your stuff
done
exit

running the above script will cause you hangups using nohup i.e. the following will likely not run:
nohup sh test.sh &

The way around this is dumping your for loop output to a file therefore
instead of the above contents for the shell script use:



#!/bin/bash

for i in 1 2 3 4 5 6 7 8
do
#your stuff
done >  out.log #or whatever you want to name this file
exit


This will have no issues running using nohup and you can make your script run as a daemon.

Questions? Comments? Post below!

Comments

  1. Thank you!!!

    I also discovered running the for loop in the back ground THEN ctrl + Z to pause and disown to disown the job worked for me as well as a nohup-hates-for-loop work around.

    ReplyDelete

Post a Comment

Popular Posts