1

I would like to be able to log the command used to run the current python script within the script itself. For instance this is something I tried:

#test.py
import sys,subprocess
with open('~/.bash_history','r') as f:
    for line in f.readlines():
        continue

with open('logfile','r') as f:
    f.write('the command you ran: %s'%line.strip('\n'))

However the .bash_history does not seem to be ordered in chronological order. What's the best recommended way to achieve the above for easy logging? Thanks.

Update: unfortunately sys.argv doesn't quite solve my problem because I need to use process subtitution as input variables sometimes.

e.g. python test.py <( cat file | head -3)

4
  • 3
    The history isn't written after every command. Commented Apr 8, 2014 at 17:57
  • sys.argv isn't a perfect replica of the typed command, but it is close. Commented Apr 8, 2014 at 17:58
  • Indeed, sys.argv does not quite meet my need, since I use input argument with process substitutions like python.test <(cat file | head -4) Commented Apr 8, 2014 at 18:04
  • If you're on Linux, perhaps /proc/<pid>/cmdline, /proc/<pid>/exe, or other such things might be of help, although they might suffer from similar issues as sys.argv... That won't catch process substitutions, of course, because that's all handled by the shell, and the appropriate pipes, etc. are built before your program even starts running... Commented Apr 8, 2014 at 20:45

2 Answers 2

1

What you want to do is not universally possible. As devnull says, the history file in bash is not written for every command typed. In some cases it's not written at all (user sets HISTFILESIZE=0, or uses a different shell).

The command as typed is parsed and processed long before your python script is invoked. Your question is therefore not related to python at all. Wether what you want to do is possible or not is entirely up to the invoking shell. bash does not provide what you want.

If your can control the caller's shell, you could try using zsh instead. There, if you setopt INC_APPEND_HISTORY, zsh will append to its history file for each command typed, so you can do the parse history file hack.

Sign up to request clarification or add additional context in comments.

Comments

0

One option is to use sys.argv. It will contain a list of arguments you passed to the script.

import sys

print 'Number of arguments:', len(sys.argv), 'arguments.'
print 'Argument List:', str(sys.argv)

Example output:

>python test.py
Number of arguments: 1 arguments.
Argument List: ['test.py']

>python test.py -l ten
Number of arguments: 3 arguments.
Argument List: ['test.py', '-l', 'ten']

As you can see, the sys.argv variable contains the name of the script and then each individual parameter passed. It does miss the python portion of the command, though.

1 Comment

thanks for the suggestion. Please see my update in the question for why sys.argv doesn't quite solve my problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.