Is there something you can type into terminal so that it will run a command if a certain program is running? For example, if <Libre office is running> then sudo pkill <Libre ofice>
.
Edit 1: So what I want to do is make an if statement in terminal that runs if a program is running and if it is not it does nothing, or does something else.
To determine if a program is running given its name, use
pgrep
. To dosomething if LibreOffice's word processor is running:Similarly, to do something if LibreOffice's word processor is not running:
Further, you can use these two in combination:
Or, if things get more complicated, use an
if
statement:The above work because
pgrep
, just likegrep
, sets a useful exit code. If a process is found, it returns with an exit code of zero (which the shell interprets as logical true). If not, it returns with one (the shell interprets any non-zero exit code to mean false).To find out all the programs that are currently running (and hence what name to use as the argument to
pgrep
), runps ax