LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-18-2002, 07:13 PM   #1
jkcunningham
Member
 
Registered: May 2002
Location: Seattle, WA, USA
Distribution: Gentoo
Posts: 136

Rep: Reputation: 15
How do I launch a daemon only once?


I am currently launching fetchmail on a machine on which I normally log in locally. Originally, I put the command to launch the daemon in my ~/.bashrc file, but once I realized that every time I launched another terminal window it was running another copy of the daemon, I tried to figure out where to put it so it would only be launched once when I logged in. I settled on ~/.bashrc_profile. Locally, this seems to work just fine.

The problem is, if I log on to this machine from elsewhere, it starts up another copy of the daemon.

How do I set this up so it only launches the fetchmail daemon when I'm logged on locally, and only once, because presumably I could log on multiple times locally and I still don't want multiple daemons running.

Thanks.
 
Old 10-18-2002, 08:31 PM   #2
adam_boz
Member
 
Registered: Jul 2002
Location: Santa Cruz, CA
Distribution: lfs
Posts: 538

Rep: Reputation: 30
What I do is put it in the booscripts.... but that might not work for your situation.

EDIT:

you might also be able to incorperate some kind of check to see if fetchmail is running before starting the daemon.... maybe something like (psudo):

if
ps -e | grep fetchmail = no
then start
else
don't start

p.s.- I went psudo b/c I can't think of how to do it correctly :-)

Last edited by adam_boz; 10-18-2002 at 08:34 PM.
 
Old 10-18-2002, 10:06 PM   #3
adam_boz
Member
 
Registered: Jul 2002
Location: Santa Cruz, CA
Distribution: lfs
Posts: 538

Rep: Reputation: 30
ooh, I just got another idea! If you have it in you ~/.bashrc or something, when you start it up, also do a "touch ~/.fetchmail_is_running" ... kindof like your own lock file. then you can check if that is there, so you don't have to deal with the "ps" command. then, in you ~/.bash_logout file you can put something like "rm ~/.fetchmail_is_running"

maybe something like this:
in ~/.bash_profile:

if [ ! -e ~/.fetchmail_is_on ] ; then
sendmail -k
touch /root/.fetchmail_is_on
fi

then in ~/.bash_logout :

rm ~/.fetchmail_is_on

try that out... it should do the job


EDIT:

just another thought....

you might want to put that file in a world readable place, or /tmp or something, then put the script to delete it in /etc/rc.d/rc6.d/ named K05rm_fetchmail_is_on (don't forget to make it executable)

this way, if you log on then log off, the file won't be deleted...

good luck

-Adam



Last edited by adam_boz; 10-18-2002 at 10:23 PM.
 
Old 10-18-2002, 10:15 PM   #4
jkcunningham
Member
 
Registered: May 2002
Location: Seattle, WA, USA
Distribution: Gentoo
Posts: 136

Original Poster
Rep: Reputation: 15
Cool idea. I thought of one problem though. If there's a power failure, the file would prevent restarting fetchmail until I removed it by hand.

I think your other idea of doing a ps -A | grep fetchmail is going to work. I am trying to figure out how to get bash to evaluate and test the return of that command now.

Thanks.
 
Old 10-18-2002, 10:32 PM   #5
jkcunningham
Member
 
Registered: May 2002
Location: Seattle, WA, USA
Distribution: Gentoo
Posts: 136

Original Poster
Rep: Reputation: 15
Hey, your idea works great! Here's what I used:

if [ -z "$(ps -A | grep fetchmail)" ]; then
/usr/bin/fetchmail
fi

Thanks
 
Old 11-26-2002, 06:22 PM   #6
realos
Member
 
Registered: Jul 2002
Location: Germany
Distribution: Redhat 7.3, Debian 3.1, Knoppix 3.1, Ubuntu 6.10
Posts: 113

Rep: Reputation: 15
-z str is true if length of str is zero, right?

hi jkcunningham & all!

you suggest:

if [ -z "$(ps -A | grep fetchmail)" ]; then
/usr/bin/fetchmail
fi

I think "-z str" delivers the value true if the length of str is equal to zero!!

right?

how should this string equal zero. I receive following when I check for "ps -ef | grep fetchmail":
realos 4273 1 0 01:14 ? 00:00:01 /usr/bin/fetchmail -d 180

As you see it is not zero. Am I doing some silly mistake?

thanks,
 
Old 11-26-2002, 07:05 PM   #7
adam_boz
Member
 
Registered: Jul 2002
Location: Santa Cruz, CA
Distribution: lfs
Posts: 538

Rep: Reputation: 30
It looks like your fetchmail is running in daemon mode ( -d 180 ... It's checking your mail every 180 seconds), so it will stay in the background.
 
Old 11-26-2002, 07:56 PM   #8
realos
Member
 
Registered: Jul 2002
Location: Germany
Distribution: Redhat 7.3, Debian 3.1, Knoppix 3.1, Ubuntu 6.10
Posts: 113

Rep: Reputation: 15
yes, that `s true but I started it manually. My question was about that "z" as an option. do you know why it should be there?
 
Old 11-26-2002, 09:33 PM   #9
adam_boz
Member
 
Registered: Jul 2002
Location: Santa Cruz, CA
Distribution: lfs
Posts: 538

Rep: Reputation: 30
The z is there because jkcunningham wanted to test if fetchmail was running on his system.

the stuff inside the [ ]'s is tested, and if it is returned true, then fetchmail will be run. The -z returns true if the "ps -A | grep fetchmail" doesn't return anything... that is... the string is empty.

ps -A | grep init

and

ps -A | grep someprog

the first one will return a string, which means that the -z will make the test return false, while the second one will return nothing, and the -z will make the test return true

You probably have a fetchmailrc file that tells it to start up in daemon mode.

here's more on test commands:

http://yz.kiev.ua/www/etc/bash.html
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Could not launch KDEInit smurcoch Slackware 2 05-18-2005 03:15 PM
Remote launch GUI from Daemon with nobody logged in? Merlin53 Linux - General 1 08-12-2004 09:03 PM
Where to put xscreensaver to launch as daemon chrisk5527 Linux - General 2 05-18-2004 11:01 PM
How do I launch KDevelop? jhirshon Linux - Newbie 5 04-30-2004 09:53 AM
Can't launch fluxbox ICO Linux - Software 4 02-09-2004 09:31 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:39 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration