pgrepপরিবর্তে ব্যবহার করুন:
pgrep -cxu $USER -f my-tool
ব্যবহৃত বিকল্পগুলি হ'ল:
-c, --count
Suppress normal output; instead print a count of matching pro‐
cesses. When count does not match anything, e.g. returns zero,
the command will return non-zero value.
-x, --exact
Only match processes whose names (or command line if -f is spec‐
ified) exactly match the pattern.
-u, --euid euid,...
Only match processes whose effective user ID is listed. Either
the numerical or symbolical value may be used.
আপনি যদি এটি কোনও ব্যাশ স্ক্রিপ্টে ব্যবহার করতে চান যা এটি ইতিমধ্যে চলমান কিনা তা পরীক্ষা করে দেখে আপনি ব্যবহার করতে পারেন $0। এটি বর্তমান স্ক্রিপ্টের (যেমন /home/username/bin/foo.sh) পথে প্রসারিত হয় তবে আমাদের কেবল প্রয়োজন foo.sh। পেতে, আমরা সবকিছু শেষ পর্যন্ত অপসারণ করতে পারেন /ব্যবহার ব্যাশ এর স্ট্রিং ম্যানিপুলেশন সরঞ্জাম : ${0##*/}। এর অর্থ আমরা এর মতো কিছু করতে পারি:
## If there are more than 1 instances of the current script run
## by this user
if [[ $(pgrep -cxu "$USER" "${0##*/}") -gt 1 ]];
then
echo "Script already running, exiting."
exit
fi
আপনি এর জন্য লকফিলগুলি ব্যবহার করার বিষয়টিও বিবেচনা করতে পারেন:
## If the lock file exists
if [ -e /tmp/$USER.foo.lock ]; then
## Check if the PID in the lockfile is a running instance
## of foo.sh to guard against crashed scripts
if ps $(cat /tmp/$USER.foo.lock) | grep foo.sh >/dev/null; then
echo "Script foo.sh is already running, exiting"
exit
else
echo "Lockfile contains a stale PID, continuing"
rm /tmp/$USER.foo.lock
fi
fi
## Create the lockfile by printing the script's PID into it
echo $$ > /tmp/$USER.foo.lock
## Rest of the script here
## At the end, delete the lockfile
rm /tmp/$USER.foo.lock
pidof, আরও ভাল সরঞ্জাম মিথ্যা রয়েছেpgrep