LinuxQuestions.org
Review your favorite Linux distribution.
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 04-21-2005, 09:26 AM   #1
csfalcon
Member
 
Registered: Jun 2004
Location: MD
Distribution: Fedora Core
Posts: 269

Rep: Reputation: 31
output to screen from a deamon


Hi,

Is it possible to output to the screen where the deamon was started from? How can it be done?

For example, if the deamon ran into an error I want to be able to inform the user something went wrong.
 
Old 04-21-2005, 09:43 AM   #2
masand
LQ Guru
 
Registered: May 2003
Location: INDIA
Distribution: Ubuntu, Solaris,CentOS
Posts: 5,522

Rep: Reputation: 69
try implementing

xmessage (this is utliity from x server package)

from that deamon

regards
 
Old 04-21-2005, 10:10 AM   #3
csfalcon
Member
 
Registered: Jun 2004
Location: MD
Distribution: Fedora Core
Posts: 269

Original Poster
Rep: Reputation: 31
I am actually looking for a way to do so in all UNIX platforms (AIX, HPUX, Solaris and Linux)
 
Old 04-21-2005, 10:38 AM   #4
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
You could make the daemon remember where it was started from with a bit of code. But the whole point of a daemon is to detach it from a terminal so wouldn't it be easier to just write errors to a log file like most daemons do?
 
Old 04-21-2005, 10:42 AM   #5
csfalcon
Member
 
Registered: Jun 2004
Location: MD
Distribution: Fedora Core
Posts: 269

Original Poster
Rep: Reputation: 31
yes, the deamon already has it's log files, but the "users" are not happy with checking the log files. they want to know by looking at the screen.

can you point me to the code or any howtos? thanks
 
Old 04-21-2005, 11:36 AM   #6
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
I'm trying to understand the logic here.

Are the users logging off and back on while the daemon runs? What if they log on the next time and they get a different terminal? Then your daemon will be printing messages to a terminal that's not logged in or even possibly printing the messages to someone else that logged in on that terminal.

And if they're not logging off then why daemonize the program? Just run it in the background.

If they have a terminal that's dedicates to just watching error messages (again, if they're logging off/on I think you'll have a syncronization issue with making sure they get that same terminal ID) then why don't they just tail -f the log file so it automatically shows the error as soon as it's logged?

In every scenario I can think of, what you're proposing to do just doesn't make sense to me.
 
Old 04-21-2005, 11:54 AM   #7
pfonseca
LQ Newbie
 
Registered: Mar 2004
Location: Lisbon, Portugal
Distribution: Debian
Posts: 9

Rep: Reputation: 0
Why not "tail" your logfile with the "-f" option ?

$ tail -f logfile.log

Regards
 
Old 04-21-2005, 12:12 PM   #8
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
More or less by definition, a daemon has no access to its terminal. And if you need to talk to the terminal, why run it in daemon mode?

Code below shows itsme86's idea about remembering the the terminal the daemon was started from. But note (as itsme86 pointed out as well) that the message may appear in the wrong terminal if the user who started the program already logged of.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <syslog.h>

/* The message to write to the terminal */
#define MESSAGE "Somethin...\n"

int main(int argc, char **argv)
{
    int fd;
    char *termfile;

    printf("Just started. I'm not a daemon yet\n");
    
    /* Remember the terminal device file we're on at this moment. */
    termfile = strdup(ttyname(1));
    if (termfile == NULL) { /* Catch just in case error. */
        fprintf(stderr, "Error: Out of memory while copying tty-name\n");
        return 1;
    }
    printf("My terminal device file is: %s\n", termfile);

    /* Daemonize */
    printf("Will try to run as a daemon now...\n");
    if (daemon(0,0) < 0) {
        perror(*argv);
        return 1;
    }
    syslog(LOG_INFO, "I'm a daemon now.");
    syslog(LOG_INFO, "I will try to write something to the terminal I was started from.");

    /* Open filedescriptor to the terminal file */
    fd = open(termfile, O_WRONLY | O_NOCTTY);
    if (fd < 0) {
        syslog(LOG_ERR, "Could not open %s. Exit..", termfile);
        return 2;
    }

    /* Write something to the terminal */
    if (write(fd, MESSAGE, strlen(MESSAGE)) < 0) {
        syslog(LOG_ERR, "Could not write to %s. Exit..", termfile);
        return 2;
    }

    syslog(LOG_INFO, "Success! I suppose the message is on the terminal now...");
    free(termfile);
    return 0;
}

Last edited by Hko; 04-21-2005 at 01:08 PM.
 
Old 04-21-2005, 12:25 PM   #9
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Quote:
Originally posted by pfonseca
Why not "tail" your logfile with the "-f" option ?

$ tail -f logfile.log

Regards
Isn't that what I said? o.O
 
Old 04-21-2005, 12:30 PM   #10
pfonseca
LQ Newbie
 
Registered: Mar 2004
Location: Lisbon, Portugal
Distribution: Debian
Posts: 9

Rep: Reputation: 0
Sorry itsme86, don't wanna step on your toes!
My "keyword parser" (aka eyes) skipped your message.

Credit where it's due: csfalcon, check itsme86 reply

regards
 
Old 04-22-2005, 02:14 AM   #11
V_Ganesh
LQ Newbie
 
Registered: Sep 2004
Location: India
Distribution: RH8,Fedora Core2,OpenBSD,Solaris
Posts: 4

Rep: Reputation: 0
Smile

Hi,

U can send ur messages from daemon to the machine's console. The associated device is /dev/console. Simply open the device using open system call and write the text u want to display. It will be displayed on the screen. But u need to have root permissions to do this !!!
 
  


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
standard output to screen and ffile hypexr Linux - General 2 10-27-2005 10:10 AM
dual screen/ tv output - help please Enoon SUSE / openSUSE 1 10-06-2005 02:17 PM
Can not send cron output to the screen tryingHarder Linux - Newbie 3 09-26-2004 10:11 PM
redirecting cpio screen output to file rawii Solaris / OpenSolaris 1 02-11-2004 09:28 AM
Log Screen Output to a file sdandeker Linux - Newbie 3 09-17-2003 02:57 AM

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

All times are GMT -5. The time now is 02:27 AM.

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