| •1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
# ttyoff - run command detached from terminal |
| 4 |
# |
| 5 |
# Copyright 2020, Nicholas Guriev <[email protected]> |
| 6 |
# |
| 7 |
# Permission is hereby granted, free of charge, to any person obtaining a |
| 8 |
# copy of this software and associated documentation files (the "Software"), |
| 9 |
# to deal in the Software without restriction, including without limitation |
| 10 |
# the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 11 |
# and/or sell copies of the Software, and to permit persons to whom the |
| 12 |
# Software is furnished to do so, subject to the following conditions: |
| 13 |
# |
| 14 |
# The above copyright notice and this permission notice shall be included |
| 15 |
# in all copies or substantial portions of the Software. |
| 16 |
# |
| 17 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 18 |
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 20 |
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 21 |
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 22 |
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 23 |
# OTHER DEALINGS IN THE SOFTWARE. |
| 24 |
|
| 25 |
=head1 NAME |
| 26 |
|
| 27 |
ttyoff - run command detached from terminal |
| 28 |
|
| 29 |
=head1 SYNOPSIS |
| 30 |
|
| 31 |
B<ttyoff> [B<-a> I<NAME>] [B<-s>] [I<COMMAND>...] |
| 32 |
|
| 33 |
=head1 DESCRIPTION |
| 34 |
|
| 35 |
Simple script that detaches itself from the controlling terminal and executes |
| 36 |
passed command. The script is primarily intended for running automated tests. |
| 37 |
|
| 38 |
If no command is specified, current Shell is executed. |
| 39 |
|
| 40 |
Options: |
| 41 |
|
| 42 |
=over |
| 43 |
|
| 44 |
=item B<-a> |
| 45 |
|
| 46 |
Override the zeroth argument of the command. |
| 47 |
|
| 48 |
=item B<-s> |
| 49 |
|
| 50 |
Before proceeding, close standard streams (numbered 0, 1, and 2). |
| 51 |
|
| 52 |
=back |
| 53 |
|
| 54 |
=head1 AUTHOR |
| 55 |
|
| 56 |
Written by Nicholas Guriev on New Year's Eve 2021. |
| 57 |
|
| 58 |
=head1 SEE ALSO |
| 59 |
|
| 60 |
tty(4) |
| 61 |
|
| 62 |
=cut |
| 63 |
|
| 64 |
|
| 65 |
use strict; |
| 66 |
use warnings; |
| 67 |
|
| 68 |
use Getopt::Std; |
| 69 |
BEGIN { require "sys/ioctl.ph" } |
| 70 |
|
| 71 |
$Getopt::Std::STANDARD_HELP_VERSION = 1; |
| 72 |
our ($opt_a, $opt_s); |
| 73 |
getopts("a:s") or exit 1; |
| 74 |
|
| 75 |
if (open my $term, "+<", "/dev/tty") { |
| 76 |
ioctl $term, TIOCNOTTY, 0 or die $!; |
| 77 |
} |
| 78 |
if ($opt_s) { |
| 79 |
close $_ or die $! for (*STDIN, *STDOUT, *STDERR); |
| 80 |
} |
| 81 |
unless (@ARGV) { |
| 82 |
push @ARGV, $ENV{SHELL} || "/bin/sh"; |
| 83 |
} |
| 84 |
exec { $opt_a or $ARGV[0] } @ARGV or die $!; |