2

I'm looking to trigger a command on xrdp session start and end, if /var/log/xrdp.log file get update with session started in last 10 minutes, I would like to trigger a shell script.

Example: Session Login Trigger.

[root@CentOS73-RDPDemo sp]# cat trigger-login.sh

#!/bin/bash

if [ $(( $(date +%s) - $(date +%s -r /var/log/xrdp.log) )) -le 180 ]; then   
    tail -n 4 /var/log/xrdp.log | grep -i "socket: 11"   
   sh /usr/src/sp/sql-login.sh  
fi

Example: Session Logout Trigger

[root@CentOS73-RDPDemo sp]# cat trigger-logout.sh

#!/bin/bash

if [ $(( $(date +%s) - $(date +%s -r /var/log/xrdp.log) )) -le 180 ]; then

    tail -n 4 /var/log/xrdp.log | grep -i "socket: 12"
   sh /usr/src/sp/sql-logout.sh  
fi
  • You want to run trigger-login.sh and trigger-logout.sh if there is an update in last 10 mins in log file ? – Rocoder Feb 02 '17 at 13:53
  • Yes, I would to trigger shell file if there is any update in my log file. Like for each xrdp session my log generate specific entry. If shell script fetch last 10 minutes activity on specific content and send update to my DB will help tracking – Gulab Pasha Feb 02 '17 at 14:29

1 Answers1

0

Write a script (say main.sh) which checks if there is any update in file (var/log/xrdp.log). If there is any update in file, call your desired scripts (trigger-login.sh and trigger-logout.sh).

You can do it using stat or md5sum

Example for reference (main.sh).

#!/bin/bash

touch /tmp/checkMD5

nchksum=`md5sum a.out | awk -F " " '{print $1}'`
ochksum=`cat /tmp/checkMD5`

if [ "$nchksum" == "$ochksum" ]; then
        echo "both are same"
else
        sh trigger-login.sh
        sh trigger-logout.sh
fi

echo $nchksum >/tmp/checkMD5

schedule a crontab job which will run the main.sh script in every 10 minutes.

Rocoder
  • 1,083
  • 2
  • 15
  • 26