-1

Environment

  • Bash in normal linux
  • Executable file: /usr/bin/eexxee

Requirement

I need to run the follow command only once: bash eexxee -a xxxx -b xxxx and we can start this command with these follow similar command-lines: bash eexxee -b xxxx -a xxxx /usr/bin/eexxee -b xxxx -a xxxx /usr/bin/eexxee -a xxxx -b xxxx Now I want to write a shell/python script to start this command with checking if the similar command-line is running.

Question

How can we judge the matching of these similar command-lines?

colben
  • 31
  • 1
  • 7
  • You want to test that `eexxee -b xxxx -a xxxx` is the same as `eexxee -a xxxx -b xxxx`? – dawg Aug 15 '17 at 03:04

2 Answers2

0

Not sure how much differentiation you need, but these two should be enough:

  1. lsof /usr/bin/eexxee
  2. ps fxau | grep eexxee
  • You did not get what I meaned in the question, fox example, the command "eexxee -b xxxx -a xxxx" is running, and I want to check if the similar command is running before start the command "eexxee -a xxxx -b xxxx" in a shell script, how can we make it? – colben Aug 15 '17 at 03:07
  • @colben if you want to be able to monitor the commands being run, you can use the subprocess module in python. That way you can check to see if the process if running. A good answer regarding figuring out what processes are running in python is this one: [https://stackoverflow.com/questions/26688936/python-how-to-get-pid-by-process-name] (https://stackoverflow.com/questions/26688936/python-how-to-get-pid-by-process-name) Python is probably the best choice for you if you want very fine control over the details of your process run – Vincent Yuan Aug 15 '17 at 03:58
0

perhaps something like this can help.

I'm making few assumptions while writing this. Im assuming xxxx and yyyy will always be 2nd or 4th argument.

#!/usr/bin/bash

# exeee -a xxxx -b yyyy
# $0 = exeeee
# $1 = -a
# $2 = xxxx
# $3 = -b
# $4 = yyyy

pid=$$

cnt=$(
ps -ef |\
grep 'eexxee -[ab] '"$2"' -[ab] '"$4"'$' |\
grep -v $pid |\
wc -l
)

if [ $cnt -ne 0 ];then
   echo "another instance running"
   exit 1
fi
Abis
  • 155
  • 8