Killing background processes in linux

Kill background processes in Linux

I’m trying to build a start script and a stop script for my project. I need to run a sass auto-compiler as well as a server, and redirect the output of both to a file. I’m using lite-server and sass —watch for this. To make these processes run concurrently, I’m using & to background the processes. This poses the challenge of stopping the scripts, I can’t stop the scripts using Ctrl+C as I normally would. I thought I would overcome this by storing the process IDs in a text file. I came up with the following «start» script:

# Start a sass watcher and a server running simultaneously. Store the PIDs in a # text file so that the processes can be easily stopped. ( lite-server & echo $! > .pids.txt & sass --watch sass:css --style=compressed & echo $! >> .pids.txt & ) &> log.txt cat .pids.txt 

Writing the process IDs to a text file seems kind of hackish. Is there a better way to accomplish what I’m trying to do?

Without writing the PIDs to text file, if these processes are bound to run only once (one instance) in your machine, you could use killall command. killall -s KILL lite-server sass (either individually or combined)

@LukeTaylor I liked your approach with writing a file, since you can access it from other shell environments. Specifically, when using SSH for a remote connection. I don’t know of a graceful way to group all the sub-processes and only kill that process. Did you have any luck with finding something mainstream?

4 Answers 4

I may prefer to grep for the process and kill the process instead keeping the PIDs in a file.

ps -ef | grep lite-server | awk '' | kill -9 

I assume second column in output of ps -ef is PID of the process lite-server in your server.

I’ll try to fix your solution using xargs : bash ps -ef | grep lite-server | awk ‘‘ | xargs kill -9

You should add what kind of «kill» you want.. such as: kill -9 $(cat .pids.txt)

You could store them in an array, if that’s what you wouldn’t call hackish.

EDIT: Another way is to just execute:

This kills all background processes (jobs -p prints the PIDs of all background process to stdout, which are then handed to kill).

Sadly I’m also someone who knows next to nothing about bash, I only know one can use arrays in bash but never did myself. The bash reference on arrays ( gnu.org/software/bash/manual/bash.html#Arrays ) seems to indicate, that the use of arrays is very easy. You can just introduce a index variable and increment it everytime you stored something in the array: `((i++)); echo $! > array[i] To kill all the processes with the ids in the array, you could use a for-loop where you decrement i until it’s zero again and issue a kill on every pid found. Please note I didn’t test that code.

Читайте также:  Linux shell get string

In short:

setsid sh -c 'command1 & command2 & command3 & wait' process_group_id=$! # and/or save to a file like '.pgid' echo $process_group_id > .pgid # to kill everything in that process group (mind the dash): kill -TERM -$process_group_id 

Note

Killing with kill $process_group_id will NOT kill the entire group! There must be a preceding — when killing an entire process group by id.

Also, you should pass the appropriate signal using kill . To print all the signals, run:

kill -l 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR 31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3 38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8 43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2 63) SIGRTMAX-1 64) SIGRTMAX 

So a forced kill would be:

kill -9 -$process_group_id # OR kill -SIGKILL -$process_group_id 

But if the commands have a way of handling SIGTERM:

kill -15 -$process_group_id # OR kill -TERM -$process_group_id # OR kill -SIGTERM -$process_group_id 

There is more in kill: Send a signal to processes if you are using GNU coreutils. If not, maybe man kill or info kill could help better.

Источник

How to Kill a Background Process in Linux

Linux is a powerful and flexible operating system that is popular among developers and system administrators. One of benefits of Linux is that it allows users to run multiple processes simultaneously, which can increase productivity and efficiency. However, sometimes a background process may become unresponsive or cause system performance issues. In such cases, it becomes necessary to kill process. In this article, we will discuss how to kill a background process in Linux.

Understanding Background Processes

Before we dive into process of killing a background process, it’s essential to understand what background processes are and why they are important.

In Linux, a process is a running instance of a program or application. A background process is a process that runs in background, without requiring user input or interaction. These processes typically perform tasks that don’t require user interaction or are scheduled to run at a later time.

Background processes are essential in Linux as they enable users to run multiple tasks simultaneously. For example, a user can start a download and continue working on another task without interruption.

However, sometimes a background process can cause system performance issues or become unresponsive. In such cases, it becomes necessary to kill process.

Identifying Background Processes

Before killing a background process, you need to identify process. You can use following command to list all processes running on your system −

This command will list all processes running on your system, along with their process ID (PID) and other details. PID is a unique identifier for each process, which you will need to kill process.

Once you have identified process, you can use following command to send a signal to process −

The signal is a message that tells process what to do. default signal is SIGTERM, which tells process to terminate gracefully. However, if process is unresponsive or causing system performance issues, you may need to send a different signal, such as SIGKILL, which forces process to terminate immediately.

Читайте также:  Astra linux dpkg обнаружил ошибку во время установки завершено

Killing a Background Process

To kill a background process, follow these steps −

  • Step 1 − Identify process Run ps -aux command to identify process you want to kill. Make a note of process ID (PID).
  • Step 2 − Send a signal to process Use kill command to send a signal to process. syntax for kill command is as follows −

For example, to send a SIGTERM signal to a process with PID 1234, use following command −

Alternatively, you can use following command to send a SIGKILL signal to process −

  • Step 3 − Verify process has been killed Run ps -aux command again to verify that process has been killed. If process is still running, repeat steps 2 and 3 with a different signal.

Killing Multiple Background Processes

If you need to kill multiple background processes, you can use killall command. killall command allows you to kill all processes with a particular name. For example, to kill all instances of firefox process, use following command −

You can also use -9 option to send a SIGKILL signal to processes −

Killing Background Processes with System Monitor

If you prefer a graphical interface, you can use System Monitor application to kill background processes. To open System Monitor, follow these steps −

  • Step 1 − Open System Monitor Open System Monitor application from Applications menu.
  • Step 2 − Identify process Click on Processes tab to view all running processes. You can sort processes by clicking on column headings.
  • Step 3 − Kill process Select process you want to kill and click End Process button. This will send a SIGTERM signal to process. If process does not terminate within a few seconds, you can click Force Quit button to send a SIGKILL signal to process.

Additional Methods to Kill a Background Process in Linux

Apart from using kill command and System Monitor application, there are a few other methods that you can use to kill a background process in Linux −

Pkill Command

The pkill command is similar to killall command, but it allows you to kill processes based on their name or other attributes. For example, to kill all processes with name firefox, use following command −

You can also use -f option to match full command line instead of just process name −

Xkill Command

The xkill command is a graphical utility that allows you to kill a process by clicking on its window. To use xkill command, open a terminal window and type following command −

Your cursor will change to a skull and crossbones icon. Click on window of process you want to kill, and process will be terminated.

Htop Command

The htop command is an interactive process viewer that allows you to view and manage running processes. To install htop, run following command −

To launch htop, type following command −

You can navigate through processes using arrow keys and use F9 key to send a signal to selected process. Select process you want to kill and press F9 key. You can then select signal you want to send to process, such as SIGTERM or SIGKILL.

Conclusion

Killing a background process in Linux is a simple process that can be done using kill command or System Monitor application. It’s important to identify process before attempting to kill it and to use appropriate signal for situation. By following these steps, you can efficiently manage processes running on your system and improve system performance.

Читайте также:  Linux see symbols in so

Источник

How to Kill a Background Process in Linux

Linux is a multi-user and multi-task operating system. It supports more than one user and can run multiple processes simultaneously. Technically, that is not the case; the Linux kernel uses scheduling and other process management methods to assign a specific time to each process, making them appear to run simultaneously.

However, Linux allows us to perform tasks to the running processes, including background and foreground jobs.

This tutorial will discuss how to work with background processes and terminate them using various commands.

How to Run a Command in the Background

In most cases, when running a command from the Linux terminal, we wait for it to complete and exit. This functionality can be helpful for short commands or commands that require user interaction. However, in the case of processes that take a long time to complete, it can prevent you from running other commands.

Although pseudo-terminals can escape this, this becomes a problem in an sole terminal-based environment.

Use & Symbol

In Linux, one method to run a command in the background is to use the & symbol as:

The above syntax tells the shell to put whatever commands precede the ampersand in the background.

Once you put a process in the background, the shell will give you the job ID enclosed by a pair of square brackets and the PID (process ID).

Use CTRL + Z

Another method to put a process in the background is to use the CTRL + Z shortcut. Suppose we forgot to add the ampersand when running a program.

To put the said process in the background, we can press the CTRL + Z key and suspend the job. It is good to note that this does not terminate the process; it only freezes it.

To resume the process in the background, use the bg command:

As seen in the screenshot above, we run the Firefox process in the foreground, which “eats” our prompt until we terminate the process.

We freeze the process using the CTRL + Z shortcut and put it in the background using the bg command.

How to Show Running (and Stopped) Background Processes

To show the background processes, we use the jobs -l command:

The command will show both the running and stopped processes.

How to Bring a Background Process to the Foreground

To bring a background process in the foreground, you use the fg command followed by %[job id]

How to Kill a Background Process

Killing a background process is fairly straightforward; use the command pkill and the process ID, or process name as:

Using the pkill command will force terminate (-9) the processes with the process name of ping.

Conclusion

This guide walked through the basics of job control using foreground and background processes.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list

Источник

Оцените статью
Adblock
detector