Re: linux daemon process



ya it shows the name of process when i write 'ps -x'.. And the text
file gets created in the root directory itself where i have written
the C++ code.. I dont have much knowledge about whether it has the
necessary permissions for writing in the file. But when i run it
using ./process everythin goes fine. What sort of permissions r
required?? im sorry if i sound too ignorant..
The program shud keep on updating the time in the text file. according
to my program it checks for the new time every minute. Is there any
otherway to solve this task??

my C++ code is as follows..
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>
#include <iostream.h>
#include <fstream.h>
#include <time.h>

int main(void) {


pid_t pid, sid;


pid = fork();
if (pid < 0)
{
exit(EXIT_FAILURE);
}

/* If we got a good PID, then
we can exit the parent process. */

if (pid > 0)
{
exit(EXIT_SUCCESS);
}

/* Change the file mode mask */

umask(0);



/* Create a new SID for the child process */
sid = setsid();
if (sid < 0)
{
/* Log the failure */
exit(EXIT_FAILURE);
}



/* Change the current working directory */
if ((chdir("/")) < 0)
{
/* Log the failure */
exit(EXIT_FAILURE);
}

/* Close out the standard file descriptors */
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);

/* Daemon-specific initialization goes here */

/* The Big Loop */
//char myline[20];


while (1)
{
// sleep(10);
time_t now;
struct tm *ts;
char buf[80];

time(&now);

// Format and print the time, "ddd yyyy-mm-dd hh:mm:ss zzz"

ts = localtime(&now);
strftime(buf, sizeof(buf), " %H:%M", ts);
fstream file_out("/root/Akshay.txt",ios::app);
file_out<<buf;
//sleep(2);
// system("clear");

sleep(5);
//file_out.close();
}

exit(EXIT_SUCCESS);
}

thank you for paying attention to my doubts..
Regards akshay.
Jens Thoms Toerring wrote:
axo <akshay.tyagi2008@xxxxxxxxx> wrote:
Hey Jens its correct that daemon is a long running process.
My task requires me to write a process which shows the current system
time in a text file. The program should be such that the user should
not run it, it should start showing the time on its own in a text
file. So I thought that it should be a daemon process which starts on
its own..

A daemon doesn't start on it's own, it also needs to be started.
A daemon is just a program with no connection to any controlling
terminal, running in the background. If it appears in 'ps -x'
after you booted the machine then you must have somehow instruc-
ted the system to start this process. And does this program have
to write the system time to a file at regular intervals and if
yes at what intervals? If it's like every minute or every hour
or so then using a crom job that starts the program at constant
intervals might be preferable.

But the problem is that i have to execute the program using the ./
process command.. It doesnt show the output on its own..

You didn't answer the questions a) when and how does the program
get started and b), as Bill Marcum asked, has the program the
necessary permissions to write to the file when it's started not
from the command line under your account but by whatever method
you seem to be using? Since there's a lot of possibly relevant
information missing it's simply impossible to tell what's going
wrong...
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@xxxxxxxxxxx
\__________________________ http://toerring.de
.