Here I presents an overview of some useful shell scripts for starting and stopping background process, manually, or by other shells or programmes.
To start and stop background processes, in most situations we need their PID (process ID).
To find out the PID of the process
use the command;
ps
To show all the processes;
ps -A
To find out the PID and the PGID (process group ID) of the processes;
ps -o cmd,pid,pgid
To get the PID for the given process name;
pgrep< process name>
pgrep bash
To kill the process
To kill the process by process name;
pkill < process name>
pkill java
PID kill -9 < >
kill -9 12345
To kill by
PGID kill -9 -< >
kill -9 -9876
To kill process by their process name;
process name pkill < >
pkill java
Now we'll move in to background processes. These
- Other processes want
the same terminal to execute them selves. executedWhen time consuming processes is - Several processes n
eed to be started one after other etc
There is a very easy way
e.g.
2>&1 & ./server.sh
./server.sh > server.log 2>&1 &
Note: If you use '2>&1', the script won't return the terminal, until the processing is over.
But when we start and kill processes pragmatically, we encounter a new challenge. We should kill the exact process that we started, and we
To do so, we should keep tack of the PID of the processes we started.
In order to get the PIDs, we can add the following code to the shell script that we are running.
To get the PID of the process that is currently running;
echo $$ > server.pid
Or to get the PID of the process,
echo $! &> server.pid
Well, now we have the PID. Next, we have to kill the processes we stated.
If we only have single processes - which don't have child processes
cat server.pid | xargs -i kill -9 {}
But
If you want to view the process tree, you can use the command;
ps f
Or for a detailed view;
pstree
kill the process tree (recommended) kill the processes which contains the same PGID (this works because in most cases the whole process tree will have the same process group id (PGID))
Killing the process tree using the parent's PID
you can uses that following shell script;
#!/bin/sh
$1 for i in `ps -ef| awk '$3 == '${ }' { print $2 }'`
do
echo killing $i
kill -9 $i
done
echo killing $1
kill -9 $1
Save this as 'kill-process-tree.sh'
kill-process-tree.sh chmod 755
Then use the command to run the script;
./kill-process-tree.sh
./kill-process-tree.sh 7234
Killing the processes which contains the same PGID
First we need to find out the PGID of the given PID of the process;
echo $! |xargs echo | xargs -i ps -o pgid -p {}|xargs echo |awk '{print $2}' > server.pgid
Then to kill the processes with give PGID use the command;
cat server.pid | xargs -i kill -9 -{}