Add process to be run at boot

This is a discussion on Add process to be run at boot within the C Programming forums, part of the General Programming Boards category; I have a server program which I would like to add to my system boot. I tried to add it ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    96

    Add process to be run at boot

    I have a server program which I would like to add to my system boot. I tried to add it to '/etc/init.d/' and quickly found that this was not what I wanted (Thank you for a command line interface, haha). Where do I want to place this executable to be booted?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You need to learn about runlevels and runlevel scripts, which for many/most/all distros (for sure redhat & debian) are in /etc/rc?.d.

    Those are shell scripts run by init (process #1); you can just include a command-line invocation in the appropriate place -- perhaps rc.local, which may be in /etc/init.d. Don't put an actual program in that directory, just add a line to a script.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    96
    After reading on run-level scripts is it a run-level script that I want to run this as or is there another location where I want to put this server process? I may just be understanding you incorrectly
    Last edited by NuNn; 03-14-2009 at 02:10 PM.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by NuNn View Post
    After reading on run-level scripts is it a run-level script that I want to run this as or is there another location where I want to put this server process?
    You could make up a runlevel script:
    Code:
    !#/bin/bash
    
    /home/user/C/myserver
    give it a name, place it with the other scripts (probably in either /etc/rc.d or /etc/init.d), make sure to "chmod 755 myscript" and then place a softlink in the appropriate runlevel directory:

    ln -s /etc/init.d/myscript /etc/rc5.d/S50myscript

    for example. The link must be named "S" (for start) with a number (the priority) appended to the name of the script which exists in /etc/init.d (or /etc/rc.d, depending on the distro).

    Or, like I said, just add a line to an existing script (rc.local is a good one to use for this stuff).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,238
    Quote Originally Posted by MK27 View Post
    You could make up a runlevel script:
    Code:
    !#/bin/bash
    
    /home/user/C/myserver
    A typical runlevel script takes a parameter, which is usually "start" or "stop" (but might also support "reload", "status", "restart" and a few others, but "start" and "stop" are usually required). So a good runlevel script would look like:

    Code:
    #!/bin/bash
    
    case $1 in
        start)
            do_startup_stuff
            ;;
        stop)
            do_stopping_stuff
            ;;
    esac
    The part about linking it into /etc/rc.*/... is perfectly correct though.

    EDIT: Some weird Linux distros don't use this system, but most of them do.

    EDIT EDIT: The other problem is that runlevel scripts start the processes as root. Sometimes that is not what you want, and you have to play tricks.
    Last edited by brewbuck; 03-14-2009 at 02:28 PM.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    96
    Thank you MK/brewbuck that helps clear it up a bit for me. Another question is does it matter if I choose init.d or rc.d and if it is rc.d that I want I have a number of directories ranging from rc[1-5].d and one number rcS.d.

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,238
    Quote Originally Posted by NuNn View Post
    Thank you MK/brewbuck that helps clear it up a bit for me. Another question is does it matter if I choose init.d or rc.d and if it is rc.d that I want I have a number of directories ranging from rc[1-5].d and one number rcS.d.
    The standard is to place the script itself into /etc/init.d, and then symlink it into the appropriate rc.* directory.

    rc.3 is for normal system startup, and rc.6/rc.0 is for normal system shutdown. You probably want to link it into both of those locations. The "S50service" syntax is how you control the exact order in which services start up. Smaller numbers start/stop earlier, larger numbers do it later. If it doesn't matter (i.e. your service depends on no other service) then just pick a number.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by brewbuck View Post
    A typical runlevel script takes a parameter, which is usually "start" or "stop"
    True, but the simple method (no start or stop) will work.

    Just depends how interested you are in writing runlevel scripts.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,238
    Quote Originally Posted by MK27 View Post
    True, but the simple method (no start or stop) will work.

    Just depends how interested you are in writing runlevel scripts.
    Well, if you ignore the $1 parameter, then you need to write two scripts -- one for startup, one for shutdown. You could certainly do it that way, but maintaining two scripts is worse than maintaining one.

    If you don't write a specific shutdown script, then your program will simply get killed with SIGTERM (and then SIGKILL if it ignores it), which might be unclean. I don't think the case statement I demonstrated is particularly painful to implement.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  10. #10
    Registered User
    Join Date
    Nov 2007
    Posts
    96
    MK I tried your solution but when I restarted my system ubuntu never starts while the process is the only thing on the screen. How would I get Ubuntu to start normally and still have the process running; would I have to follow what brewbuck listed?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. One process with two console windows
    By siavoshkc in forum Windows Programming
    Replies: 8
    Last Post: 01-30-2009, 03:13 PM
  2. create a child process that creates a child process
    By cus in forum Linux Programming
    Replies: 9
    Last Post: 01-13-2009, 01:14 PM
  3. Vector vs. array.
    By matsp in forum C++ Programming
    Replies: 37
    Last Post: 06-23-2008, 12:41 PM
  4. App to run as service and normal process
    By knutso in forum Windows Programming
    Replies: 4
    Last Post: 12-04-2006, 11:12 AM
  5. freebsd and redhat dual boot.
    By xddxogm3 in forum Linux Programming
    Replies: 1
    Last Post: 05-09-2004, 06:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21