how to execute a command after resume from suspend?

You can place your scripts in the /etc/pm/sleep.d directory to have them run after suspend. You will need to add a conditional to make your script run only during resume and not during the suspend process as well. For example, your touchpad script would look like:

case "${1}" in
    resume|thaw)
        DISPLAY=:0.0 ; export DISPLAY
        xinput set-int-prop "SynPS/2 Synaptics TouchPad" "Two-Finger Scrolling" 8 1
        xinput set-int-prop "SynPS/2 Synaptics TouchPad" "Synaptics Two-Finger Scrolling" 8 1 1
        xinput set-int-prop "SynPS/2 Synaptics TouchPad" "Synaptics Two-Finger Pressure" 32 10
        xinput set-int-prop "SynPS/2 Synaptics TouchPad" "Synaptics Two-Finger Width" 32 8
        setxkbmap -layout gb
        xkbset m
        xkbset exp =m
        su $USER -c "sleep 3; /usr/bin/xmodmap -e "keycode 135 = Pointer_Button2"" &
;;
esac

Be sure your script is marked globally executable and change $USER to the corresponding username.

You can find more detailed information in the pm-suspend manpage (man pm-suspend) or by looking at the documentation in /usr/share/doc/pm-utils (particularly /usr/share/doc/pm-utils/HOWTO.hooks.gz).


On Ubuntu 16.04 I had to create service this way:

  1. create file

    sudo gedit /etc/systemd/system/somename.service
    
  2. put inside

    [Unit]
    Description=Some description
    Before=sleep.target
    StopWhenUnneeded=yes
    
    [Service]
    Type=oneshot
    RemainAfterExit=yes
    ExecStop=-/path/to/your/script.sh
    
    [Install]
    WantedBy=sleep.target
    
  3. enable service

    sudo systemctl enable somename
    
  4. (optional) if not working after resume from suspend > check for errors with

    journalctl -u somename.service
    

Open this file:

sudo vim /lib/systemd/system-sleep/hdparm

Contents:

#!/bin/sh

case $1 in 
  post)
    /usr/lib/pm-utils/power.d/95hdparm-apm resume
    ## Paste your command to run your script
    ;; esac

Your command will execute with admin privileges.