Timeout now sends the exit code of the command, even though the time limit was reached. $ timeout -preserve-status -s SIGINT 5 tail -F /etc/redhat-release Let's run the same command again, this time with the preserve status option. If we want the exit code of the command, we must use the -preserve-status option. Remember, if the timeout reaches it's time limit it will always return the 124 exit code. The exit code is 124, this is because we are getting the exit code of timeout, NOT the command. $ timeout -s SIGINT 5 tail -F /etc/redhat-release Let's tell timeout to send SIGINT after 5 seconds and check the exit code. Now we know that if we send SIGINT to tail it generates a 130 exit code. This will generate an exit code of 130, which means command terminated by CTRL+C. Here we will run the tail command and interrupt it with the CTRL+C keyboard combination (SIGINT). Let's look at some examples to better demonstrate how this works. ![]() This will instruct the timeout utility to use the exit status of the command, even if it times out. You can preserve the exit status of the command by using the -preserve-status option. If the command completes before the specified time limit, it will return the exit status of the command. Timeout will exit with status code 124 if the command reaches the time limit and is timed out. Timeout: sending signal INT to command ‘tail’ Preserving Exit Status (Exit Code) ~]$ timeout -v -s SIGINT 5 tail -F /etc/redhat-release Let's try another example, but this time we will send the SIGINT signal. Timeout: sending signal TERM to command ‘tail’Īs you can see, timeout printed the signal it sent to the tail command. $ timeout -v 5 tail -F /etc/redhat-release We will use the tail command as an example for readability. As mentioned in the previous section, SIGTERM will be used by default. Here is an example of timeout run without specifying a signal. This will print the signal used by timeout to stderr (standard error). If you want to know which signal was used you can supply the -v (-verbose) option. In the last section we discussed using specific signals with timeout. You can get a list of signals by calling the following command: kill -lįor more information on signals see Signal(IPC) at WikiPedia or signal7 Manual Page. We can specify the signal by name or number, like so: timeout -s SIGINT 10 top For example, let's say we wanted to send the SIGINT signal to the top command after 10 seconds. We can specify which signal to send by using the -s (-signal) option followed by the desired signal. Instead we will focus on how to send a specific signal.īy Default the timeout command sends a SIGTERM or termination signal. Outlining all of them is outside the scope of this tutorial. There are may different signals you can send to a command. Now let's explore some of the options for timeout. ![]() Using the timeout command we can tell timeout to exit after 10 seconds. Normally top would run indefinitely until sent the SIGINT signal by pressing CTRL+C on the keyboard. Let's say we wanted to run top on a system to check the running processes. You call timeout, specify the number of seconds you want, then enter the command you want to run. Using the timeout utility is quite simple.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |