Reputation: 7
I know how to check if a script is already running (if pidof -o %PPID -x "scriptname.sh"; then...
). But now I have a script that accepts inputs as flags, so it can be used in several different scenarios, many of which will probably run at the same time.
Example:
/opt/scripts/backup/tar.sh -d /directory1 -b /backup/dir -c /config/dir
and
/opt/scripts/backup/tar.sh -d /directory2 -b /backup/dir -c /config/dir
The above runs a backup script that I wrote, and the flags are the parameters for the script: the directory being backed up, the backup location, and the configuration location. The above example are two different backups (directory 1
and directory 2
) and therefore should be allowed to run simultaneously.
Is there any way for a script to check if it is being run and check if the running version is using the exact same parameters/flags?
Upvotes: 0
Views: 262
Reputation: 2023
The ps -Af
command will provide you all the processes that run on you os with the "command" line used to run them.
Upvotes: 1
Reputation: 185871
One solution :
if ps auxwww | grep '/[o]pt/scripts/backup/tar.*/directory2'; then
echo "running"
else
echo "NOT running"
fi
Upvotes: 0