LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Run my bash script as a daemon. (http://www.linuxquestions.org/questions/linux-newbie-8/run-my-bash-script-as-a-daemon-377669/)

jaimese 10-28-2005 04:42 AM

Run my bash script as a daemon.
 
Hi guys!

I have finished my script and I need to run it as a daemon, in background in order to keep it running while Iīm doing other tasks.

I explain u the situation. I have an script written in bash that manage telnet connections to monitorize a network. It works nice but I need to run it in background. I want to pass all the echos from the screen to a log file for debugging purposes. I have tried to use the usual structure to make daemon copying it from the dhcpd service for example, but it doesnīt work.
It seems to work but when it start, Isee the line running script..., but it stops there until the script is finished. Since is a loop script, this doesnīt happen never...

I also have tried to start the script finished with & and it works, but I donīt recover the console until it has finished.

Iīm connecting with the machine througt a telnet connection, so I donīt have multiple consoles to change it.

I will thank u any idea.

Jaime.

scuzzman 10-28-2005 05:10 AM

You should be able to use the ampersand (&) to background it - you may need to press enter...
Or, try using screen:
Code:

man screen

spaniel 10-28-2005 05:51 AM

Use nohup
 
You could try the following:

nohup <script> &

This way the script keeps on running even when you log out or press control-C in the session.

Note that it will create a file nohup.out in the directory where you start nohup!

see "man nohup" or "info nohup" for details on the nohup command.

alienDog 10-28-2005 06:11 AM

To have it go the the background automatically (without & on the commandline) you could do something like:

if [ "x$1" != "x--" ]; then
$0 -- 1> /dev/null 2> /dev/null &
exit 0
fi

inside the script. This will put start another instance of the script in the background if the first parameter is not -- and exit.
You will of course need to edit it a bit if you need to pass parameters to your script.

Dtsazza 10-28-2005 06:55 AM

A minor note: While alienDog's solution works as intended, for any non-trivial program, you probably wouldn't want to lose the output. It would be much better to redirect the streams to a file instead of /dev/null, so that if there was an error, you could see what it was up to. Something like
Code:

if [ "x$1" != "x--" ]; then
$0 -- 1> ~/myscript.out.log 2> ~/myscript.err.log &
exit 0
fi

which will create two files in your home directory where the output will be logged. As it stands, this will empty the logs every time the script is run, which may be what you want - but if you'd rather keep all the generated output (and delete the contents manually when it gets too big) replace the single '>'s with double '>>'s, telling the script to append to the file rather than overwrite.

alienDog 10-28-2005 07:12 AM

Dtsazza: Right you are, silly me for not reading his original post properly ;)

jaimese 10-30-2005 06:47 PM

Thank very much!..

I've found a solution but I think your is better, so I will try it.

Bye

bulislaw 05-25-2008 03:08 AM

Hi,
could anybody explain me why in the test its "x" added ??
if [ "x$1" != "x--" ]; then

why not just:

if [ "$1" != "--" ]; then

PMorph 05-25-2008 03:46 AM

Its a trick to avoid problems when the parameter is not given.

bulislaw 05-25-2008 11:37 AM

thx - good to know

jonaskoelker 05-25-2008 12:21 PM

When is the "x$1" = "" necessary? Both bash and sh seems to handle `test "" = ""' just fine. I think it's only necessary when you don't have the quotes. Can anyone comment?

roybal 09-30-2009 10:17 AM

use a background process
 
doservice () {
# do the real processing here
sleep 10
}

doservice &
# main thread of execution dies here.

j13ett5 02-10-2011 03:28 PM

Quote:

Originally Posted by jonaskoelker (Post 3164206)
When is the "x$1" = "" necessary? Both bash and sh seems to handle `test "" = ""' just fine. I think it's only necessary when you don't have the quotes. Can anyone comment?

"x$1" = "" is never true, I'm assuming you mean "x$1" = "x" (or similar)

It's not required in bash scripts, but some other environments may require it to protect against the case where the string being tested starts with a special character.

the forms
Code:

! test "$1"
and
Code:

test -z "$1"
have the same effect as
Code:

test "$1" == ""
test counts the number of arguments and treats the case with a single argument as a special case


All times are GMT -5. The time now is 03:28 AM.