Unix & Linux: Terminating a bash shell script running in the background (3 Solutions!!)

preview_player
Показать описание
Unix & Linux: Terminating a bash shell script running in the background

The Question: I often use bash shell scripts to run simple commands for many different files.
For example, suppose that I have the following bash shell script, called
#!/bin/bash

for strname in "a" "b" "c"
do
foo $strname".txt"
done
Also, suppose that foo $strname".txt" is slow, so the execution of the script
will take a long time (hours or days, for example). Because of this, I would
like to use nohup so that the execution continues even if the terminal is
closed or disconnected. I would also like the script to immediately go to the
background, so I will use the & operator. Thus I will use the following command
This works fine for running the script in the background and without hangup,
but now suppose that I would like to terminate the execution at some point for
some reason. How can I do this?
The problem that I have encountered is that, by looking at top, I see only the
well, and so on. For tens or hundreds of text files specified in the for loop,
it becomes a pain to terminate every foo, one by one! So somehow I need to
instead terminate the shell script itself, not the particular calls issued from
the shell script.
When I type the command
ps -u myusername
where myusername is my username, I get a list of processes that I'm running.
But I see two different process IDs called bash. How do I know which of these

Solutions: Please watch the whole video to see all solutions, in order of how many people found them helpful

== This solution helped 12 people ==
You have a couple of options. Since your process is running in the background,
you can use jobs to find it:
...
jobs
kill %1
jobs
You can also use pkill to search the process table for a command line matching;
viz:

== This solution helped 11 people ==
By default, ps will not show the parameters the program was called with. The
options -f and -l both will show the full call.
ps -fu username
will result in output that looks like:

Рекомендации по теме
welcome to shbcf.ru