LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 10-28-2005, 05:42 AM   #1
jaimese
LQ Newbie
 
Registered: Oct 2005
Posts: 11

Rep: Reputation: 0
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.
 
Old 10-28-2005, 06:10 AM   #2
scuzzman
Senior Member
 
Registered: May 2004
Location: Hilliard, Ohio, USA
Distribution: Slackware, Kubuntu
Posts: 1,851

Rep: Reputation: 47
You should be able to use the ampersand (&) to background it - you may need to press enter...
Or, try using screen:
Code:
man screen
 
Old 10-28-2005, 06:51 AM   #3
spaniel
Member
 
Registered: Mar 2005
Location: Netherlands
Distribution: Fedora Core 5 x86_64
Posts: 48

Rep: Reputation: 15
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.
 
Old 10-28-2005, 07:11 AM   #4
alienDog
Member
 
Registered: Apr 2004
Location: Europe
Distribution: Debian, Slackware
Posts: 505

Rep: Reputation: 46
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.
 
Old 10-28-2005, 07:55 AM   #5
Dtsazza
Member
 
Registered: Oct 2005
Location: Oxford, UK
Distribution: Debian Etch (w/ dual-boot XP for gaming)
Posts: 282

Rep: Reputation: 30
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.
 
Old 10-28-2005, 08:12 AM   #6
alienDog
Member
 
Registered: Apr 2004
Location: Europe
Distribution: Debian, Slackware
Posts: 505

Rep: Reputation: 46
Dtsazza: Right you are, silly me for not reading his original post properly
 
Old 10-30-2005, 07:47 PM   #7
jaimese
LQ Newbie
 
Registered: Oct 2005
Posts: 11

Original Poster
Rep: Reputation: 0
Thank very much!..

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

Bye
 
Old 05-25-2008, 04:08 AM   #8
bulislaw
LQ Newbie
 
Registered: May 2008
Location: Poland
Distribution: Ubuntu
Posts: 2

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

why not just:

if [ "$1" != "--" ]; then
 
Old 05-25-2008, 04:46 AM   #9
PMorph
Member
 
Registered: Sep 2003
Distribution: Debian
Posts: 213

Rep: Reputation: 31
Its a trick to avoid problems when the parameter is not given.
 
Old 05-25-2008, 12:37 PM   #10
bulislaw
LQ Newbie
 
Registered: May 2008
Location: Poland
Distribution: Ubuntu
Posts: 2

Rep: Reputation: 0
thx - good to know
 
Old 05-25-2008, 01:21 PM   #11
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 46
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?
 
Old 09-30-2009, 11:17 AM   #12
roybal
LQ Newbie
 
Registered: Sep 2009
Posts: 8
Blog Entries: 8

Rep: Reputation: 0
Thumbs up use a background process

doservice () {
# do the real processing here
sleep 10
}

doservice &
# main thread of execution dies here.
 
Old 02-10-2011, 04:28 PM   #13
j13ett5
LQ Newbie
 
Registered: Nov 2005
Distribution: debian
Posts: 13

Rep: Reputation: 1
Quote:
Originally Posted by jonaskoelker View Post
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
run-parts gives error on bash script ChoKamir Programming 7 08-18-2014 07:31 AM
bash script running as daemon Satriani Programming 12 03-29-2011 06:23 PM
how to run one bash script from within another? babag Programming 9 04-28-2005 01:12 AM
Run script at login instead of bash uzi4u Linux - General 2 04-28-2004 03:31 PM
How to run a perl script as a daemon ? paonethestar Programming 17 11-06-2003 09:53 PM


All times are GMT -5. The time now is 09:18 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration