Find the answer to your Linux question:
Results 1 to 7 of 7
I would like to know how to start a bash script before user login in Debian, with "root" permission. I think that it has something to do with daemons no?...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Feb 2013
    Posts
    5

    Daemonize a bash Script


    I would like to know how to start a bash script before user login in Debian, with "root" permission.
    I think that it has something to do with daemons no?

  2. #2
    Linux Engineer Kieren's Avatar
    Join Date
    Aug 2007
    Location
    England
    Posts
    848
    You need to create a bash script and save it in the /etc/init.d/ folder. You will need to be root to do this.

    An example script will look something like this:

    Code:
    #! /bin/sh
    #
    ### BEGIN INIT INFO
    # Provides:          scriptname
    # Required-Start:    $all
    # Required-Stop:     $all
    # Default-Start:     2 3 4 5
    # Default-Stop:      0 1 6
    # Short-Description: Short description of your script
    # Description:       Slightly longer desciption of what your script does
    ### END INIT INFO
    
    case "$1" in
      start)
    	echo -n "Put the code you want to execute when the system starts here"	
    	;;
      stop)
    	echo -n "Put the code you want to execute when the system stops here"	
    	;;
    esac
    For more properties check out LSBInitScripts - Debian Wiki

    When the script has been saved you need to make sure it's executable (change myscript to whatever you called your file):

    chmod 755 /etc/init.d/myscript
    Finally you need to create symbolic links to put your file in the correct location. Luckily there is a tool that will do this for you (again change myscript to whatever your file is called):

    update-rc.d myscript defaults
    And to remove the symbolic links you can use the command

    update-rc.d -f myscript remove
    Last edited by Kieren; 02-06-2013 at 01:16 PM.
    Linux User #453176

  3. #3
    Linux Engineer
    Join Date
    Apr 2012
    Location
    Virginia, USA
    Posts
    893
    Here's a tutorial I found:
    Linux Daemon Writing HOWTO

  4. #4
    Just Joined!
    Join Date
    Feb 2013
    Posts
    5
    I will check this out. Thank you really much

  5. #5
    Just Joined!
    Join Date
    Mar 2007
    Location
    Melbourne, Australia
    Posts
    30
    If you are sure that your script is absolutely free of bugs and you have verified all the states of the script, then you can also use /etc/inittab where restarting of the script is handled automatically for you if the script terminates for whatever reason! I've created several scripts this way; the scripts are critical to the monitoring a production web site so can't have them terminating without an option to have them automatically restarted!

    Cheers,
    ak.

  6. #6
    Just Joined!
    Join Date
    Feb 2013
    Posts
    5
    Thanks tk2766!

  7. #7
    Just Joined!
    Join Date
    Feb 2013
    Posts
    5
    OK! I found the solution following Kieren post :
    So, things look like this :
    2 file, one for the Daemon which will start AND stop (and something else if you want), "My_Daemon_initScript" and "my_Daemon_initScript.sh" as following :


    >>>>> file to put in the "/etc/init.d" folder : name "My_Daemon_initScript"

    #! /bin/sh

    ### BEGIN INIT INFO
    # Provides: My_Daemon_initScript <--- I'm not sure about what does it do!!
    # Required-Start: $all <--- your deamon will start after all other daemons started at boot
    # Required-Stop: $all <--- your daemon will stop before all other daemon when you stop your pc
    # Default-Start: 2 3 4 5 <--- see charpter 11.5 at www debian org/doc/manuals/debian-faq/ch-customizing.en.h t m l
    # Default-Stop: 0 1 6 <--- idem
    # Short-Description: What my daemon do <--- custumize it
    # Description: Longer description <--- custumize it
    ### END INIT INFO

    Daemon_ID=/var/run/myDaemonID.pid

    start_My_Daemon()
    {
    /usr/share/My_Daemon_Script.sh >/dev/null 2>&1 & exit 0;
    }

    case "$1" in
    start)
    if [ -f $Daemon_ID ]; then
    echo "My Daemon is already running!"
    else
    echo "Starting My Daemon"
    start_My_Daemon
    fi;
    exit 1;
    ;;
    stop)
    if [ -f $Daemon_ID ]; then
    echo "Stoping My Daemon"
    kill -KILL $(cat $Daemon_ID);
    rm -f $Daemon_ID;
    else
    echo "My Daemon already stoped!!"
    fi;
    exit 0;
    ;;
    *)
    echo "Usage: service My_Daemon_initScript {start|stop}"
    exit 1;
    ;;
    esac

    exit 0;

    >>>>>>>> Now we need to create the /usr/share/My_Daemon_Script.sh ---> this is an example, you can put your file anywhere else but don't forget to change the line in start_My_Deamon function of the /etc/inint.d folder.

    #! bin/bash

    echo $$ > /var/run/myDaemonID.pid <--- this is fondamental for this script to be killed by the init file ("service My_Daemon_initScript stop")

    while [0]; do
    anything you want
    sleep the_Number_Of_Sec
    done


    ________________________________
    Ok that's it. This did pretty what I wanted

    Once finished,
    -change doc properies : chmod 755 /etc/init.d/My_Daemon_initScript
    -use the "update-rc.d "My_Daemon_initScript" defaults" at root prompt (as said Kieren before. I can't post the page so search for man pages debian update-rc)

    Ok, so, it might be it. I hope that it will help and that nothing is missing.

    Thanks to everyone

    Julien Mehl Vettori

Posting Permissions

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