LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-12-2004, 05:46 AM   #1
sinoob
LQ Newbie
 
Registered: Feb 2004
Posts: 6

Rep: Reputation: 0
Programmed Logout


i want to exit or logout from my account by programming ,i.e., i want to logout automatically after a program is executed( the execution time is unknown or it depends). How can i program this....
thank you
 
Old 02-12-2004, 07:02 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Code:
#include <stdio.h>      /* For printing output       */
#include <signal.h>     /* for kill()                */
#include <unistd.h>     /* for getppid() and sleep() */
#include <sys/types.h>  /* for pid_t variable type   */

int main(int argc, char *argv[])
{
     int i;
     pid_t parent;

     /* To make this program exit from the shell it was
      * started from, we need to kill the parent
      * process of the process that is running this
      * program.
      *
      * Our parent process is the process (probably
      * a shell) that started this program.
      */

     /* get the process ID (PID) of our parent */
     parent = getppid();

     /* Countdown */
     printf("\nLogging out in 5 seconds: ");
     for (i = 5; i > 0; --i) {
	  fflush(stdout);
	  sleep(1);
	  printf("%d ", i);
     }
     fflush(stdout);
     printf("  GO!\n");
     sleep(1);

     /* Use USR1 signal, because the shell ignores the
      * QUIT and TERM.  As KILL and STOP are to strong
      * for this purpose, and QUIT or ABRT would cause
      * a core dump, we best chose USR1, USR2 or HUP
      */
     if (kill(parent, SIGHUP) < 0) {
	  /* If kill() returns -1, we were not able
	   * to kill the parent. So print an error
	   * message to stderr.
	   */
	  perror(argv[0]);  /* also mention program name */
	  return 1;
     }
     return 0;
}

Last edited by Hko; 02-12-2004 at 07:10 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
What is linux programmed in... frostbute669 Programming 5 11-14-2005 02:40 PM
dlink 614+ -> Can it be programmed?!?! lazlow69 Linux - Hardware 0 03-22-2004 10:27 PM
how was the first ever part of linux programmed? slakker Linux - General 1 05-01-2003 01:12 PM
Linux programmed with... chadb70 Linux - Newbie 2 03-11-2003 08:18 AM
Apache die after programmed task barDo Linux - Networking 0 02-07-2003 10:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration