When I start a shell, in my case bash and/or zsh, what lines do I need to add to .zshrc and/or .bashrc to check to see if a program is already running, and if the program is not already running, start it?
Example: I am starting a new zsh shell. If top is not already running, I would like to start top and have it take over the shell. If top is already running in another window, I would like the .zshrc to continue loading and give me an open terminal.
6-16 Update:
if ! pgrep -U $USER top >/dev/null; then
exec top
fi
The script above satisfies my request. The -q flag wouldn't work for me because -q is not an option on my system. So I had to pipe the PID to /dev/null. Thank you mveroone, Kusalananda, and Thomas Dickey.
I have some feature creep that is outside of the scope of the original question. Suppose I am on a single user system where I can become root without needing to enter a password. How would I autorun top as root if I needed to?
6-18 Ok, the feature creep issue of passing a command as root is resolved. I'm using this to launch several
Thank you for all of the thoughtful replies.