Skip to main content
minor fixes
Source Link
SquareOne
  • 7.6k
  • 1
  • 16
  • 35

You can ask MMA to run the traditional ps,grep,kill,killall "unix" shell commands (using for example the RunProcess commandand Runcommands).

RunProcess[{$SystemShell,myExternalCommand "-c",= "ps -acx -o pid -o command | grep -i chrome"

RunProcess[{$SystemShell, "-c", myExternalCommand}, "StandardOutput"]

Here,We use here the Mathematica command RunProcess because it is useful not only to execute an external command but also to retrieve the external output of the command.

What does the external command myExternalCommand : 1/ the shell command ps lists all the running processes (instances of running programs) on your computer, then the result is passed (piped) to the command grep which here fetchs and displays only every line containing the word "chrome" (case insensitive here).

myExternalCommand = "ps -acx -o pid -o command | grep -i chrome$"        

RunProcess[{$SystemShell, "-c", 
  "ps -acx -o pid -o command | grep -i chrome$$SystemShell, "-c", myExternalCommand}, "StandardOutput"]

the "$""chrome$" pattern means that you look for the word "chrome" at the end of a line ...

use1/ the killall shell command must be followed by the plain app name (as displayed by the ps command) ( !!! WARNING : the following command will really quit Chrome)

myExternalCommand="killall 'Google Chrome'"

RunProcess[{$SystemShell, "-c", "killall 'Google Chrome'"myExternalCommand}, "StandardOutput"]

or more simply just use for example Run because we don't need to retrieve any useful output here :

Run[myExternalCommand]

2/ the kill shell command must be followed by the app PID (Process ID) number (as displayed by the ps command in the first column, see the output above)

(* WARNING WARNING WARNING WARNING !!!!!*)
(* only run the following \
external command if you know exactly what you are doing, otherwise you could \
quit some other important processes and make damages *)

(*Before running, replace myPIDnumber with the number displayed in \
the output of the ps command above, here I should replace it by 1709 *) \

RunProcess[{$SystemShell,myExternalcommand "-c",= "kill myPIDnumber"},myPIDnumber";
(* "StandardOutput"]Then just the RunProcess or Run command above in 1/*)

These bothkillall and kill commands both will send the given app (here Chrome) a request to quit but this might not be taken into account by the app if it has some other higher priority tasks running (writing on disk, ...). It is recommended to use this method first but if it does not work you can force the app to quit immediately by adding to the commands the "-9" option, that means : so "kill -9 ..." or "killall -9 ..."

You can ask MMA to run the traditional ps,grep,kill,killall "unix" shell commands (using for example the RunProcess command).

RunProcess[{$SystemShell, "-c", "ps -acx -o pid -o command | grep -i chrome"}, "StandardOutput"]

Here, the shell command ps lists all the running processes (instances of running programs) on your computer, then the result is passed (piped) to the command grep which here fetchs and displays only every line containing the word "chrome" (case insensitive here).

RunProcess[{$SystemShell, "-c", 
  "ps -acx -o pid -o command | grep -i chrome$"}, "StandardOutput"]

the "$" means that you look for the word "chrome" at the end of a line ...

use the killall shell command followed by the plain app name (as displayed by the ps command) ( !!! WARNING : the following command will really quit Chrome)

RunProcess[{$SystemShell, "-c", "killall 'Google Chrome'"}, "StandardOutput"]

or the kill command followed by the app PID (Process ID) number (as displayed by the ps command in the first column, see the output above)

(* WARNING WARNING WARNING WARNING !!!!!*)
(* only run the following \
command if you know exactly what you are doing, otherwise you could \
quit some other important processes and make damages *)

(*Before running, replace myPIDnumber with the number displayed in \
the output of the ps command above, here I should replace it by 1709 *) \

RunProcess[{$SystemShell, "-c", "kill myPIDnumber"}, "StandardOutput"]

These both commands will send the app a request to quit but this might not be taken into account by the app if it has some other tasks running (writing on disk, ...). It is recommended to use this method first but if it does not work you can force to quit by adding to the commands the "-9" option : so "kill -9 ..." or "killall -9 ..."

You can ask MMA to run the traditional ps,grep,kill,killall "unix" shell commands (using for example the RunProcess and Runcommands).

myExternalCommand = "ps -acx -o pid -o command | grep -i chrome"

RunProcess[{$SystemShell, "-c", myExternalCommand}, "StandardOutput"]

We use here the Mathematica command RunProcess because it is useful not only to execute an external command but also to retrieve the external output of the command.

What does the external command myExternalCommand : 1/ the shell command ps lists all the running processes (instances of running programs) on your computer, then the result is passed (piped) to the command grep which here fetchs and displays only every line containing the word "chrome" (case insensitive here).

myExternalCommand = "ps -acx -o pid -o command | grep -i chrome$"        

RunProcess[{$SystemShell, "-c", myExternalCommand}, "StandardOutput"]

the "chrome$" pattern means that you look for the word "chrome" at the end of a line ...

1/ the killall shell command must be followed by the plain app name (as displayed by the ps command) ( !!! WARNING : the following command will really quit Chrome)

myExternalCommand="killall 'Google Chrome'"

RunProcess[{$SystemShell, "-c", myExternalCommand}, "StandardOutput"]

or more simply just use for example Run because we don't need to retrieve any useful output here :

Run[myExternalCommand]

2/ the kill shell command must be followed by the app PID (Process ID) number (as displayed by the ps command in the first column, see the output above)

(* WARNING WARNING WARNING WARNING !!!!!*)
(* only run the following \
external command if you know exactly what you are doing, otherwise you could \
quit some other important processes and make damages *)

(*Before running, replace myPIDnumber with the number displayed in \
the output of the ps command above, here I should replace it by 1709 *) \

myExternalcommand = "kill myPIDnumber";
(* Then just the RunProcess or Run command above in 1/*)

killall and kill commands both will send the given app (here Chrome) a request to quit but this might not be taken into account by the app if it has some other higher priority tasks running (writing on disk, ...). It is recommended to use this method first but if it does not work you can force the app to quit immediately by adding to the commands the "-9" option, that means : "kill -9 ..." or "killall -9 ..."

Source Link
SquareOne
  • 7.6k
  • 1
  • 16
  • 35
Loading