Writing a service in Linux


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Writing a service in Linux
# 1  
Old 10-01-2002
Writing a service in Linux

Hi All:
I want to write a program that runs like a service(in the background) and should start up when the system boots. It should always be running, no matter who has logged in, no matter if anybody has logged in et all.
Is there any online help i could get on this topic, appreciate the help
Thanks,
Preetham.
# 2  
Old 10-01-2002
It's pretty simple to do in unix. No where near as complicated as it is in Win32.

You can execute your code with `nohup` (man nohup for more information) in the startup of your system. If need be, you should be able to sudo to any user you desire for this daemon for security purposes.

I use `nohup` with some network monitoring stuff that I do with tethereal and it works like a charm.
# 3  
Old 10-08-2002
I got u r question ... u mean that it should run in back ground for ever till the server is rebooted.
What u have to do is

define the service in a program set the program in such a way that it should become the group leader.

setsid=getpid() ; /* This will make u r service the leader of u r group u belong to */

i don't remember the whole syntax in detail u can go thru the daemon concepts and find it out.

u can run this by manually running the program or setting this particular program in u r cron job if u have any difficulties in cron please feel free to contact me at sivhard@rediffmail.com
# 4  
Old 10-08-2002
Why don't you just create a shell scripts and put it in /etc/rc.d/rc3.d and make a link over /etc/init.d/scripts-name. This should run when server boot up. (Linux)

Quan
# 5  
Old 10-24-2002
If your program is written in C, your program should use the daemon() library call to make your program a daemon. This will cause your program to close its standard input, output, and error, and fork() into the background.

You should then add a command to one of your boot scripts to cause your program to run. The location of the script to change differs between Linux distributions. If you are running Red Hat, you should edit /etc/rc.d/rc.local. The script you need to edit is started directly or indirectly from /etc/inittab.

my-daemon.c:
Code:
#include <unistd.h>
#include <err.h>

int main()
{
   if(daemon(0,0) == -1)
       err(1, NULL);

   while(1)
   {
      /* do stuff */
   }
}

/etc/rc.d/rc.local:
Code:
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

# Run my-daemon in the background
/usr/sbin/my-daemon

Your daemon will run until the system shuts down, or until it crashes. Also, if your daemon has a memory leak, the kernel will kill it when it uses too much RAM.

If you make your program a non-daemon that closes its standard input/output/err, you can start it directly from inittab, with the action field set to "respawn". In this case, if your program crashes, init will restart it.


my-service.c:
Code:
#include <unistd.h>

int main()
{
   close(0);
   close(1);
   close(2);
   chdir("/");

   while(1)
   {
      /* do stuff */
   }
}

/etc/inittab:
Code:
#
# inittab       This file describes how the INIT process should set up
#               the system in a certain run-level.
#

# [ snip for brevity ]

# Run only in runlevel 5. Init will kill my-service if
# another runlevel is selected. If it crashes quickly
# and repeatedly, init will stop restarting it for 5 minutes.

mine:5:respawn:/usr/sbin/my-service

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Hire Personal Assistant | SEO Virtual Assistant | Data Entry Service | Content Writing

Hello Everyone, Here I am offereing my Services Word Posting Content Posting PDF to excel or word Image to Ms Word typeing Document Creation Video Creation Any type Copy-Paste Ads Posting Article Posting Manual Directory Submission PR Submission Social Bookmarking Web 2.0 Blogs... (1 Reply)
Discussion started by: abseova
1 Replies

2. HP-UX

Writing from Unix DB to Linux NFS

I have an HP Unix server with Oracle DB and want to write Datapumo export files across the network to IMB/Linux NFS. Will that work? (3 Replies)
Discussion started by: Duane McDonough
3 Replies

3. Linux

Important Service Linux

Hi there; I need to know all the things about services which help my Linux OS running. So what're important services of LINUX OS ? Anyone can help me... Tks all ? (1 Reply)
Discussion started by: quan0509
1 Replies

4. Programming

Linux Service

Hi, I want to write a service like windows service for my rhel4u5 server.And this service every day at 4:00 am connect a ftp server and download file then parse information at txt file then insert or update info at my database server(oracle).and this service can be stopped or paused by user.And... (1 Reply)
Discussion started by: ariyurek
1 Replies

5. Programming

writing your own command in unix/linux

Hi I am very new to Linux programming,otherwise I have exposure to Linux. Was thinking about something like writing my own commands for Linux. Any ideas where to start, any useful links and what I need to know before I start with this. Thanks :) Sidhu (3 Replies)
Discussion started by: Amardeep
3 Replies

6. Programming

Basic questions on writing a Unix Service (newbie help!)

Hi there. I've got 12 years experience writing C++ on Windows, and 3 years C# on Windows. Now my boss wants me to write a C++ app to run on Unix as a multithreaded 'service' (i.e. a program that runs with no user intervention). Some quick questions for The Experts: * Whats the best C++... (3 Replies)
Discussion started by: Rutland Gizz
3 Replies

7. SuSE

writing on win ntfs partition from linux

Is it possible to save a file from linux environment to a windows ntfs partition? I use SUSE 8.3 and I can access win ntfs files only as read only. I want to know whether writing is possible on win ntfs partition. (2 Replies)
Discussion started by: suyashkunte
2 Replies

8. What is on Your Mind?

I'm writing a new Linux program!

Yep, that's right. I'm writing a Linux binary that requires an X11 Server. It will also be released in a Shell, Win32, and Cocoa (Mac OS X). It's a program that's a text editor and more. It not just creates TXT and RTF files, it also can save in XML, RSS, and a whole lot of other formats. ... (11 Replies)
Discussion started by: Danny_10
11 Replies

9. Programming

Writing a .NET service to be Hosted in UNIX

Has anyone here written an MS compatible .NET service that is hosted and served from a UNIX server instead of a W2K server? I love programming and writing .NET services with Visual Studio.NET and C#, but there is a fundamental problem --the Win2k server itself. It is not near as reliable as Sun... (0 Replies)
Discussion started by: yeslekmc
0 Replies
Login or Register to Ask a Question