First C Program In Linux



1. Open your terminal window using
           Ctrl + Alt + T

2. Make directory for all c program you going to do
          mkdir c_program

3. Go to C_program directory and create directory for first program
          cd c_program
        mkdir first_program

4. Go to the first_program and create the file to write your program
         cd first_program
         gedit first_prog.c

5. It will open the editor to enter the program, enter the following code in the       editor

       #include <stdio.h>
       
        int main(int argc, char *argv[])
        {
                printf("\n This Is My First C Program Under Linux\n");
                return 0;
        }
6.
 6. Save and exit from the editor

 7. Compile the program with gcc compiler, format of the compilation should be  gcc -o <output_file_name> <your_program.c>
         gcc -o output first_prog.c

8. run the output
         ./output
9. You will get following as the output
       This Is My First C Program Under Linux

10. Enjoy!



Run Root Application Without Entering The Password


DESCRIPTION:

    Run <sudo mysql> command from terminal. It will ask password to enter from user. By using following procedure you no need to type password still it will run as root.

PROCEDURE:
  • Enter <sudo visudo>  terminal. It will open a file with your default editor
  • Add following lines to the file which opened by previous command which you entered
       
       <username> ALL=NOPASSWD: <path_to_application>

       ex: sujin ALL=NOPASSWD: /usr/bin/mysql
  • Save the file
  • Now run <sudo mysql> from terminal. It will not ask password to enter.
  • Enjoy!!



Remove Root Permission Of Any File

DESCRIPTION:

  • Following command will remove the root permission of file which you giving in file name

COMMAND:

          sudo chown -R $LOGNAME <filename>

         Ex: sudo chown -R $LOGNAME /home/sujin/Documents/myfile.sh


Procedure To Run An Application As Daemon In Linux


DESCRIPTION:

  • I wrote a simple server program in C. It monitor my client application.
  • Client application need to send acknowledge to server continuously with out any help from user.
  • So i making my client application runs in background and it should start at boot time.
PROCEDURE:
  • My client application name is client_ack. Copy this file to bin directory
         sudo cp client_ack /bin/client_ack
  • Go to the /etc/init.d/ directory and create a file and 
         cd /etc/init.d
         sudo vi client_d
  • Write the following script into file

         #!/bin/sh
         ### BEGIN INIT INFO
         # Provides:          client_d
         # Required-Start:    $network $remote_fs $syslog
         # Required-Stop:     $network $remote_fs $syslog
         # Should-Start:      network-manager
         # Should-Stop:       network-manager
         # X-Start-Before:    $x-display-manager gdm kdm xdm wdm 
                              ldm sdm nodm
         # X-Interactive:     true
         # Default-Start:     2 3 4 5
         # Default-Stop:      0 1 6
         # Short-Description: server client application
         ### END INIT INFO

         USAGE="usage: $0 {start|stop}";    
         usage() {
             echo $USAGE >&2
         }     
    
         ss_start() {
             cd /bin
             rm -r nohup.out
             exec nohup ./client_ack &
             sleep 3
             echo ""
         }
     
         ss_stop() {
             killall -9 client_ack
         }

         case $1 in
             start) ss_start ;;
             stop) ss_stop ;;
             *) usage
             exit 1
             ;;
         esac
  • Give executable permission to client_d file
         chmod +x client_d
  • Create symbolic link for start up using following command
         sudo update-rc.d client_d defaults
  • Restart the PC
  • You can check the client_d process running by using following command
         ps aux | grep client_d
  • You can Stop and Start the client_d manually by using following commands
        TO START : sudo service client_d start
        TO STOP  : sudo service client_d stop
  • Now my server getting acknowledgement message from client without any help from user and its start at boot time.