# Now that we know how to run a process from shell, we will look at # a few more commands that work with processes. So far, we have only # seen ‹kill›, which, given a process ID, sends a signal to the # process. You can learn more about signals in ‹man 7 signal› -- # unfortunately, we won't have time to get into details in the # seminar. # First we will create a process and get its pid, like we did # before: chmod +x spin.sh ./spin.sh & set -x pid=$! # The first command that I want to show you is ‹pgrep›, which allows # you to find processes by looking at the commands those processes # are executing. Of course, you can look at ‹man pgrep›. Without # ‹-f›, it checks only the command itself, with ‹-f› it will also # match on the command line arguments. To make things a little more # complicated, executable scripts sometimes have their filename as # the command name, and sometimes the interpreter name, depending on # the operating system. On ‹aisa›, it is the former, so the first # command will work as we expect. pgrep spin.sh pgrep -f spin.sh # The ‹pgrep› command has a companion called ‹pkill›, which does the # same thing but instead of printing the process id's of the # matching processes, it kills them (sends them a ‘please terminate’ # signal, also known as ‹TERM›). Killing processes forcibly can be # done with the ‹-KILL› switch to either ‹kill› or ‹pkill›. It is # usually better to not use ‹-KILL›. # The last user command for process management that you need to know # is ‹ps›, which is short for ‘processes’ and simply prints a list # of all processes. By default, only interactive processes owned by # you are shown: ps # If we want to see more details, we can add ‹u› to the options. # Notice that unlike most other unix programs, the switches to ‹ps› # can be specified without the dash. ps u # But if you are more comfortable with the dash, you can have it, # too: ps -u # the same as the previous command # We can request to also see processes that do not have a # controlling terminal. ps -x # And finally, we can also request to see all the processes in the # system with ‹ps -a›, but we won't run that in the script because # there are many. You can try running it by hand if you like. # Another command to try yourself would be ‹top›, which # interactively shows current processes in the system and tells you # how much CPU and memory they use. # Okay, let's wait for a bit to check our process is running. Be # sure to notice our process in the process listings from ‹ps›. sleep 3 kill $pid # Let's do an exercise, ‹1.txt›.