Can I see in a log file all GUI based tasks in its alternative command-line format?

user308164

For example, I normally open mousepad (xfce equivalent of gedit) from the applications menu. However, I know that you can also do this in a terminal by typing mousepad.

Following this example, what I want is whenever I open mousepad via GUI, a new line is written in a log file stating something like Sep 5 15:35:11 lucho@lucho:~$ mousepad. More in general, what I want is to log all GUI activities that are potentially do-able via command-line (like opening programs, changing permissions, modifying a system settings, etc) but written in its alternative command-line execution format. I want this in order to improve my knowledge of how to use the command-line (without going through the man pages). There are many things I do through the GUI which I don't do via command-line (some potentially automatable via a script or via keyboard shortcuts) and having this log file would be a good way to learn them.

I'm aware of the existence of the syslog file in /var/log but that is not what I need. The Activity Log Manager app from Ubuntu repositories does not show command-line format, so far as I know. I need something like the .bash_history file that exist in my home folder but recording my GUI-based activities.

Sergiy Kolodyazhnyy

Introduction

While it's not possible to log all GUI actions, such things as logging commands that correspond to open windows can be done. Below is the simple python script that does the job. It's still in development, but does 90% of the required task.

Source code

#!/usr/bin/env python3
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('Gdk', '3.0')
from gi.repository import Gdk,Gtk
import time
import os
import subprocess

def run_cmd(cmdlist):
    """ Reusable function for running external commands """
    new_env = dict(os.environ)
    new_env['LC_ALL'] = 'C'
    try:
        stdout = subprocess.check_output(cmdlist, env=new_env)
    except subprocess.CalledProcessError:
        pass
    else:
        if stdout:
            return stdout
def print_info(stack,event):
    base_xprop = ['xprop','-notype']
    for xid in stack:
        pid = None
        check_pid = run_cmd(base_xprop + [ '_NET_WM_PID', '-id',str(xid)])
        if check_pid:
            pid = check_pid.decode().split('=')[1].strip()
        with open('/proc/'+pid+'/cmdline') as fd:
            command = fd.read()
        print(time.strftime("%D %H:%M:%S" + " "*3) + event + pid + " " + command)

def main():
    sc = Gdk.Screen.get_default()
    old_stack = None

    while True:
        stack = [ win.get_xid() for win in sc.get_window_stack() ]
        if old_stack:
            # Difference between current and old stack will show new programs
            diff = set(stack) - set(old_stack)
            if diff:
                print_info(diff," 'New window open' ")
        else:
            print_info(stack," 'Script Started' ")

        old_stack = stack
        time.sleep(2)

if __name__ == '__main__': main()

Test run:

$ ./log_open_windows.py                                                                                                
01/25/17 15:33:13    'Script Started' 2915 nautilus-n
01/25/17 15:33:13    'Script Started' 3408 /opt/google/chrome/chrome
01/25/17 15:33:13    'Script Started' 12540 /usr/bin/python/usr/bin/x-terminal-emulator
01/25/17 15:33:13    'Script Started' 2454 compiz
01/25/17 15:33:13    'Script Started' 2454 compiz
01/25/17 15:33:13    'Script Started' 2454 compiz
01/25/17 15:33:13    'Script Started' 2454 compiz
01/25/17 15:33:13    'Script Started' 2454 compiz
01/25/17 15:33:21    'New window open' 15143 /usr/lib/firefox/firefox-new-window
01/25/17 15:33:27    'New window open' 15196 unity-control-center

The script shows timestamp, event type, the window PID, and the corresponding command.

How to use

The standard rules of any script apply. Make sure you store the script in ~/bin directory. If you don't have ~/bin directory , then create one. Save script file there and ensure it is executable with chmod +x ~/bin/log_open_windows.py. After than you can run it from command line at any time you wish by calling ~/log_open_windows.py in command-line.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How can I get the extension(s) of a file based on its content?

분류에서Dev

How can I create a file with a specific size from a command line?

분류에서Dev

How can I run a specific line as a command in a text file?

분류에서Dev

Can I see the number of PCI slots with a command?

분류에서Dev

How I can I start a powershell command line from a batch file and enforce it to stay open?

분류에서Dev

How can I check through command-line the computer form(Desktop, notebook or All-in-one)?

분류에서Dev

How can I kill all child processes of a certain process from the command line?

분류에서Dev

How do I boot to a command line interface instead of a GUI?

분류에서Dev

How do I run a gui app from the command line?

분류에서Dev

How do I copy the directory and its folders in windows command line?

분류에서Dev

How can I turn on File and Printer sharing, and the Firewall from the Windows command line

분류에서Dev

How can I pass command line arguments to a program in a Windows batch file?

분류에서Dev

Can I make a batch file that sets up a https proxy for IE without showing command line or window?

분류에서Dev

How can I pass parameter in Talend exported job, through command line or bash script file?

분류에서Dev

Text Processing - How to output file that match a pattern in all of its line

분류에서Dev

Can I see the summed up results of the ping command while it is running?

분류에서Dev

How can I pretty format a JSON file, with all the correct indents and everything?

분류에서Dev

Command line interface Vs GUI

분류에서Dev

Why I can't see port opened by a process in file descriptors?

분류에서Dev

How can I save the last command to a file?

분류에서Dev

Command-line equivalences of some tasks in Synaptic

분류에서Dev

Can I change the output format of the "last" command to display the year?

분류에서Dev

How can I format this div without the text inside breaking a line?

분류에서Dev

How to see all the bits of a file?

분류에서Dev

Can I see in history output from which directory I had actually issued a command?

분류에서Dev

How do I see all previous output from a completed terminal command?

분류에서Dev

How can I preview HTML documents from the command line?

분류에서Dev

How can I install one language by command-line

분류에서Dev

How can I import a database using command line?

Related 관련 기사

  1. 1

    How can I get the extension(s) of a file based on its content?

  2. 2

    How can I create a file with a specific size from a command line?

  3. 3

    How can I run a specific line as a command in a text file?

  4. 4

    Can I see the number of PCI slots with a command?

  5. 5

    How I can I start a powershell command line from a batch file and enforce it to stay open?

  6. 6

    How can I check through command-line the computer form(Desktop, notebook or All-in-one)?

  7. 7

    How can I kill all child processes of a certain process from the command line?

  8. 8

    How do I boot to a command line interface instead of a GUI?

  9. 9

    How do I run a gui app from the command line?

  10. 10

    How do I copy the directory and its folders in windows command line?

  11. 11

    How can I turn on File and Printer sharing, and the Firewall from the Windows command line

  12. 12

    How can I pass command line arguments to a program in a Windows batch file?

  13. 13

    Can I make a batch file that sets up a https proxy for IE without showing command line or window?

  14. 14

    How can I pass parameter in Talend exported job, through command line or bash script file?

  15. 15

    Text Processing - How to output file that match a pattern in all of its line

  16. 16

    Can I see the summed up results of the ping command while it is running?

  17. 17

    How can I pretty format a JSON file, with all the correct indents and everything?

  18. 18

    Command line interface Vs GUI

  19. 19

    Why I can't see port opened by a process in file descriptors?

  20. 20

    How can I save the last command to a file?

  21. 21

    Command-line equivalences of some tasks in Synaptic

  22. 22

    Can I change the output format of the "last" command to display the year?

  23. 23

    How can I format this div without the text inside breaking a line?

  24. 24

    How to see all the bits of a file?

  25. 25

    Can I see in history output from which directory I had actually issued a command?

  26. 26

    How do I see all previous output from a completed terminal command?

  27. 27

    How can I preview HTML documents from the command line?

  28. 28

    How can I install one language by command-line

  29. 29

    How can I import a database using command line?

뜨겁다태그

보관