0

doa

#!/bin/sh
myexe

myexe

if sys.stdout.isatty():
    print 'from a script'
else:
    print 'not from a script'

OUTPUT (if i execute doa from terminal):

not from a script

OUTPUT (if i execute myexe from terminal):

not from a script

I want it to say 'from a script' if executed from doa

Question: is it possible for myexe to know that it's being executed from a bash script?

jww
  • 97,681
  • 90
  • 411
  • 885
ealeon
  • 12,074
  • 24
  • 92
  • 173
  • I don't know of a way to detect if the files been called from a script (In theory, it is always called by a script, be it a batch script, the command line, of your mouse double clicking on it). You could instead pass it a parameter from the script to detect with `sys.argv[index]` and tell it where it has been called from. – NDevox Mar 24 '15 at 18:00
  • A bash script as opposed to what? Your `sh` script? An interactive shell? When does it matter? – that other guy Mar 24 '15 at 18:01
  • 2
    Seeing if stdout is a TTY is a common, sensible check because it lets you decide whether to output in a human-readable or computer-readable format (e.g., colored output when connected to a TTY, plain output when piped to another process). But checking how your program is invoked... what would you do differently based on the identity of the parent process–and **why**? – John Kugelman Mar 24 '15 at 18:01
  • from your terminal vs called from a script – ealeon Mar 24 '15 at 18:01
  • 2
    I'm pretty sure the answer is no. What problem are you really trying to solve? – Steven Rumbalski Mar 24 '15 at 18:04
  • @StevenRumbalski The problem is that myexe is suppose to say it's from a script if it is not called directly from terminal. tty check works with most scripts except shell script, it seems – ealeon Mar 24 '15 at 18:15
  • "The problem is that myexe is suppose to say it's from a script if it is not called directly from terminal." Yes. That's what you asked. But why? Why is it so important to know? – Steven Rumbalski Mar 24 '15 at 18:28
  • Did you try to redirect output to a file and see what it says then? Cause if you invoke it from a script whose output is a terminal then it transitively means the output of your exe is also a terminal. – bobah Mar 09 '21 at 17:35

1 Answers1

4

You can use psutil to ask for the name of the process with id the parent process id:

import psutil
import os

ppid = os.getppid() # Get parent process id
psutil.Process(ppid).name() == "bash"

You can install psutil with pip command:

pip install psutil
JuniorCompressor
  • 19,631
  • 4
  • 30
  • 57
  • 1
    If the user's console is also bash, then this won't be able to determine if it is being run from a script or if it's run directly from the console... – Bill Lynch Mar 24 '15 at 18:09
  • i know i gave bash as an example but i want to encompasses whole script. myexe can be called from another python script or whatnot – ealeon Mar 24 '15 at 18:11
  • @BillLynch can you check if the user console is bash or not and then compare against it? oh wait nvm.. hmm.. – ealeon Mar 24 '15 at 18:20
  • @ealeon: You can usually tell if the user console is bash by checking `$SHELL`. But personally, all of this is a bad idea. – Bill Lynch Mar 24 '15 at 18:21