Find the answer to your Linux question:
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 ...
  1. #1
    Linux Engineer RobinVossen's Avatar
    Join Date
    Aug 2007
    Location
    The Netherlands
    Posts
    1,422

    Shell Script as Deamon?

    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 can I make it a Deamon?
    Thanks.
    New Users, please read this..
    Google first, then ask..

  2. #2
    Linux Guru fingal's Avatar
    Join Date
    Jul 2003
    Location
    Birmingham - UK
    Posts
    1,539
    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

  3. #3
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    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:

    Code:
    myscriptname &
    But if you want to make a genuine daemon out of this, it's slightly more complicated.

    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.
    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
    Read section 5 and ignore section 6.

    Hope this helps.

  4. #4
    Just 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....

  5. #5
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Quote Originally Posted by mckman View Post
    Thanks for the post wje_lf, this is a big help.
    Robin? Is that you?

  6. #6
    Linux Engineer RobinVossen's Avatar
    Join Date
    Aug 2007
    Location
    The Netherlands
    Posts
    1,422
    No, Im not mckman :P
    But thanks alot. you helped me very much..
    Again!
    Thank you
    New Users, please read this..
    Google first, then ask..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...