Thread: Write a service in Linux

  1. #16
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by MisterIO View Post
    Yes, I do "command > /dev/null &". Why would you want to have a daemon print its output to the console? Clearly, if you don't redirect the output, it screws things up.
    Note that the CONSOLE is different from a TTY. CONSOLE should not close, _ever_ (keeping in mind that if you don't have one, it still NEVER closes). In fact, it even stays alive after the kernel has shut down (if doing a shutdown -h now), and can still display messages. Having a daemon print to the console is done quite often.

    The reason that your jobs stop if you have started background jobs is that you started the jobs as a non-root user. If you start a job in background mode (either by & or by CTRL-Z and then bg) as root, init will adopt the job believing it to be a critical function.

  2. #17
    Registered User
    Join Date
    Nov 2008
    Posts
    75
    Quote Originally Posted by Kennedy View Post
    Note that the CONSOLE is different from a TTY. CONSOLE should not close, _ever_ (keeping in mind that if you don't have one, it still NEVER closes). In fact, it even stays alive after the kernel has shut down (if doing a shutdown -h now), and can still display messages. Having a daemon print to the console is done quite often.

    The reason that your jobs stop if you have started background jobs is that you started the jobs as a non-root user. If you start a job in background mode (either by & or by CTRL-Z and then bg) as root, init will adopt the job believing it to be a critical function.
    Yeah, I wasn't talking about consoles as in "CTRL+ALT+F1",etc., because those can never be closed and the post I was answering to was talking about closing the terminal.
    But what do you mean that the console stays alive after the kernel has shut down?
    Consoles stay alive even during the system is going to a halt, but when the kernel finally halts, no consoles remain.

  3. #18
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by MisterIO View Post
    Yeah, I wasn't talking about consoles as in "CTRL+ALT+F1",etc., because those can never be closed and the post I was answering to was talking about closing the terminal.
    But what do you mean that the console stays alive after the kernel has shut down?
    Consoles stay alive even during the system is going to a halt, but when the kernel finally halts, no consoles remain.
    ALT-F1 is a tty, as is ALT-F2 - ALT-F7 (F7 is mostly used for X<something>). THE console, however, is how the kernel talks to the world. There is a big difference between the console and a tty.

    As far as CTRL+ALT+F1, I'm assuming you are referring to "shelling" out of X<something>. That is still not the console and YES these can be closed. It only requires you editing /etc/inittab and commenting out the shells that you don't want to run, then send init a hang-up (killall -HUP init).

  4. #19
    Registered User
    Join Date
    Nov 2008
    Posts
    75
    Quote Originally Posted by Kennedy View Post
    ALT-F1 is a tty, as is ALT-F2 - ALT-F7 (F7 is mostly used for X<something>). THE console, however, is how the kernel talks to the world. There is a big difference between the console and a tty.

    As far as CTRL+ALT+F1, I'm assuming you are referring to "shelling" out of X<something>. That is still not the console and YES these can be closed. It only requires you editing /etc/inittab and commenting out the shells that you don't want to run, then send init a hang-up (killall -HUP init).
    I know that I can change their number, but that's different from closing them. It seems as if by "The Console" you mean simply the interface that the kernel uses to do output, but how is that related to the much simpler discussion above about how to deamonize a program?

  5. #20
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by MisterIO View Post
    I know that I can change their number, but that's different from closing them. It seems as if by "The Console" you mean simply the interface that the kernel uses to do output, but how is that related to the much simpler discussion above about how to deamonize a program?
    Because you asked the question
    Quote Originally Posted by MisterIO
    Why would you want to have a daemon print its output to the console?
    And the answer is because you may want to send messages to the console after the controlling tty has died.

    [EDIT]More info: check out the specs on /dev/console[/EDIT]

  6. #21
    Registered User
    Join Date
    Nov 2008
    Posts
    75
    Quote Originally Posted by Kennedy View Post
    And the answer is because you may want to send messages to the console after the controlling tty has died.
    Thanks for this.

  7. #22
    Registered User
    Join Date
    Jul 2009
    Posts
    61
    Thanks all!!! My daemon have to monitor other users and maybe logoff one of them,
    remove some files, restart the system and something like these.
    So I have to be root to do these. Am I wrong?!!! I have written this service for windows
    with WIN API. I want just know that is there any API like WIN in LIN to do this?


  8. #23
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by hosseinyounesi View Post
    Thanks all!!! My daemon have to monitor other users and maybe logoff one of them,
    remove some files, restart the system and something like these.
    So I have to be root to do these. Am I wrong?!!! I have written this service for windows
    with WIN API. I want just know that is there any API like WIN in LIN to do this?
    Since this is most easily done with a sequence of system commands, the normal and sane way to do this would be with a shell script, so there is no such API. Many linux daemons are (more and less complicated) bash shell (aka. the "command language interpreter") scripts that use system commands, which system commands are written in C. Indeed, the bash shell is derived from an older unix power tool called "the C shell", and many "system commands" are actually part of the shell -- they do not have their own executables.

    If you do a bit of research into basic system commands you will realize you do not have to write any new ones for this*, so don't bother using C -- it will just be a string of system(), popen(), etc. I would almost bet that you could learn enough bash to do this in less time than it would take to actually write an awkward and probably inefficient (since you must invoke the shell implicitly to do all that) C program. If you don't use system/exec() calls, this will take you years and ten of thousands of lines of code to re-write commands that already exist.

    Advanced Bash-Scripting Guide

    You can even ask questions here about that; if no one else will answer them, I will...there are some goofy things about the syntax, like it is whitespace sensitive and heavy on braces. But it has regular expressions, functions, arrays, etc.

    You're right, this is a root/sys admin task. So it's about the shell. The bash shell. You don't have to write anything in C, it's already written. I promise you, that is how the pros do it and that is what the shell is for.

    *check users logged on: use "who", check the time: use "date", to log a user out: use "kill" to kill all the processes with that user ID (maybe you want to send a message first with "write" or "wall"). To delete files: "rm". To wait, "sleep". To fork, "&". To take the output of a such a SHELL command (who, date) and put into an array for further manipulation analysis, for example, is simple in a SHELL script; to do it in C, a real pain in the
    Last edited by MK27; 07-22-2009 at 10:10 AM.
    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. #24
    Registered User
    Join Date
    Nov 2008
    Posts
    75
    Quote Originally Posted by MK27 View Post
    Since this is most easily done with a sequence of system commands, the normal and sane way to do this would be with a shell script, so there is no such API. Many linux daemons are (more and less complicated) bash shell (aka. the "command language interpreter") scripts that use system commands, which system commands are written in C. Indeed, the bash shell is derived from an older unix power tool called "the C shell", and many "system commands" are actually part of the shell -- they do not have their own executables.

    If you do a bit of research into basic system commands you will realize you do not have to write any new ones for this*, so don't bother using C -- it will just be a string of system(), popen(), etc. I would almost bet that you could learn enough bash to do this in less time than it would take to actually write an awkward and probably inefficient (since you must invoke the shell implicitly to do all that) C program. If you don't use system/exec() calls, this will take you years and ten of thousands of lines of code to re-write commands that already exist.

    Advanced Bash-Scripting Guide

    You can even ask questions here about that; if no one else will answer them, I will...there are some goofy things about the syntax, like it is whitespace sensitive and heavy on braces. But it has regular expressions, functions, arrays, etc.

    You're right, this is a root/sys admin task. So it's about the shell. The bash shell. You don't have to write anything in C, it's already written. I promise you, that is how the pros do it and that is what the shell is for.

    *check users logged on: use "who", check the time: use "date", to log a user out: use "kill" to kill all the processes with that user ID (maybe you want to send a message first with "write" or "wall"). To delete files: "rm". To wait, "sleep". To fork, "&". To take the output of a such a SHELL command (who, date) and put into an array for further manipulation analysis, for example, is simple in a SHELL script; to do it in C, a real pain in the
    I agree with you, unless he's doing that just for fun. In that case, he could simply take a look at those apps code.

  10. #25
    Registered User
    Join Date
    Jul 2009
    Posts
    61
    Maybe you are right
    I'm now reading about bash and linux command line. It seems that Bash is some thing more than it's name !!!!!

    Thank you for all

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Thinking of upgrading to linux...
    By Yarin in forum General Discussions
    Replies: 37
    Last Post: 07-24-2009, 11:40 AM
  2. running my program as service
    By daher in forum Windows Programming
    Replies: 5
    Last Post: 09-05-2008, 12:30 PM
  3. Windows service status checking
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 06-01-2008, 08:19 AM
  4. A question about windows programming
    By Hussain Hani in forum Windows Programming
    Replies: 16
    Last Post: 05-23-2007, 07:38 AM
  5. installing linux for the first time
    By Micko in forum Tech Board
    Replies: 9
    Last Post: 12-06-2004, 05:15 AM

Tags for this Thread