LinuxQuestions.org
Visit the LQ Articles and Editorials section
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 09-23-2001, 04:44 PM   #1
gurra
LQ Newbie
 
Registered: Sep 2001
Distribution: Flyinglinux
Posts: 12

Rep: Reputation: 0
background process


How do I make my program run as a background process?

I am trying to make a web-server but I don't want it to show in the console.

(I am programming in C)

Thanx
 
Old 09-23-2001, 04:55 PM   #2
rshaw
Senior Member
 
Registered: Apr 2001
Location: Perry, Iowa
Distribution: Mepis , Debian
Posts: 2,694

Rep: Reputation: 45
when you call the program, include an '&' at the end.

Code:
./myprog &
 
Old 09-24-2001, 10:14 AM   #3
gurra
LQ Newbie
 
Registered: Sep 2001
Distribution: Flyinglinux
Posts: 12

Original Poster
Rep: Reputation: 0
Yes, but the '&' doesn't work on my program since I have made it myself =) .
 
Old 09-24-2001, 10:45 AM   #4
rshaw
Senior Member
 
Registered: Apr 2001
Location: Perry, Iowa
Distribution: Mepis , Debian
Posts: 2,694

Rep: Reputation: 45
the fact that you wrote it isn't the issue, all software was written by someone. if you start this program(s) at the prompt then the '&' will push it to the background.
 
Old 09-24-2001, 05:35 PM   #5
gurra
LQ Newbie
 
Registered: Sep 2001
Distribution: Flyinglinux
Posts: 12

Original Poster
Rep: Reputation: 0
wow

yes, it really runs in the background!!!

I was misled by the output from the program
but now I see that it really runs as a background process.

thank you very much!
 
Old 09-24-2001, 06:16 PM   #6
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,414

Rep: Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967
but surely there is a way to do this properly, in code, rather than a fairly nasty command line adaption. Most daemon programs do return to the background once they've set themselves up, such as fetchmail, which runs it's initial check, and then threads into the background.
 
Old 09-24-2001, 09:04 PM   #7
pinoy
LQ Newbie
 
Registered: Sep 2001
Posts: 21

Rep: Reputation: 15
A daemon is a background process without a controlling terminal.
See Unix Programming FAQ
 
Old 09-27-2001, 02:36 AM   #8
isajera
Senior Member
 
Registered: Jun 2001
Location: San Antonio, TX
Distribution: distro? what's a distro?
Posts: 1,635

Rep: Reputation: 45
kewpie - i use this to deprioritize any process i don't want to hog the cpu - i think this might be what you were referring to. the higher the number the priority is set to, the less cpu it uses.

#include <sys/resources.h>
#include <unistd.h>

void Set_Process_Priority(void){

int prio;

prio = getpriority(PRIO_PROCESS, getpid());
setpriority(PRIO_PROCESS, getpid(), prio + 10);
}
 
Old 09-28-2001, 06:55 PM   #9
pinoy
LQ Newbie
 
Registered: Sep 2001
Posts: 21

Rep: Reputation: 15
Here you go.

Code:
int make_daemon()
{
    pid_t pid;
    if ((pid = fork() < 0) {
        perror("fork error\n");
        return 0;
    } else if (pid != 0)
        exit(0);

    setsid();
    chdir("/");
    umask(0);
    return 1;
}
There's a longer version that ensures it never gets a controlling terminal by double forking, but that should do for most unix.
 
Old 09-30-2001, 02:31 PM   #10
gurra
LQ Newbie
 
Registered: Sep 2001
Distribution: Flyinglinux
Posts: 12

Original Poster
Rep: Reputation: 0
Smile Thanx

Thanx, guys!

That Unix Programming FAQ is great!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
really background a process Henster Linux - Newbie 1 07-01-2005 06:29 AM
how to run any binary in background - background process tej Linux - Newbie 1 08-16-2004 01:27 AM
Background Process mojozoox Linux - General 1 08-26-2003 03:16 PM
Background process h8r Linux - Newbie 5 01-13-2003 05:21 PM
stdout for a background process GOLDF1NG3R Linux - General 3 11-14-2001 02:10 PM


All times are GMT -5. The time now is 11:55 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration