vitess.io/vitess@v0.16.2/go/cmd/tools.go (about) 1 /* 2 Copyright 2019 The Vitess Authors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 package cmd 17 18 import ( 19 "fmt" 20 "os" 21 "os/exec" 22 "strings" 23 "syscall" 24 ) 25 26 // DetachFromTerminalAndExit allows a command line program to detach from the terminal and continue running 27 // even if the parent process is terminated 28 func DetachFromTerminalAndExit() { 29 args := os.Args[1:] 30 i := 0 31 for ; i < len(args); i++ { 32 if strings.HasPrefix(args[i], "-detach") || strings.HasPrefix(args[i], "--detach") { 33 args[i] = "--detach=false" 34 break 35 } 36 } 37 cmd := exec.Command(os.Args[0], args...) 38 cmd.Stdin = os.Stdin 39 cmd.Stdout = os.Stdout 40 cmd.Stderr = os.Stderr 41 _ = cmd.Start() 42 fmt.Println("[PID]", cmd.Process.Pid) 43 os.Exit(0) 44 } 45 46 // IsRunningAsRoot checks if a component is being ran as root 47 func IsRunningAsRoot() bool { 48 return syscall.Geteuid() == 0 49 }