15

I mean I can't use it in bash, is it not available on OS X, or is it just missing on my Mac?

It's not a PATH variable issue, because I searched with find command, and there's no file named setsid on my Mac at all.

If it's missing on OS X, is there any alternative to it? Or if it's the case that I somehow deleted it accidentally, where can I find a copy of it?

Nathaniel_Wu
  • 191
  • 1
  • 7
  • This should be part of the XCode command line tools. Have you installed them? – user3207838 Apr 13 '16 at 07:06
  • @user3207838 Yes, I have Xcode installed already, and "xcode-select --install" returns that command line tools are already installed. – Nathaniel_Wu Apr 14 '16 at 07:34
  • 1
    Ahem, isn't [setsid(2)](https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man2/setsid.2.html) a system call and not a runnable program? –  Apr 14 '16 at 09:02
  • 4
    @SamiLaine I know why this is now, it's a runnable program on many other *nix OSes, but it's missing in FreeBSD, thus OS X doesn't have that either. – Nathaniel_Wu Apr 22 '16 at 16:57
  • 2
    "Missing" implies that it *should* be available. POSIX specifies the C function for `setsid` program, but the command-line utility is purely an extension -- no OS vendor is *obligated* to provide it. – Charles Duffy Dec 29 '17 at 03:52

3 Answers3

12

use Brew:

brew install util-linux
Til
  • 5,150
  • 13
  • 26
  • 34
prodriguez903
  • 161
  • 1
  • 4
  • I've just tried this to no avail either: `brew install util-linux` Warning: util-linux 2.34 is already installed and up-to-date To reinstall 2.34, run brew reinstall util-linux; `which setsid` returns prompt, but $PATH set correctly. Found but not tried https://github.com/tzvetkoff/setsid-macosx – volvox Oct 04 '19 at 08:18
  • 3
    When installing it: `util-linux is keg-only, which means it was not symlinked into /usr/local, because macOS provides the uuid.h header.` Which is a bit sad, but... To run it, you can either invoke it directly: `/usr/local/opt/util-linux/bin/setsid` Or add these to `/usr/local/bin` (I do this, but it's not recommended as Homebrew prefers to manage this itself): `ln -s /usr/local/opt/util-linux/bin/* /usr/local/bin/` – David Cuthbert Jan 01 '20 at 20:57
  • 1
    If you wish to add the binaries to your PATH, be sure to follow the instructions at the end (It is not done automatically, unlike most brew packages) – Jordan Réjaud Nov 03 '20 at 04:55
2

Yes. /usr/bin/setsid is missing on Mac OS/X.

The OS interface is available, so based on the chapter 2 man page there may be some hope for porting the Linux source to Darwin.

J_H
  • 17,926
  • 4
  • 24
  • 44
1

While macOS does not come with a setsid command, it does come with scripting languages which support calling the setsid C function, such as Perl and Python. So, if you don't want to (or for some reason can't) install a setsid command via Homebrew (or MacPorts or whatever), another option is to write your own in a scripting language. As an example, try this Perl script (which I based off this with some minor changes):

#!/usr/bin/perl -w
use strict;
use POSIX qw(setsid);
fork() && exit(0);
setsid() or die "setsid failed: $!";
exec @ARGV;

If you don't like Perl, Python's os module has a setsid function too.

A simple demo, which relies on the fact that /dev/tty is an alias of your controlling terminal if you have one, but reads/writes to it fail with an IO error if you don't:

$ bash -c 'echo I have a controlling terminal. > /dev/tty'
I have a controlling terminal.
$ ./setsid.pl bash -c 'echo I have a controlling terminal. > /dev/tty'

bash: /dev/tty: Device not configured
$

(Warning: With the release of macOS Catalina (10.15) in 2019, Apple deprecated the Perl, Python, Ruby and Tcl language runtimes shipped with macOS – they say new software should not use them, and they may be removed in a future macOS version – and Apple is not going to update their versions, which are becoming increasingly outdated. However, they are still there in Monterey, and while I haven't upgraded to Ventura yet, I haven't heard anything about their removal in that version either. One obviously shouldn't rely on them for any supported applications – if such software needs one of these runtimes, it should install its own copy of them. However, if it is just for a quick hacky script to easily test how some program behaves without a controlling terminal, using these OS-bundled runtimes is still fine.)

Simon Kissane
  • 4,373
  • 3
  • 34
  • 59