Re: help in linux startup process

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, 7 Jan 2004, mara ailiez sy wrote:

> elo... im currently doing my thesis and i want to ask if there is a tutorial available on how to create a startup background process in linux... hope someone could help me out tnx...=)
>
Don't know if there is a tutorial but its pretty easy.

>From shell you use &:

	$ someprog &

Course then its still tied to your tty, so then you add nohup:

	$ nohup someprog &

In all cases the pid of the process is in $! (if I recall correctly 
check the bash man page).

Programatically, you use fork, exec, setsid.  In perl it looks like this:

	use POSIX qw(setsid);

	$pid = fork();
	if($pid < 0) {
		die "Could not fork()!";
	}
	if($pid == 0) {
		#
		# This is child code
		# Dissassociate from terminal...
		setsid()

		#
		# Either close stdout, stderr and stdin
		# or send them to /dev/null (this very important).
		open(STDOUT, ">&/dev/null");
		open(STDERR, ">&/dev/null");
		open(STDIN, "<&/dev/null);

		#
		# Exec the new command (you only need this if you
		# want to load some other executable).
		exec('someprog');
		#
		# This won't return
	}

	#
	# Parent code can now do anything it wants.  The process
	# of the backgrounded task is in $pid.
	...

There are other nuiances, but a good start is reading the 
man pages on fork, exec, and setsid (for instance, things like
signal handling for your backgrounded process).  The example above
was in perl, but the same system calls exist in C, and the code would
look mighty similar.

Cheers...james
> 
> ---------------------------------
> Do you Yahoo!?
> Yahoo! Hotjobs: Enter the "Signing Bonus" Sweepstakes


_______________________________________________
Redhat-devel-list mailing list
Redhat-devel-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/redhat-devel-list

[Index of Archives]     [Kernel Newbies]     [Red Hat General]     [Fedora]     [Red Hat Install]     [Linux Kernel Development]     [Yosemite News]

  Powered by Linux