Exit program linux command

exit — Unix, Linux Command

exit-The exit command terminates a script, just as in a C program. It can also return a value, which is available to the script’s parent process.

SYNOPSIS

DESCRIPTION

exit-Issuing the exit command at the shell prompt will cause the shell to exit.

In some cases, if you have jobs running in the background, the shell will remind you that they are running and simply return you to the command prompt. In this case, issuing exit again will terminate those jobs and exit the shell. Common aliases for exit include «bye», «logout», and «lo».Every command returns an exit status (sometimes referred to as a return status or exit code). A successful command returns a 0, while an unsuccessful one returns a non-zero value that usually can be interpreted as an error code. Well-behaved UNIX commands, programs, and utilities return a 0 exit code upon successful completion, though there are some exceptions.

Likewise, functions within a script and the script itself return an exit status. The last command executed in the function or script determines the exit status. Within a script, an exit nnn command may be used to deliver an nnn exit status to the shell (nnn must be an integer in the 0 — 255 range).

EXAMPLES

$ exit

output:
# su ubuntu

ubuntu@ubuntu:~$ exit
exit
root@ubuntu:/home/ubuntu#

exit command is used to return the succsess/failure of functionality in script.

#!/bin/bash
echo «This is a test.»
# Terminate our shell script with success message
exit 0

root@ubuntu:/home/ubuntu# ./test.sh
This is a test.root@ubuntu:/home/ubuntu# echo $?
0

root@ubuntu:/home/ubuntu# cat test.sh
#!/bin/bash
echo «This is a test.»
# Terminate our shell script with failure message
exit 1

root@ubuntu:/home/ubuntu# ./test.sh
This is a test.
root@ubuntu:/home/ubuntu# echo $?
1

Источник

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.

Читайте также:  Reaver and kali linux

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 .

Источник

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.

Читайте также:  Как установить flash linux

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.

Источник

Exit Command in Linux

In Linux, the exit command is used to terminate the shell (the current login session) in which it is run. Also, similar to a C program, the exit command is used in shell scripts to terminate the script. Moreover, it returns a value to the script’s parent process.

What Will We Cover?

In this guide, we will see an overview of the exit command in Linux with some use cases.

The Exit Command and Exit Status

You can also use the exit command to specify the exit status of the shell. The exit status is a numeric value that is returned to the parent process when the shell terminates.

The exit status can be used to indicate the success or failure of the command. For example, a shell script might use the exit status to determine whether to perform certain actions based on the success or failure of a command.

To specify the exit status, you can use the exit command followed by a number. For example, exit 0 indicates a successful exit, while exit 1 indicates a failure.

The exit command accepts a single, optional argument which becomes the script’s exit status.

When no argument is passed, the exit status defaults to the exit status of the last command that is executed. In order to see the exit status, use the echo “$?” command.

Examples of Exit Codes (Exit Status)

The exit command is crucial to indicate that a severe error has occurred since the calling processes typically consider the exit codes other than zero as failure cases. If you need a general error trap in your scripts, try using the exit code snippet.

Let’s see some examples of using the exit code in a script and on a terminal:

Example 1: Using the Exit Command with File Expression

Let’s first take an example of a script:

#!/bin/bash
# test-file: Evaluate the status of a file
FILE = / proc / uptime
if [ -e » $FILE » ] ; then # Check the existence of file.
if [ -f » $FILE » ] ; then # Check if it is a regular file.
echo » $FILE is a regular file.»
fi
if [ -b » $FILE » ] ; then # Checks if it is a block file
echo » $FILE is a block device»
fi
else
echo » $FILE is neither a regular nor a block file or may not exist»
exit 1
fi
exit

Читайте также:  Проверить существует ли директория linux

The script performs an evaluation on the file that is allocated to the constant “FILE” and outputs the results as it runs.

First, it checks if it is a regular file or a block file. If the condition is not satisfied, it prints the message in the else block.

The first exit command sets the exit status of the script in case of failure (exit value as 1). The second exit command is a formal expression. This means that when the script completes its execution to the end, it has the exit status of the last command which, in this case, is the exit command. This last exit command will result in success (exit value as 0):

Example 2: Using True and False Commands with the Exit Command

Additionally, entering a simple true or false argument causes the terminal to quit with 0 status code and shows 0 as true. If we instead provide a false statement, the exit status will be 1 since false exits with code 1. Let’s see this in a practical manner. First, issue the true command and check the exit status:

Now, issue the false command and again check the exit status:

Example 3: Interrupting a Script with Ctrl+C

When a user hits Control-C, a special signal known as SIGINT is sent. This interrupts the script or running program, causing it to exit with the code 130. Let’s take an example: suppose you started searching a file with the find command:

The search is still going on. Then, you suddenly interrupted it with “Ctrl+C”. This causes the command to stop with the exit code 130.

Example 4: The Shell Variable SHLVL and the Exit Command

With the shell-specific variable, SHLVL, you can see how many levels deep in the Bash shell you are. Each new shell session that is opened in a terminal increases the session count, starting at 1 for the initial session. If SHLVL is > 1, the exit command closes the current session. In the same way, if the SHLVL is = 1, the exit command logs you out of the system.

To simply put it, when you type “exit” at the command prompt and press Enter, the shell closes (close the current login session) and you will be returned to the shell or command prompt from which you launched it.

Conclusion

We have seen the different use cases of the exit command and the interpretation of the exit status in each case. There are many different exit code numbers for different purposes. Note that these exit codes are Bash specific. If you tend to use other shells such as C-shell or tcsh, the results may differ.

About the author

Ali Imran Nagori

Ali imran is a technical writer and Linux enthusiast who loves to write about Linux system administration and related technologies. You can connect with him on LinkedIn
.

Источник

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