📜  kill job linux - Shell-Bash (1)

📅  最后修改于: 2023-12-03 15:17:08.634000             🧑  作者: Mango

kill job in Linux Shell/Bash

Introduction

In Linux, jobs are processes that are started from the current shell session. They can be put in the background and brought back to the foreground. However, sometimes it becomes necessary to stop a job or process that has been started. This is where the kill command comes in.

kill Command

The kill command sends a signal to a process, requesting it to terminate. By default, kill sends a SIGTERM signal to a process, which requests it to gracefully terminate. However, it is also possible to send other signals, such as SIGKILL, which forces a process to immediately terminate.

Syntax
kill [options] [pid] [...]
Options
  • -s [signal]: send a specific signal instead of the default signal SIGTERM.
  • -l: list all available signals.
  • -p: print the process ID of the named process(es), without sending any signals.
  • -a: ignore errors when attempting to kill non-existent processes.
Examples
  1. To terminate a process gracefully (send the SIGTERM signal):
kill 1234

Where 1234 is the process ID of the process to be terminated.

  1. To forcefully terminate a process (send the SIGKILL signal):
kill -9 1234
  1. To terminate all processes with a specific name:
killall firefox

Where firefox is the name of the process to be terminated.

job Command

In the Bash shell, you can put a process in the background by using the & symbol. When you do this, the shell will return a job ID:

$ command &
[1] 1234

You can see all running jobs by running the jobs command:

$ jobs
[1]+ Running command &

To bring a job to the foreground, you can use the fg command:

$ fg %1

Where 1 is the job ID returned by the jobs command.

Combining kill and job

To terminate a job, you can use the kill command with the -job option:

$ kill %1

This will send a SIGTERM signal to the job specified by job ID 1. If you want to forcefully terminate the job, you can use the -9 option:

$ kill -9 %1
Conclusion

In the Linux Shell/Bash, the kill command is a powerful tool for terminating processes. When combined with the job command, it can be used to terminate background jobs with ease.