Closing program in linux

Proper way to exit command line program?

I’m using mac/linux and I know that ctrl-z stops the currently running command in terminal, but I frequently see the process is still running when i check the system monitor. What is the right way to stop a command in terminal? Typically I run into this issue when running python or ruby apps, i’m not sure if that has something to do with it, just thought I would add that.

also, i have to force quit the process from activity monitor as opposed to just quitting it to get it to stop, that could mostly be a fault of the type of program i was running, but i felt it was worth mentioning

3 Answers 3

Using control-z suspends the process (see the output from stty -a which lists the key stroke under susp ). That leaves it running, but in suspended animation (so it is not using any CPU resources). It can be resumed later.

If you want to stop a program permanently, then any of interrupt (often control-c ) or quit (often control-\ ) will stop the process, the latter producing a core dump (unless you’ve disabled them). You might also use a HUP or TERM signal (or, if really necessary, the KILL signal, but try the other signals first) sent to the process from another terminal; or you could use control-z to suspend the process and then send the death threat from the current terminal, and then bring the (about to die) process back into the foreground ( fg ).

Note that all key combinations are subject to change via the stty command or equivalents; the defaults may vary from system to system.

Источник

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.

x sign

Thomas Hawk via Flickr. CC BY-NC 2.0

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.

Читайте также:  Nut linux ups настройка

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 :

 $ gedit & [1] 14852 $ killall -I GEDIT [1]+ Terminated gedit

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 .

Using 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 .

Источник

How to gracefully kill (close) programs and processes via command line

Using a program called wmctrl, you can close programs the way they are meant to be shutdown. wmctrl has many other uses besides this, but for this howto, the close functions is all we care about.

  • -c
    • Close the window gracefully.
    • Example:
      • For window title: theory of evolution — OpenOffice.org Writer
      • could be: openoffice
      • You want to close OpenOffice.org
        • The title of the window is theory of evolution — OpenOffice.org Writer
        • Execute: wmctrl -c openoffice
        • If the file is unsaved, it will pop-up, Do you want to save.
          • Note it will only close one instance of OpenOffice, so if you have two files open, you would need to run it twice.

          Console programs [ ]

          To close console programs you use the kill command. Kill has a bunch of different signals that can be sent to a programs to initial a close.

          It is not always obvious what kill signal will close the program gracefully so it is likely you will have to test out different signals, and figure out which signal closes the programs better. If you are lucky the programs user man will give you a hint.

          • You want to close, rtorrent , allow it to first close all connections
            • The user guide states: SIGINT: Normal shutdown with 5 seconds to send the stopped request to trackers.[1]
            • SIGINT is #2
            #!/bin/bash # rtorrent if [[ -n `pidof rtorrent` ]];then echo "rtorrent is open, time to die (gracefully). " kill -2 `pidof rtorrent` fi

            Close command for specific programs [ ]

            • rtorrent
              • kill -2 `pidof rtorrent`
              • To close a single window of Firefox wmctrl -c Firefox
              • To quit Firefox, entirely (closing all windows)
              if [[ -n `pidof firefox` ]];then WID=`xdotool search "Mozilla Firefox" | head -1` xdotool windowactivate --sync $WID xdotool key --clearmodifiers ctrl+q fi
              • OpenOffice.org
                • wmctrl -c openoffice If you have multiple instances of OpenOffice, you need to execute the close command more than once
                • wmctrl -c libreoffice If you have multiple instances of OpenOffice, you need to execute the close command more than once
                • wmctrl -c gimp
                • wmctrl -c inkscape
                • wmctrl -c qBittorrent
                • gnome-session-save—kill
                #!/bin/bash # rtorrent if [[ -n `pidof rtorrent` ]];then echo "rtorrent is open, time to die (gracefully). " (kill -2 `pidof rtorrent` &) fi # mozilla firefox. if [[ -n `pidof firefox` ]];then echo "closing firefox. " WID=`xdotool search "Mozilla Firefox" | head -1` xdotool windowactivate --sync $WID xdotool key --clearmodifiers ctrl+q fi # galeon. if [[ -n `pidof galeon` ]];then echo "closing galeon. " (galeon -q &) fi # openoffice.org if [[ -n `pidof soffice.bin` ]];then echo "closing openOffice (if its open). " (wmctrl -c OpenOffice &) fi # LibreOffice if [[ -n `pidof soffice.bin` ]];then echo "closing LibreOffice. " (wmctrl -c LibreOffice &) fi # Inkscape if [[ -n `pidof inkscape` ]];then echo "closing Inkscape. " (wmctrl -c Inkscape &) fi # Gimp echo "closing Gimp (if its open). " (wmctrl -c gimp &) # Pidgin. echo "closing pidgin (if its open). " (purple-remote "quit" &) # Transmission. echo "closing Transmission (if its open). " (wmctrl -c Transmission &) # qBittorrent echo "Closing qBittorrent (if its open). " (wmctrl -c qBittorrent &) if [[ -n `pidof VirtualBox` ]];then WID=`VBoxManage list runningvms|sed 's/.*//'` VBoxManage controlvm $WID savestate fi

                See also [ ]

                Источник

                How to Stop a Program in Linux Terminal

                It’s amusing how the simplest of the things like stopping a running program could be overwhelming when you are new to the Linux command line.

                It’s amusing how the simplest of things could be complicated when you are new to something. The other day, I found my friend could not figure out how to exit the top command. Instead of stopping the command, he closed the entire terminal application. That’s not only unnecessary, but it is also a not good thing to do.

                Stopping programs in Linux

                stop a program linux terminal

                In Linux, you can use the Ctrl+C keys to stop a running program in the terminal. This works for Ubuntu as well as any other Linux distribution. Take the ping command for example. If you do not stop it, it will keep on displaying the result. Hold the Ctrl button and press the C key at the same time. It sends the SIGKILL signal to the running program to force quit the command. Do you see the ^C? The caret (^) means Ctrl. So basically, the terminal shows the Ctrl+C keystrokes as ^C. The Ctrl+C works very well for the commands designed to keep running until interrupted. You feel like you have to cancel the command, use Ctrl+C. You can find the process ID and kill a running process in a more complicated method. That’s more advanced stuff and used only when the process is running in the background or by another user or in another terminal window. Apart from that, some other commands and command line tools have their own exit commands. Let me briefly mention some of them here.

                How to exit Vim editor

                How to exit Vim editor in Linux

                Existing Vim editor has made so many jokes in the Linux world. It is difficult to figure out when you are new to this powerful command line based text editor. Among several ways of quitting vim, the most common is to press the Esc key and then type a colon (:) and then type q! for force quit without save or wq for save and quit.

                How to exit Nano editor

                Save and Exit Nano editor

                Quitting the Nano editor is a bit simpler than exiting Vim. Why? Because Nano mentions the shortcut at the bottom. You may not understand it if you are new to it but at least you’ll be able to figure it out the next time. To exit Nano, press Ctrl+X. It will ask if you want to save the changes made to the file or not. You can enter your choice.

                How to exit less command

                The less is an excellent command that lets you view without cluttering your terminal screen like the cat command. If you are inside the less command view, use the key q to exit less.

                How to exit the terminal

                To exit the terminal itself, instead of closing the terminal, either use Ctrl+D keyboard shortcut or type the exit command:

                This actually exists you from the current shell. When you open a terminal in Ubuntu or any other Linux distribution, it runs the default shell. When you exit from this shell, terminal ends as well. Ctrl+D is the shortcut to do the same and quit the terminal. I hope you find this quick tutorial helpful. I highly recommend learning these Linux command tips.

                Источник

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