How to see which users execute a command (Shell Scripting)

I am wondering if you can help me with this problem. I need to write a shell script to see if other users of the watch command are running on the same machine the x command is running on. Could you help me guys? Many thanks.

+4
source share
3 answers

To resolve the issue, you can use it ps -aux | grep <prgname>as the root user.

EG: ps -aux | grep firefoxexecuting this command (with root privileges), returns the following result:

sergio    3252 24.1  6.7 1840936 540264 ?      Sl   09:48 123:36 /usr/lib/firefox/firefox
root     23059  0.0  0.0  15944   948 pts/7    S+   18:20   0:00 grep --color=auto firefox

The last line is the command I executed!

ps script, . , , , , Ubuntu 14.

#!/bin/bash

i=0
search="watch"

tmp=`mktemp`
ps -aux | tr -s ' ' | grep "$search" > $tmp

while read fileline
do
user=`echo $fileline | cut -f1 -d\  `
prg=`echo $fileline | cut -f11 -d\  `
prg=`basename $prg`
if [ $prg == $search ]; then
    echo "$user - $prg"
    i=`expr $i + 1`
fi
done < $tmp

if [ $i == 0 ]; then
    echo User not found
fi

rm $tmp
+1
while [ 1 ]; do
    if [ -n "`ssh $hostname pgrep -f 'Pino_special_command'`" ]; then
        ssh $hostname "ps -aux | grep watch" | grep -v "grep"
    fi
done
+1

Use command pgrep

pgrep watch
0
source

Source: https://habr.com/ru/post/1589466/


All Articles