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)

sys.argvisn't a perfect replica of the typed command, but it is close./proc/<pid>/cmdline,/proc/<pid>/exe, or other such things might be of help, although they might suffer from similar issues assys.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...