I want something like this:
maybeexec command arg1 arg2 &> /logs/log
Where maybeexec is a bash function, like so:
maybeexec() {
if ! ps ax | grep -v grep | grep "$*" > /dev/null
then
"$@"
fi
}
So basically it checks if command arg1 arg2 is already running, and if so, does not run it.
My problem, though, is that even if command arg1 arg2 is already running and so maybeexec does not run it concurrently, /logs/log is still opened for redirection and will overwrite the existing /logs/log, which I do not want.
What's the most elegant way to solve this? If possible, I'd like to keep calling maybeexec as I do now, since I use it to run many commands that all redirect output to different files.
Thanks!