Go Back   The macosxhints Forums > Working with OS X > OS X Developer



Reply
 
Thread Tools Rating: Thread Rating: 2 votes, 5.00 average. Display Modes
Old 08-27-2003, 01:19 PM   #1
jkp23
Triple-A Player
 
Join Date: Feb 2003
Location: back in t'ol' smoke :(
Posts: 225
Creating a custom Daemon with shell scripting/perl?

I need a little bit of a hand here trying to write a system daemon...

Basically, I have written a little shell script that can monitor if my internet connection goes down, and then reconnect if needed (See previous thread...). I know there are programs that do this, or alternatively you you could just set a periodic ping to (re)initiate the connection, but my ISP over here has an anoying authentication system, that means navigating to a web page and entering username and password.

anyway, I have battled that bit out, and written a lovely little script that uses curl to negociate receiving the cookies and sending the correct data etc, so now it is all fully automated...all i have to do is click a button to launch the app.

actually, i had gone one stage further than that, and Cron'd it to run every 5 minutes, but i am unsatisfied with that as a final solution.

what i really want to happen, is to make the script into a system daemon, that i can run using the startupitems functionality of the OS. The problem is, that i have tried this before in another situation (starting a prog from a shell script as as a startupitem) but the process remained atached to its parent process, and inherently left a shell proccess running. this i do not want.

I have done some research, and it seems that on most *nixs, you can do something called forking, though im not sure how exactly - and that will detach the process and leave it as an independant process. I have however also read some reports that shell scripts do not make good daemons, and that i should be thinking about rewritting it in perl for example, as an easier option than C.

So my question is, does anyone have experience creating such a custom daemon using either a shell script (preferably) or perl? Can you offer me any advice, or point me to any decent tutorials? Are the principles exactly the same on X or do I need to make some adaptions to the standard Unix procedures?

I am comfortable with creating the startup item, but it is the details of the actual process that i am creating that i am interested in...

Also as a side note - does anyone know if you can get PPPoE to dial before people log in? So for example, the computer boots, sits at the login screen, but the daemon tries to talk to the net...Will this trigger the PPPoE connection, therefore allowing the daemon to start its work and login without interference or a login?

I realise that there are a hell of a lot of questions in one go here...any help with this would be well appreciated...

thanks in advance people.
__________________
JKP.
jkp23 is offline   Reply With Quote
Old 08-27-2003, 04:07 PM   #2
hayne
Site Admin
 
Join Date: Jan 2002
Location: Montreal
Posts: 32,473
A "daemon" is usually used to mean a program that sits around doing nothing until some event occurs.
So what you are talking about would not be a daemon.

All you need to do is make it a standard Startup Item. There is at least one MacOSXHints article about how to do that. You could even make it a Login Item which is even easier - just use the Login Items preference panel to do this with any standard program.

As to the parent shell hanging around, why not just start your script in the background by using '&' when you invoke it?
E.g. instead of invoking your script in the Startup Item script as:
myscript
do this:
myscript &
to put it into the background. Then the parent shell will exit.
hayne is offline   Reply With Quote
Old 08-27-2003, 04:54 PM   #3
jkp23
Triple-A Player
 
Join Date: Feb 2003
Location: back in t'ol' smoke :(
Posts: 225
thanks for the response hayne...

so will appending the & to the end fork the process? also, how do I disable the standard out or standard err on a shell script? Basically, i dont want it trying to print to a terminal as im sure that will drive it crazy.

I have read some interesting stuff about creating a perl daemon, and to be honest it doesnt seem that hard. i know you didnt think it was a daemon, but i think it is, in that it is there to test if the connection is still up - apon failure, it will do its thing. i was going to run it on a loop with a sleep setting...

im still not sure if itsnt better to use perl, according to what i have read.

some urls that might interest you, if youd like to give me a further opinion:

Unix Daemons in Perl
Shell scripts as Daemons

well...im still not sure what im gonna do...gonna sit on it for a while and have a think.

thanks for your help
__________________
JKP.
jkp23 is offline   Reply With Quote
Old 08-27-2003, 07:07 PM   #4
hayne
Site Admin
 
Join Date: Jan 2002
Location: Montreal
Posts: 32,473
I/O redirection

You need to read up on I/O redirection in shells.
(it uses the '>' operator)
hayne is offline   Reply With Quote
Old 08-27-2003, 07:09 PM   #5
bluehz
MVP
 
Join Date: Jan 2002
Posts: 1,562
One thing you might want to look at is the awesome daemontools pkg. I use it too control a myriad of deamons on my Linux server. Basically it is like a watchdog that makes sure the processes are always running, restarts as necessary, etc. Although it has not been ported to OS X, I downloaded the source code and installed and it compiled and installed fine. I have not yet started writing daemons for my OS X box as I don't have that much need for them, but I have been meaning to play with it and was planning on doing a writeup for the main MacOSXHints site.

The daemontools pkg is located here:
http://cr.yp.to/daemontools.html

and the optional deamontools-conf, which makes setting up daemons a little easier is located here:
http://js.hu/package/daemontools-conf.html

Hope that helps!

FYI - the author of daemontools is known for his cut and dry, get to the point, absolutely secure software. I also have replaced my insecure Bind with his dnscache and tinydns. He's not too big on helping if you run into problems, but you can usually find help pretty easily.
bluehz is offline   Reply With Quote
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

Forum Jump



All times are GMT -5. The time now is 06:08 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.