Tag Info

Hot answers tagged

110

You should be able to do this in the same shell you're in with the wait command: $ sleep 30 & [1] 17440 $ wait 17440 && echo hi ...30 seconds later... [1]+ Done sleep 30 hi excerpt from Bash man page wait [n ...] Wait for each specified process and return its termination status. Each n may be a process ID or a ...


72

There are 3 common ways of doing this: Pipefail The first way is to set the pipefail option (ksh, zsh or bash). This is the simplest and what it does is basically set the exit status $? to the exit code of the last program to exit non-zero (or zero if all exited successfully). # false | true; echo $? 0 # set -o pipefail # false | true; echo $? 1 ...


71

If you are using bash, you can use the PIPESTATUS array variable to get the exit status of each element of the pipeline. $ false | true $ echo "${PIPESTATUS[0]} ${PIPESTATUS[1]}" 1 0 If you are using zsh, they array is called pipestatus (case matters!) and the array indices start at one: $ false | true $ echo "${pipestatus[1]} ${pipestatus[2]}" 1 0 To ...


60

When a process exits, all its children also die (unless you use NOHUP in which case they get back to init). This is wrong. Dead wrong. That person has been lying to you, either when they said that or when they said they knew something about Unix and processes. There are two ways in which the death of a process can indirectly cause the death of its ...


59

fg returns with the exit code from the program it resumes. You can therefore suspend your program with ^Z and then use fg && ... to resume it. $ /bin/myprog some output... ^Z [1]+ Stopped /bin/myprog $ fg && /usr/bin/mycleanup


53

() runs commands in the subshell, so by exit you are exiting from subshell and returning to parent shell. Use braces {} if you want to run commands in the current shell. From bash manual: (list) list is executed in a subshell environment. Variable assignments and builtin commands that affect the shell's environment do not remain in effect after ...


28

nohup read the man page for nohup usage. nohup is the way it's been done long since before screen, tmux, etc were invented. Example: nohup my_long_running_proc & Runs "my_long_running_proc", and any console (stdout/stderr) messages go into a file called "nohup.out" in the directory from which the command was started.


22

You can use false (/bin/false, /usr/bin/false, or shell builtin): $ false || echo It failed. It failed. $ You can also use exit 1 from a subshell: $ (exit 1) || echo Gosh, it failed too. Gosh, it failed too. $


22

There is no significance to exiting with code 99, other than there is perhaps in the context of a specific program. Either way, exit exits the shell with a certain exit code, in this case, 99. You can find more information in help exit: exit: exit [n] Exit the shell. Exits the shell with a status of N. If N is omitted, the exit status is that ...


19

I think it works. But probably it doesn't do what you expect. $ exit & Will create a sub-shell process, and make it run as a background job which will just finish right away.


18

While not exactly what you asked, you could use #!/bin/bash -o pipefail so that your pipes return the last non zero return. might be a bit less coding Edit: Example [root@localhost ~]# false | true [root@localhost ~]# echo $? 0 [root@localhost ~]# set -o pipefail [root@localhost ~]# false | true [root@localhost ~]# echo $? 1


15

Try using the ssh connection termination escape sequence. In the ssh session, enter ~. (tilde dot). You won't see the characters when you type them, but the session will terminate immediately. $ ~. $ Connection to me.myhost.com closed. From man 1 ssh The supported escapes (assuming the default ‘~’) are: ~. Disconnect. ~^Z Background ssh. ...


14

What I do when possible is to feed the exit code from foo into bar. For example, if I know that foo never produces a line with just digits, then I can just tack on the exit code: { foo; echo "$?"; } | awk '!/[^0-9]/ {exit($0)} {…}' Or if I know that the output from foo never contains a line with just .: { foo; echo .; echo "$?"; } | awk '/^\.$/ {getline; ...


14

It does work. & forks the shell, starting a new process (you could think of it as & exit, except of course that syntax actually means something else). exit is a shell built-in that ends the shell process -- in this case the new backgrounded shell. > exit & [1] 1709 > ps -p 1709 PID TTY TIME CMD [1]+ Done ...


13

Because in UNIX/POSIX, the exit code of a program is defined to be an unsigned 8-bit value. Converting -1 to unsigned 8-bit gives 255. Edit to add: To give more detail: the wait*() family of system calls in UNIX encode the result of a process into a single 32bit integer. The 32 bits of that result are further broken up to provide information such as ...


13

The problem is that you're issuing wait in a subshell: if `wait $!`;then Because wait is a builtin, not a command, it's operating on the subshell, not your current shell. The output that you would see but aren't is: wait: pid 12344 is not a child of this shell ...with a return status of 1. To perform your test you will need to do it without using a ...


12

The status passed to exit() by a process is masked to a single byte, so it's limited to the range [0-255]. See the exit documentation: The value of status may be 0, EXIT_SUCCESS, EXIT_FAILURE, or any other value, though only the least significant 8 bits (that is, status & 0377) shall be available to a waiting parent process. On two's complement ...


12

In addition to @Chris Down, there is some return code that reserved for the shell, they have special meaning: RETVAL Meaning 1 General errors 2 Misusage 127 Command not found You can refer to this for more details.


11

The only way that statement can be reached is if the exec itself fails; if it succeeds, the ffmpeg command replaces the shell completely. (Pedantically, the && will fail in that case also so it can't be reached at all.) You don't want to exec it, just run it.


11

Ctrl-D or exit is the same. If you using the "X", the window manager sends a terminate signal to the shell. Which has at the end the same result. If the shell does not react, the user usually gets a warning and can then force the shell to end (equal to kill -9 $PID). There is one small difference between the "X"- and exit-method if you are using zsh as ...


11

Executing the exit in a subshell is one pitfall: #!/bin/bash function calc { echo 42; exit 1; } echo $(calc) The script prints 42, exits from the subshell with return code 1, and continues with the script. Even replacing the call by echo $(CALC) || exit 1 does not help because the return code of echo is 0 regardless of the return code of calc. And calc is ...


10

This solution works without using bash specific features or temporary files. Bonus: in the end the exit status is actually the exit status and not some string in a file. Situation: someprog | filter you want the exit status from someprog and the output from filter. Here is my solution: stdintoexitstatus() { read exitstatus return $exitstatus } ...


10

Starting from the pipeline: foo | bar | baz Here is a general solution using only POSIX shell and no temporary files: exec 4>&1 error_statuses="`((foo || echo "0:$?" >&3) | (bar || echo "1:$?" >&3) | (baz || echo "2:$?" >&3)) 3>&1 >&4`" exec 4>&- $error_statuses contains the status codes ...


10

I think you are mixing two things the return value typically indicates if a command was successful (return value 0) or not (anything else). You can get the return value of a command from the variable $? grep -c returns the count to stdout, to capture the count you can use something like variable=$(grep -c pattern filename) Afterwords you can ...


9

set -e ? set: set [-abefhkmnptuvxBCHP] [-o option-name] [--] [arg ...] Set or unset values of shell options and positional parameters. Change the value of shell attributes and positional parameters, or display the names and values of shell variables. Options: -a Mark variables which are modified or created for export. -b Notify of job ...


9

Three answers written in 2014, all saying that in Unices and in Linux the process is reparented to process #1 without exception. Three wrong answers. ☺ As the SUS says, quoted in one of the other answers here so I won't quote it again, the parent process of orphaned children is set to an implementation-defined process. Cristian Ciupitu is right to ...


8

Both the kernel and the C runtime do some of the work. Some of the things that the C runtime does which the kernel doesn't do: it runs handlers previously registered with atexit() and it arranges for the integer return value from main() to be returned to the system as if with exit(). Of course in the end the kernel will reap all resources (files, memory) ...


8

Perhaps wmctrl could be of some assistance. You could use the -c option that closes a window gracefully: wmctrl -c chrome The string chrome is matched against the window titles. Note that the window might not close if some message pops-up (e.g. when you have multiple tabs open).


8

You could decide that the exit status 77 for instance means exit any level of subshell, and do set -E trap '[ "$?" -ne 77 ] || exit 77' ERR (echo here; (echo there; (exit 12); echo ici; exit 77); echo not here); echo not here either set -E in combination with ERR traps is a bit like an improved version of set -e in that it allows you to define your own ...


8

Use tmux or screen to provide a persistent session environment for the command to run in. Using tmux, this could be accomplished in the following way: Start a new session environment: tmux new -s my-session-name Run your command: longrunningcommand Close your terminal window, SSH session or manually detach from tmux using Ctrl+b, then d Reconnect to your ...



Only top voted, non community-wiki answers of a minimum length are eligible