Results 1 to 6 of 6
Is there a way to make a Shell Script run as a Deamon?
I need that in order to generate Random Network Traffic.
I already have the script. But how ...
- 10-25-2007 #1
- 10-25-2007 #2
Hi - You could add your script to /etc/rc.d/rc.local so it initiates at boot time, but be careful to give it the correct permissions / ownership. Really, a daemon isn't a script but a carefully written and compiled piece of software, written in C / C++.
You could also run your script as a Cron job at required times.I am always doing that which I can not do, in order that I may learn how to do it. - Pablo Picasso
- 10-25-2007 #3
It is generally rude to correct someone's spelling. I'd like to do that in this case, though, because it might help you in case you might want to use a search engine on this topic. It's "daemon", not "deamon".
If all you want to do is run this puppy without tying up a console, just take the script you already have and append an ampersand to the script name. So you would do this at the command line:
But if you want to make a genuine daemon out of this, it's slightly more complicated.Code:myscriptname &
It's common to write a daemon in C, of course, and it's possible to do so in Perl. But I'd never thought of doing it as a simple bash script. fingal is right that the daemon should be carefully written, but it can in fact be written as a bash script.
I googled and found this. In theory, knowledge of C is required if you want to understand this document. But for doing this in bash, you should read section 3 of the document. Section 4 can all be done with the following stuff wrapped around the guts of your script. I've tested it this morning, but you may wish to test it yourself. Substitute your own code for the colored parts.
Read section 5 and ignore section 6.Code:#!/bin/bash if [ "$1" != "setsidxxx" ] then # Don't forget the ampersand at the end of the following statement. /usr/bin/setsid $0 setsidxxx "$@" < /dev/null >> /tmp/mylogfile 2>> /tmp/mylogfile & exit fi shift umask 077 # or you might want to use umask 0 cd / while true do # Put your stuff here. # .... sleep 1 # if you want done
Hope this helps.
- 10-28-2007 #4Just Joined!
- Join Date
- Oct 2007
- Posts
- 1
Thanks for the post wje_lf, this is a big help. I tested your code on my system and it works as expected, however, it took me awhile to figure out the setsidxxx part. Clever.
At first I thought it was some special argument, but it actually prevents an infinite loop.
When you first call the script, setsidxxx is not supplied as an argument, the loop condition is met and the setsid call is made and the program exits, however when the script is submitted by setsid it has setsidxxx as it's first argument. The loop condition is not met in this case, which prevents the setsid call from spawning a new shell over and over again. Without it, the program keeps generating new sessions and exiting without the daemon code running.
Not easy to stop and no work gets done....
- 10-28-2007 #5
Robin? Is that you?
- 10-28-2007 #6


Reply With Quote
