LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 01-09-2013, 01:53 AM   #1
seeLnd
LQ Newbie
 
Registered: May 2004
Location: Beijing, China
Distribution: Debian
Posts: 16

Rep: Reputation: 0
BASH - different setsid


I show the command first:
Code:
see@linux-ub7v:~> setsid sleep 5 & echo "$! setsid"; sleep 1; ps -eo pid,args | grep sleep | grep -v grep
12498 setsid
12502 sleep 5

see@linux-ub7v:~> killall sleep

see@linux-ub7v:~> bash -c 'setsid sleep 5 & echo "$! setsid"; sleep 1; ps -eo pid,args | grep sleep | grep -v grep'
12507 setsid
12507 sleep 5

see@linux-ub7v:~> setsid --version
setsid from util-linux 2.21.2
The first commands were executed by an interactive shell, we can see there are the different pid between setsid and sleep,
but the third commands, I put them into a non-interactive shell, and then, we can see there are the same pid of setsid and sleep.
I guess setsid in first command may be "fork -> exec sleep by child -> parent exit" and setsid in third command just do "exec sleep" directly.
If I am right, why does setsid have a different behavior between interactive shell and non-interactive shell ?

Thanks in advance!

Last edited by seeLnd; 01-09-2013 at 06:04 AM.
 
Old 01-29-2013, 12:11 AM   #2
Ztcoracat
LQ Guru
 
Registered: Dec 2011
Distribution: Slackware, MX 18
Posts: 9,484
Blog Entries: 15

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Hi!

Welcome to Linux Questions!

A shell running a script is always a non-interactive shell.
Also a non-interactive shell won't stop execution to ask anyone and can be run unattended by
the system.

It also doesn't have an associated terminal for output.

http://tldp.org/LDP/abs/html/intandnonint.html
http://www.linuxquestions.org/questi...shells-680599/
http://tldp.org/LDP/abs/html/

Hope this helps.
 
Old 01-29-2013, 06:59 AM   #3
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
The answer lays in setsid's source code. Specifically this part:
Code:
	if (getpgrp() == getpid()) {
		switch (fork()) {
		case -1:
			err(EXIT_FAILURE, _("fork"));
		case 0:
			/* child */
			break;
		default:
			/* parent */
			return 0;
		}
	}
setsid will fork if process group is equal to process ID which means that the process is group leader. This happens when you run the command interactively because bash, as part of its job control, creates a new process group for the setsid command. When the command is run in a non-interactive fashion, setsid command is part of the same process group as the whole shell and thus it's not a process group leader and it does not fork.
 
  


Reply



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
[SOLVED] Advantage to creating a daemon vs just setsid Keozon Linux - Server 5 03-22-2011 05:58 AM
Is it necessary to use setsid in order to create a daemon? mitsulas Programming 1 04-21-2010 11:52 AM
setsid and command-line piping in bash ta0kira Programming 7 04-28-2008 09:31 PM
setsid() without forking OR debugging forked process with gdb ta0kira Programming 3 08-09-2007 06:22 AM
setsid() question: child has terminal after all? registering Programming 1 06-16-2003 06:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:49 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration