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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
09-23-2001, 03:44 PM
|
#1
|
LQ Newbie
Registered: Sep 2001
Distribution: Flyinglinux
Posts: 12
Rep:
|
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
|
|
|
09-23-2001, 03:55 PM
|
#2
|
Senior Member
Registered: Apr 2001
Location: Perry, Iowa
Distribution: Mepis , Debian
Posts: 2,692
Rep:
|
when you call the program, include an '&' at the end.
|
|
|
09-24-2001, 09:14 AM
|
#3
|
LQ Newbie
Registered: Sep 2001
Distribution: Flyinglinux
Posts: 12
Original Poster
Rep:
|
Yes, but the '&' doesn't work on my program since I have made it myself =) .
|
|
|
09-24-2001, 09:45 AM
|
#4
|
Senior Member
Registered: Apr 2001
Location: Perry, Iowa
Distribution: Mepis , Debian
Posts: 2,692
Rep:
|
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.
|
|
|
09-24-2001, 04:35 PM
|
#5
|
LQ Newbie
Registered: Sep 2001
Distribution: Flyinglinux
Posts: 12
Original Poster
Rep:
|
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!
|
|
|
09-24-2001, 05:16 PM
|
#6
|
Moderator
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417
|
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.
|
|
|
09-24-2001, 08:04 PM
|
#7
|
LQ Newbie
Registered: Sep 2001
Posts: 21
Rep:
|
A daemon is a background process without a controlling terminal.
See Unix Programming FAQ
|
|
|
09-27-2001, 01:36 AM
|
#8
|
Senior Member
Registered: Jun 2001
Posts: 1,635
Rep:
|
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);
}
|
|
|
09-28-2001, 05:55 PM
|
#9
|
LQ Newbie
Registered: Sep 2001
Posts: 21
Rep:
|
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.
|
|
|
09-30-2001, 01:31 PM
|
#10
|
LQ Newbie
Registered: Sep 2001
Distribution: Flyinglinux
Posts: 12
Original Poster
Rep:
|
Thanx
Thanx, guys!
That Unix Programming FAQ is great!
|
|
|
All times are GMT -5. The time now is 05:09 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|