Skip to content

Instantly share code, notes, and snippets.

@negokaz
Created November 5, 2019 13:44
Show Gist options
  • Save negokaz/d561fb3ccf113958862bd5ff4e7b0f6e to your computer and use it in GitHub Desktop.
Save negokaz/d561fb3ccf113958862bd5ff4e7b0f6e to your computer and use it in GitHub Desktop.
A Perl implementation of setsid(1)
#!/usr/bin/env perl
#
# A Perl implementation of setsid(1)
#
# setsid(1) - run a program in a new session
# http://man7.org/linux/man-pages/man1/setsid.1.html
use POSIX setsid;
setsid() or die "Failed to create new session";
exec @ARGV;
@FBnil
Copy link

FBnil commented Jun 24, 2021

There is a bug there and it is important:

  • Do a fork before calling setsid
  • From https://perldoc.perl.org/POSIX
  • My example uses minimal amount of RAM usage by not exporting anything
  • call it with the fully qualified name.
  • Nothing wrong with use POSIX qw(setsid);
  • Die with a message is descriptive enough, $! tend to follow the local language though
  • This works:

`use POSIX ();

fork() && exit(0);
POSIX::setsid() or die $!;
exec @argv;
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment