- How to kill a process or stop a program in Linux
- Here are several options for terminating a program in Linux using the command line or a graphical interface.
- Subscribe now
- Using the command line/termination characters
- Ctrl + C
- Ctrl + Z
- Using kill
- Using killall
- Using xkill
- Ubuntu Linux Stop a Process
- Gnome: System Monitor Application To Kill a Process
- How Do I End a Process?
- kill Command Line Option
- pkill Command Line Option
- killall Command Line Option
- Linux: How to write a System V init script to start, stop, and restart my own application or service
How to kill a process or stop a program in Linux
Here are several options for terminating a program in Linux using the command line or a graphical interface.
Subscribe now
Get the highlights in your inbox every week.
When a process misbehaves, you might sometimes want to terminate or kill it. In this post, we’ll explore a few ways to terminate a process or an application from the command line as well as from a graphical interface, using gedit as a sample application.
Using the command line/termination characters
Ctrl + C
One problem invoking gedit from the command line (if you are not using gedit & ) is that it will not free up the prompt, so that shell session is blocked. In such cases, Ctrl+C (the Control key in combination with ‘C’) comes in handy. That will terminate gedit and all work will be lost (unless the file was saved). Ctrl+C sends the SIGINT signal to gedit . This is a stop signal whose default action is to terminate the process. It instructs the shell to stop gedit and return to the main loop, and you’ll get the prompt back.
Ctrl + Z
This is called a suspend character. It sends a SIGTSTP signal to process. This is also a stop signal, but the default action is not to kill but to suspend the process.
It will stop (kill/terminate) gedit and return the shell prompt.
Once the process is suspended (in this case, gedit ), it is not possible to write or do anything in gedit . In the background, the process becomes a job. This can be verified by the jobs command.
jobs allows you to control multiple processes within a single shell session. You can stop, resume, and move jobs to the background or foreground as needed.
Let’s resume gedit in the background and free up a prompt to run other commands. You can do this using the bg command, followed by job ID (notice [1] from the output of jobs above. [1] is the job ID).
This is similar to starting gedit with &, :
Using kill
kill allows fine control over signals, enabling you to signal a process by specifying either a signal name or a signal number, followed by a process ID, or PID.
What I like about kill is that it can also work with job IDs. Let’s start gedit in the background using gedit & . Assuming I have a job ID of gedit from the jobs command, let’s send SIGINT to gedit :
Note that the job ID should be prefixed with % , or kill will consider it a PID.
kill can work without specifying a signal explicitly. In that case, the default action is to send SIGTERM , which will terminate the process. Execute kill -l to list all signal names, and use the man kill command to read the man page.
Using killall
If you don’t want to specify a job ID or PID, killall lets you specify a process by name. The simplest way to terminate gedit using killall is:
This will kill all the processes with the name gedit . Like kill , the default signal is SIGTERM . It has the option to ignore case using -I :
To learn more about various flags provided by killall (such as -u , which allows you to kill user-owned processes) check the man page ( man killall )
Using xkill
Have you ever encountered an issue where a media player, such as VLC, grayed out or hung? Now you can find the PID and kill the application using one of the commands listed above or use xkill .
xkill allows you to kill a window using a mouse. Simply execute xkill in a terminal, which should change the mouse cursor to an x or a tiny skull icon. Click x on the window you want to close. Be careful using xkill , though—as its man page explains, it can be dangerous. You have been warned!
Refer to the man page of each command for more information. You can also explore commands like pkill and pgrep .
Источник
Ubuntu Linux Stop a Process
H ow do I stop a process under Ubuntu Linux using command line and GUI tools?
You can use the following tools to stop a process under Ubuntu Linux:
- System Monitor application – A gui tools displays current active processes. it also provides detailed information about individual processes, and enables you to control active processes i.e. kill or end process.
- kill command – Send signal to a process such as kill or end a process.
- pkill command – Find processes or processes based on name and send end or kill singles to processes.
- killall command – Kill processes (including all its children) by name.
Gnome: System Monitor Application To Kill a Process
To start System Monitor GUI, click on System menu > Select Administration > System Monitor . Alternatively, open a command-line terminal (select Applications > Accessories > Terminal), and then type:
$ gnome-system-monitor
Click on Processes Tab:
Fig.01: A list of process and end Process button
How Do I End a Process?
- First select the process that you want to end.
- Click on the End Process button. You will get a confirmation alert. Click on “End Process” button to confirm that you want to kill the process.
- This is the simplest way way to stop (end) a process.
kill Command Line Option
You can use the kill command to send a signal to each process specified by a process identifier (PID). The default signal is SIGTERM (15). See the list of common UNIX / Linux signal names and numbers for more information. In this example, ps command is used to find out all running processes in the system:
$ ps aux | grep firefox
To end a process, enter:
$ kill -s 15 PID-HERE
$ kill -s 15 2358
OR send signal 9 (SIGKILL) which is used for forced termination to PID # 3553:
$ kill -9 PID-HERE
$ kill -9 3553
See our previous FAQ “how to kill a process in Linux” for more information.
pkill Command Line Option
The pkill command allows you to kill process by its name, user name, group name, terminal, UID, EUID, and GID. In this example, kill firefox process using pkill command for user vivek as follows:
$ pkill -9 -u vivek firefox
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
killall Command Line Option
The killall command sends a signal to all processes. To terminate all httpd process (child and parent), enter:
$ sudo killall -9 httpd
OR
$ sudo killall -9 apache2
See sending signal to Processes wiki article for more information.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник
Linux: How to write a System V init script to start, stop, and restart my own application or service
System V (abbreviated as SysV) is most widely used across most Linux distributions. But what is System V?
Init is the program on Unix and Linux systems which spawns all other processes. It runs as a daemon and typically has PID 1. It is the parent of all processes. Its primary role is to create processes from a script stored in the file /etc/inittab file. The main advantages is flexibility and scalability provided by SysV.
The Runlevels in System V describe certain states. For example:
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
- Runlevel 0: Halt
- Runlevel 1: Single user mode
- Runlevel 6: Reboot
All System V init scripts are stored in /etc/rc.d/init.d/ or /etc/init.d directory. These scripts are used to control system startup and shutdown. Usually you will find scripts to start a web server or networking. For example you type the command:
# /etc/init.d/httpd start
OR
# /etc/init.d/network restart
In above example httpd or network are System V scripts written in bash or sh shell. Here is a sample shell script:
Above script is specific to Cent OS or Fedora Core/Red Hat Linux. But it should work on all other Linux distro as well. Make sure you replace the FOO name (word/path start with FOO) with actual application name.
Category | List of Unix and Linux commands |
---|---|
Documentation | help • mandb • man • pinfo |
Disk space analyzers | df • duf • ncdu • pydf |
File Management | cat • cp • less • mkdir • more • tree |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Linux Desktop Apps | Skype • Spotify • VLC 3 |
Modern utilities | bat • exa |
Network Utilities | NetHogs • dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop |
Searching | ag • grep • whereis • which |
Shell builtins | compgen • echo • printf |
Text processing | cut • rev |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Comments on this entry are closed.
Don’t forget to highlight the description and chkconfig comments at the top, they’re what the chkconfig utility uses to figure out what run levels to start/stop the script in. It’s not necessary for the script to work, but if the comments are in there they should be right!
Hello, I have a problem and would like some real help. I believe it deals with the topic just discussed. At this point, I’m not sure of the questions I’m supposed to be asking. Here’s my problem: I’m about to start programming some “multiplayer†games for the blind. The games will be text-based games (no heavy load graphics). I am going to write my game interface using my ‘visual studio 2005’ software in c++. All the players will have a copy of my game interface on their computers. The game interface will send each player’s move or ‘turn decision’ to a ‘constantly’ running script, which will be ‘listening’ for clients on the Internet. The script will then send the ‘move’ to all the other players of the game so everyone has the same instance of the game at the same time. I have never done this before. I am just extrapolating on what I like to call the ‘logical progression flow’. I have a domain (htpad.com) with the host4profit website hosting service. They have two files in my root directory: ‘.bash_profile’ and ‘.bashrc’. I know these two files have something to do with the ‘bash’ shell. I was also told I would need to create a ‘deamon bash script’ if I wanted a script that would continuously ‘listen’ on the Internet for clients in order to process their moves. My question is this: Am I on the write track, and how do I create a ‘safe’ deamon script that will not make my host provider nervous?
This is script is an example to start or stop daemon or service. You are coding in C/C++ and you need to write daemon using same language until and unless you are writing API for perl or php.
Is a daemon simply a program that I write in ‘bash’ and is always running on my host’s server and it simply calls my pearl script that does all the ‘ios::socket’ listening? I’m not sure what a daemon is and how to create one. Please explain or give me a link to a text file explaining the daemon file. P.S. Should this be a new topic? If so, where should I post it. I would need to post the original message.
Yes, I agree with comment#4, How we program FOO is much more important than a bash script.
When I use the script for a test daemon, get the following error for line:45 which the echo statement below. Am I missing something?
./testd: line 45: restart: command not found
–cmd and –run are incompatible with –string or –name
./testd: line 45: reload: command not found
./testd: line 45: status>â€: command not found
If you cut and past this example, it may have inserted the wrong ” characters. Simply replace the double quotes in your copy and it should work.
I would be interested to know if and how the game for the blind was written.
For those who are used to J2EE, Apache modules, .NET and all those other technologies, something that does netcat | sed … | netcat might be a refreshing change.
I get :
[root@mybox init.d]# service myserviced start
env: /etc/init.d/myserviced: No such file or directory
the file does exists and its executable.
chkconfig myserviced on
went without complaining? any tips?
I’m on Centos 5.5.
beware of extra CR while editing text files in DOS format..
I will use this script to restart mysql service , so I will change the following :
/path/to/FOO & –> to be /retrovue/home/mysql
retrovue ( user )
touch /var/lock/subsys/FOO –> to be touch /var/lock/subsys/mysql
killproc FOO —-> to be killproc mysql
rm -f /var/lock/subsys/FOO –> rm -f /var/lock/subsys/mysql
How I can make this Script run daily at 01:00 AM
I wait your reply ASAP
I haven’t used mysql for a few years, but it should have a System V service script of its own, or use upstart or whatever replaces System V services on your machine.
You have to specify what operating system, version etc.
Note also that the above script probably won’t work on Debian based systems as there is no /etc/init.d/functions – and you have to do all those things the slow way.
Both Fedora and Ubuntu have moved on to Upstart (and I believe Fedora have something even newer), so the System V service scripts become an emulation.
With System V you would use a command like:
service restart mysql
With Upstart this becomes something like:
restart mysql
To do this daily at 01:00 you would have to edit your root user’s crontab, and place the commands there – like
..and place a line in the file like:
00 01 * * * restart mysql
But then I have questions for you (to think about):
How long have you been using Unix/Linux?
If this is a production server – do you need expert advice, rather than doing it on your own?
Why is there a need to restart a database server every night?
Especially the last question. I can’t imagine that recent releases of MySQL have memory leaks or other problems. I leave database servers running for months at a time with no problems.
Maybe the problem is rather that you have long running queries that you don’t control?
Does this work by placing in the /etc/init.d or do we have to link them to rc3.d and rc0.d. if so… can you please let me know the detailed steps as well…
Do we need to link these to rc3.d and rc0.d . If so, please let me know the detailed procedure apart from placing this script in /etc.init.d/
Also please let me know how to check if the service is stopped before the server is down.
Once again, I do’t know what operating system etc., so it’s difficult to answer.
Both Debian/Ubuntu and Fedora/Redhat/CentOS have ‘chkconfig’ scripts that set up the System V style links. If the init.d script is written correctly (with comments indicating the runlevels required), it is enough to do ‘chkconfig myservice on’.
(chkconfig might not be installed by default on Ubuntu, use apt-get…)
If the scripts are written correctly, then the ‘stop’ is invoked before system shutdown (which means – transition to runlevel 0).
Источник